Container View getting pushed down as if it had a UINavigationBar?

What you are missing here is that a translucent navigation bar sits on top of your viewcontroller’s view, while a non-translucent navigation bar pushes down your view controller’s view (effectively resizing it). So what is happening here is that with a translucent navigation bar, that white space is actually hidden underneath the bar, while when … Read more

In SwiftUI, how to use UIHostingController inside an UIView or as an UIView?

View controllers are not just for the top level scene. We often place view controllers within view controllers. It’s called “view controller containment” and/or “child view controllers”. (BTW, view controller containers are, in general, a great way to fight view controller bloat in traditional UIKit apps, breaking complicated scenes into multiple view controllers.) So, Go … Read more

How to get UIScrollView vertical direction in Swift?

If you use an UIScrollView then you can take benefit from the scrollViewDidScroll: function. You need to save the last position (the contentOffset) it have and the update it like in the following way: // variable to save the last position visited, default to zero private var lastContentOffset: CGFloat = 0 func scrollViewDidScroll(_ scrollView: UIScrollView) … Read more

How add tabs programmatically in UITabBarController with swift?

UPDATE SWIFT 5 One example of how to create an UITabBarController programmatically could be like this: First we create the UIViewControllers that will be the content for each tab of the tab bar interface. For this example we only create one very simple. class Item1ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = UIColor.green … Read more

How to detect if view controller is being popped of from the navigation controller?

You can detect whether a view is being popped using the isMovingFromParentViewController property for a view controller as shown below: – (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; if ([self isMovingFromParentViewController]) { NSLog(@”View controller was popped”); } else { NSLog(@”New view controller was pushed”); } } isMovingFromParentViewController Returns a Boolean value that indicates that the view controller is … Read more

viewController custom init method with storyboard

The designated initializer for view controllers in storyboards is -initWithCoder:. Since most view controllers from a storyboard are created via segues, you usually see state set during -prepareForSegue:sender:. If you’re instantiating a view controller directly from the storyboard like in the example you provided, then the pattern you’ve selected is appropriate.

Can I use autolayout to provide different constraints for landscape and portrait orientations?

Edit: Using the new concept of Size Classes introduced in Xcode 6, you can easily setup different constraints for specific size classes in Interface Builder. Most devices (e.g. all current iPhones) have a Compact vertical size class in landscape mode. This is a much better concept for general layout decisions than determining the device’s orientation. … Read more

xcode storyboard Container View – How do I access the viewcontroller

There is another solution by specifying an identifier for the embed segue(s) and retrieve the corresponding view controllers in method prepareForSegue: The advantage of this way is that you needn’t rely on a specific order in which your child view controllers are added due to the fact that each child view controller is embedded via … Read more