ThR37
ThR37

Reputation: 4085

UIView loaded from xib as subview : How to set the frame/size properly?

I have a small problem with my iphone app (I am quite new to iphone programmation even if I had already done objective-c before).

I have an UIView named centerView in my main window. I have created a singleton subclass of UIViewController (let's call it FooViewController) linked with a xib file. My goal is to load the associated view as a subview (of centerView using the whole frame of centerView).

Problem : The view controlled by FooViewController does not use the frame of its superview.

I have tried to add :

But I can't get the good behavior (i.e. the good size). There are multiple questions related to this kind of problems but I was unable to find my answer in them so :

Thanks in advance for your help.

Upvotes: 0

Views: 2559

Answers (1)

Chaitanya Gupta
Chaitanya Gupta

Reputation: 4053

When loading from a NIB, a view is loaded in the view controller's loadView method, not in the initializer. Since you shouldn't really override loadView, esp. if you are loading it from a NIB, you should try doing #1 (self.view.frame = self.view.superview.frame) in the view controller's viewDidLoad method. Of course, you should ensure that the view is added to a superview by the time you execute this statement. You also need to make sure that the superview's frame has been correctly set by this time.

Another place where you can try this is in the viewWillAppear: method. Most probably your view's super view will have been set up properly by then.

Upvotes: 1

Related Questions