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.
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?
You need to uncheck 'Use size classes' in your xib
and the view frame size will be the one you set and not 600x600
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With