I’m trying to create a view controller hierarchy programmatically (without IB and Storyboard). Things work well if I use a plain UIViewController
:
// My app delegate
func application(application: UIApplication, didFinishLaunchingWithOptions options: [NSObject: AnyObject]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let vc = UIViewController(nibName: nil, bundle: nil)
vc.view = UIView(frame: CGRectZero)
vc.view.backgroundColor = UIColor.whiteColor()
let label = UILabel(frame: CGRectMake(0, 0, 100, 40))
label.text = "Hello!"
vc.view.addSubview(label)
vc.title = "Title"
self.window.rootViewController = vc
self.window.makeKeyAndVisible()
return true
}
Then I try to put the view controller inside a navigation controller:
func application(application: UIApplication, didFinishLaunchingWithOptions options: [NSObject: AnyObject]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
// vc created the same way as above.
let nav = UINavigationController(rootViewController: vc)
self.window.rootViewController = nav
self.window.makeKeyAndVisible()
return true
}
And the label is hidden under the navigation bar.
So obviously the view isn’t resized properly by the navigation view controller. I tried setting the frame of the inner vc, but that does not have an effect, probably because things are handled by autolayout instead? Should I add some constraints to make this work properly like in IB/Storyboard, or should I do some things differently?