使用autolayout时,自定义UIView不适合其脚本容器

使用autolayout时,自定义UIView不适合其脚本容器,uiview,storyboard,custom-controls,autolayout,Uiview,Storyboard,Custom Controls,Autolayout,如何使自定义nib加载的UIView/widget适合故事板中为其提供的容器?也就是说,如何使其准确地占用指定的空间,而不是更多或更少?在这种情况下,最可能忘记的是在加载nib并将其添加到自定义视图/小部件的子视图的上下文中添加以下代码: UIView* rootView = [[[NSBundle mainBundle] loadNibNamed:className owner:self options:nil] firstObject]; rootVi

如何使自定义nib加载的UIView/widget适合故事板中为其提供的容器?也就是说,如何使其准确地占用指定的空间,而不是更多或更少?

在这种情况下,最可能忘记的是在加载nib并将其添加到自定义视图/小部件的子视图的上下文中添加以下代码:

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.
(其中self是相关的自定义uiview/小部件实例。)