Frage

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?

War es hilfreich?

Lösung

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.)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top