Note that there are some explanatory texts on larger screens.

plurals
  1. POIssues with AutoLayout with Custom Views defined in NIB file and used in StoryBoard
    text
    copied!<p>I've defined a custom view in a NIB file and would like to instantiate a copy in a StoryBoard but I'm having issues with autolayout.</p> <p>In a simple example, the custom view has single label with a fixed size and centered both vertically and horizontally, all using autolayout.</p> <p><img src="https://i.stack.imgur.com/mGR3h.png" alt="enter image description here"></p> <p>The file owner is set to my class, it has an outlet to the top view. In the custom view implementation I do:</p> <pre><code>- (id)initWithCoder:(NSCoder *)aDecoder { self = [super initWithCoder:aDecoder]; if(self) { [[NSBundle mainBundle] loadNibNamed:@"FMCompassView" owner:self options:nil]; [self addSubview:self.topView]; } return self; } </code></pre> <p>Now, in my storyboard, I add a UIView, set it's class to my custom class and layout it out sized and centered on my page, again using autolayout.</p> <p><img src="https://i.stack.imgur.com/idM2s.png" alt="enter image description here"></p> <p>And my widget is positioned and size properly but it's content is not resized as illustrated below: <img src="https://i.stack.imgur.com/5L1pb.png" alt="enter image description here"></p> <p>I tried adding more constraints after loading from the NIB, something along the lines of:</p> <pre><code>UIView* subV = self.topView; NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(subV); [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[subV]|" options:NSLayoutFormatAlignAllBaseline metrics:nil views:viewsDictionary]]; [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[subV]|" options:NSLayoutFormatAlignAllBaseline metrics:nil views:viewsDictionary]]; </code></pre> <p>But this causes invalid layout constraints.</p> <p>Any ideas on how to get this to work?</p> <p>Cheers!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload