Question

How do you make a custom nib-loaded UIView/widget to fit into the container provided for it in storyboard? I.e, how do you make it to take up exactly the space specified, not more and not less?

Was it helpful?

Solution

What most likely is forgotten in this case is to add the following code in the context of loading the nib and adding it to the custom view's/widget's subviews:

UIView* rootView = [[[NSBundle mainBundle]
                    loadNibNamed:className owner:self options:nil] firstObject];
rootView.translatesAutoresizingMaskIntoConstraints = NO;

[self addConstraint:
 [NSLayoutConstraint
  constraintWithItem:rootView attribute:NSLayoutAttributeTop
  relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop
  multiplier:1.0f constant:0.0f]];

// And so on for all attributes (Bottom, Left, Right) that you want to
// constrain the loaded nib with.

(Where self is the custom uiview/widget instance in question.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top