Domanda

Using a view controller class with an XIB. Can do auto layout fine with all views except the top / root view. Can not Pin, etc. This view will be used in another XIB, etc.

How to allow / set auto layout pin in a root view of a view controller class's root view?

View is set to Freeform / Portrait / none / none / one / scale to fill

È stato utile?

Soluzione

The view controller (more specifically in this case, the view controller's view) in the xib file is simply a self-contained representation of a view hierarchy. In the specific instance of a nib, the root view controller's view has no concept of a superview at this point in time until it is loaded into the app at a particular point in your application (when it is added as a subview). In general, a view controller view's size can be specified in one of a couple of ways (with auto-layout):

  1. By letting the contents of the view dictate the specific size that it can be. Basically, the content will determine how large (or small) your view controller's view will be. To do this, constrain every subview inside of the view controller's view to be fully defined against your view controller's view. Required constraints on subviews against it's parent force the parent to conform to the subviews.

    A good way to visualize this is to have a view contain a single UILabel subview. If you constrain the label to have a required 5pt margin all the way around, your container view will grow to change size depending on the content of the text. Think of this option as making your subview's constraints the "master" constraints for your view controller's size.

    The only downside to this approach is that the superview of your view controller's view will no longer have any control over the size of this view. You are more likely to run into an "over-constrained" state ("cannot satisfy constraints" exception) when attempting to attach constraints to views like this, especially if there are more "required" constraints.

  2. The superview of your view controller's view will specify the size and position of the view controller's view in its own coordinate system. Your subviews inside of your view controller's view will now have to be flexible enough to react to the dynamic size of your parent view unless you know some assumptions about how large or small this view will be.

    Think of this option as making your view controller view's size the "master" for the subview's constraints in that your subview's constraints will have to be more tolerant to size changes.

Storyboards

If you are using a storyboard, you can add a "container view" from the object library to a view controller's view and add constraints to the container view. If you are using a bare xib, there is no way to add constraints to a bare view controller's view in a nib file since it has no concept of a superview yet. In this instance, you will have to setup constraints in code in the view that will be managing your new view controller's view (or use a storyboard for everything). As far as I understand, you cannot use a pre-constructed xib within a storyboard.

Child View Controller in Storyboard

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top