Getting restrictions using loadNibNamed and Size classes

I am loading a view from xib using the Autolayout and Size classes. Inside this view there is a subview, viewWithSizeClasses , with a restriction on its height, which depends on the size class.

What I'm trying to do is load the constraints right after loadNibNamed to get the desired height according to the current size class.

I tried various combinations of layoutSubviews() , updateConstraints() , but no matter what I do, I always get Default Any, Any height.

 override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) let xibView = NSBundle.mainBundle().loadNibNamed("View", owner: self, options: nil).first as View self.view.addSubview(xibView) height = xibView.viewWithSizeClasses.frame.size.height // <- Any, Any height } 

I am deploying on iOS8 or later.

+5
source share
1 answer

I ran into the same problem trying to use the same xib with separate limitations for iPhone vs iPad. My solution was to create a view for each size class within the same xib. Then, when loading xib, you can access the corresponding view by index. In my case, my Any Any Any class has index 0 and Regular-Regular at index 1.

 NSArray *views = [NSBundle.mainBundle loadNibNamed:NSStringFromClass(self.class) owner:self options:nil]; UIView *view = views[IS_IPAD]; 

I am sure you can translate this to Swift. I also defined a global macro

 #define IS_IPAD (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad) 
0
source

Source: https://habr.com/ru/post/1209464/


All Articles