3

I have a UIViewController with a custom UINavigationBar (inherited from it's superclass).

In this controller I am instantiating another UIViewController and adding it's view as a subview in my view like this:

// I believe this should be 'as LocationPermissionsInfoViewController' but it crashed badly that way. Any ideas?
var permissionsView = UIViewController(nibName:"LocationPermissionsInfoView", bundle: nil) as UIViewController

    self.view.addSubview(permissionsView.view)
    permissionsView.view.frame = self.view.bounds

The LocationPermissionsInfoView is designed in IB with various autolayout constraints. However, I want a 'contentView' that exists in that view to respect the Top Layout Guide (the bottom of the custom navigation bar to be precise) of the view it's added on as a subview. Right now, IB obviously only allows me to set up constraints in relation to the top level view in the interface.

Any ideas on how I can achieve this? Ideally from IB or somewhere in LocationPermissionsInfoView controller.

narner
  • 2,908
  • 3
  • 26
  • 63
Thanos
  • 844
  • 2
  • 11
  • 27

2 Answers2

5

Try one of the following:

  • Use self.edgesForExtendedLayout = UIRectEdgeNone; in your view
    controller's -viewDidLoad method. This will translate the origin.y to the bottom of the nav bar.

  • Add a constraint in your view controller that makes the LocationPermissionsInfoView's view to respect it's topLayoutGuide. Since you are adding the view programmatically, the constraint would need to be added programmatically as well.

  • In LocationPermissionsInfoView add a constraint that forces it to abide by the topLayoutGuide of it's superview. Again, this will be in code (probably in -didMoveToSuperView method). Make sure to assign this constraint to LocationPermissionsInfoView's view.superview otherwise you will get errors about view hierarchy.

Essentially, the last 2 options are pretty much doing the same thing with the same constraint and only differ in when/where you add this constraint. If you are using the LocationPermissionsInfoView in multiple places through out the app, it would be good to use the last option.

Numan Tariq
  • 1,296
  • 1
  • 9
  • 18
0

Have you tried just adding another top-level UIView in IB? This way all your XIB constraints are in relation to that view instead. So when you add that subview into your main VC and set the bounds it should respect your constraints.

var permissionsView : LocationPermissionsInfoView = UIViewController(nibName:"LocationPermissionsInfoView", bundle: nil) as UIViewController

self.view.addSubview(permissionsView.viewMainContent)
permissionsView.viewMainContent.frame = self.view.bounds
Oren
  • 5,055
  • 3
  • 34
  • 52
  • What is `viewMainContent` ? – Thanos Feb 03 '15 at 19:10
  • It's a new UIView I'm proposing you add as a top-level view inside your XIB. All your controls should go in there and constraint against that instead. It sounds like the problem is you're losing all your constraints since you're changing parent views. This approach will allow you to keep all your lower-level constraints and since you are setting the frame for the parent view it should all work as expected – Oren Feb 03 '15 at 19:18