Reputation: 5955
My application's structure looks like:
UIViewController1
- UIViewController2
- Add UIView programmatically
UIViewController1 contains UIViewController2, and I add some UIView, loading from Xib, to the UIViewController2 with the following code:
SessionView* b = [[[NSBundle mainBundle] loadNibNamed:@"SessionView" owner:self options:nil] lastObject]; [self.view addSubview:b]; //- set Frame before or after addSubView, there is no difference CGRect frame = b.frame; frame.origin.x = 0; frame.origin.y = 0; b.frame = frame;
The original UIView looks like this:
After adding to the UIViewController, it looks like this:
With the following code, when I assign the width and height of the UIView equal to a part of the width and height of the main view, it looks good
SessionView* b = [[[NSBundle mainBundle] loadNibNamed:@"SessionView" owner:self options:nil] lastObject]; [self.view addSubview:b]; CGRect frame = b.frame; frame.origin.x = 0; frame.origin.y = 0; //------- Assign the Width and Height Value //------------------------------------------- frame.size.width = CGRectGetWidth(self.view.bounds) / 3; frame.size.height = CGRectGetHeight(self.view.bounds) / 3; b.frame = frame;
Upvotes: 0
Views: 203
Reputation: 13600
It seems you are adding viewController
directly as subview, instead you should be adding view
of viewController
as subview.
SessionView* b = [[[NSBundle mainBundle] loadNibNamed:@"SessionView" owner:self options:nil] lastObject];
[self.view addSubview:b.view];
Upvotes: 0