Martin Perry
Martin Perry

Reputation: 9527

iOS - set subview width to be the same as parent (objective-c)

I have a storybaord with a UIView that has a class set to MyView (this class is defined as @interface MyView : UIView).

In class MyView, I have this construction:

-(void)awakeFromNib{            

    [super awakeFromNib];

    [[UINib nibWithNibName:@"MyNib" bundle:nil] instantiateWithOwner:self options:nil];

    [self addSubview:self.contentView];
}    

It loads design from nib and add contentView to it. Now, how can I set contentView width to be the same as width of self? I have tried add constraint programatically under addSubview method, but app crashed.

So where should I add constraint and how the code should be?

Upvotes: 0

Views: 843

Answers (2)

Teja Nandamuri
Teja Nandamuri

Reputation: 11201

You don't have to set constraints to make the width equal.

Just set the constraints for the elements in the xib so that they will get aligned according to their superview frame.

And you can set the xib view frame in the awakeFromNib method.

Upvotes: 1

T.Ratzz
T.Ratzz

Reputation: 91

You can make outlet of your xib view and then can set set constraints to programatically view accordingly to its parent view. you might have get idea from this It should work.

Upvotes: 0

Related Questions