Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIView in XIB using Autolayout is always 600x600

I created a separate .xib because I wanted to design a UIView with autolayout outside of a viewController. I created the .xib, added the UIView and the constraints, using wCompact hRegular. Simple.

enter image description here

Then add it to my viewController in viewDidLoad:

UIView *header = [[[NSBundle mainBundle] loadNibNamed:@"HeaderSearch" owner:self options:nil] lastObject];
NSLog(@"%@", NSStringFromCGRect(header.frame));
[self.view addSubview:header];

But, when it is added, the frame size is 600x600 and I cannot figure out why.

What have I done wrong here that is forcing this strange size?

like image 993
Nic Hubbard Avatar asked Jan 12 '15 08:01

Nic Hubbard


2 Answers

You need to uncheck 'Use size classes' in your xib

uncheck size classes

and the view frame size will be the one you set and not 600x600

like image 176
nano Avatar answered Oct 24 '22 09:10

nano


Please refer to this.

header.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:header];

NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:header attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0];
[self.view addConstraint:widthConstraint];

NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:header attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:130.0];
[self.view addConstraint:widthConstraint];
[self.view addConstraint:heightConstraint];
like image 38
gabbler Avatar answered Oct 24 '22 07:10

gabbler