Mrug
Mrug

Reputation: 5023

Subviewing an XIB having Auto-layout

I have created an XIB and used AutoLayout. Its showing perfectly if i check it in Preview Pan. enter image description here

Now I tried to load that NIB and then add as a subview into another ViewController using following code:

myNibView = [[NSBundle mainBundle]loadNibNamed:@"CustomNib" owner:self options:nil].lastObject;
myNibView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view myNibView];

This time it is scattering all the views as shown in the image below. enter image description here

I enable logging and checked the frame of SubView, but the frame of XIB(SubView) get changed as per the View of UIViewController but Subviews of XIB didn't arranged with that.

2015-03-21 14:38:13.527 [3015:77723] newSubview.frame before = {{0, 0}, {420, 613}}
2015-03-21 14:38:13.633 [3015:77723] containerView.bounds after = {{0, 0}, {320, 504}}
2015-03-21 14:38:13.634 [3015:77723] newSubview.frame after = {{0, 0}, {320, 504}}

Is there any idea, how to Add an XIB as subview into another with retaining its Auto layout constraints to work properly as it was there in XIB ?

Upvotes: 0

Views: 105

Answers (2)

Mrug
Mrug

Reputation: 5023

I solved this issue by moving all the Subviews of XIB along with ScrollView into another UIView.

The heirarchy was like,

XIB
--UIScrollView
----UIView 1
----UIView 2
----UIView 3
.
.

But Now I changed it as below,

XIB
--UIView
----UIScrollView
------UIView 1
------UIView 4
------UIView 3
.
.
.

And it worked.

Upvotes: 0

Dheeraj Kumar
Dheeraj Kumar

Reputation: 441

do not use myNibView.translatesAutoresizingMaskIntoConstraints = NO; because it remove all constrains into the view

Upvotes: 1

Related Questions