jdelman
jdelman

Reputation: 733

UIView's subviews rendering outside bounds

I have created a nib for a view in interface builder. Its subviews are placed with constraints such that they will scale with the size of the view's frame.

I use a helper method to initialize the view from its nib:

+ (TinyTestResultsView *)ttrView {
    TinyTestResultsView *ttrV = [[[NSBundle mainBundle] loadNibNamed:@"TinyTestResultsViewNib" owner:nil options:nil] firstObject];
    [ttrV commonInit];
    return ttrV;
}

During a view controller's viewDidLoad, I initialize the object using the ttrView method and add it as a subview to self.view. The VC then makes a call to the network to get some information, and then adds constraints as necessary to self.view and the subview to place it. It gets correctly placed & sized - however the view appears as a white box with no subviews. Why are the subviews being rendered outside the view?

If I use Debug View Hierarchy in Xcode and then try to find the subviews, I see that they are being rendered way outside the frame, and they are not complying with the autolayout rules from the NIB. I am using this exact same technique for a different custom UIView/nib, so I'm not sure what could be happening incorrectly.

Upvotes: 1

Views: 683

Answers (1)

curious
curious

Reputation: 666

It seems like you need to set translatesAutoresizingMaskIntoConstraints to NO for your view. When you load view from Nib file in such way this property is set to YES that means the system creates a set of constraints that duplicate the behavior specified by the view’s autoresizing mask. And this is not what you want when you use autolayout.

Upvotes: 1

Related Questions