Load XIB into a UIView

2019-07-14 14:20发布

问题:

I have a UIView that is not covering the entire screen of an iPhone... about 200 pixels by 300 pixels.

I'm trying to load a xib into this view, but it seems to have an issue. The code works, but the UIView takes over everything at fullscreen... not at the determined width of the UIView.

Here is the code I have:

incViewPopUP = [[[NSBundle mainBundle] loadNibNamed:@"IncidentsViewController" owner:self options:nil] objectAtIndex:0];

I also tried adding it as a subview like below.. but that also does not work.

UIView *myView = [[[NSBundle mainBundle] loadNibNamed:@"IncidentsViewController" owner:self options:nil] objectAtIndex:0];
[incViewPopUP addSubView:myView];

Thanks for the help!

回答1:

Adam, try this:

MyViewController *myViewController=[MyViewController alloc] initWithNib:@”IncidentsViewController”];

[myView addSubView:myViewController.view];

[myViewController release];

Just make sure after that the XIB has the right size for your UIView.



回答2:

Did you try setting the frame size of myview programmatically ? Set myView.frame = CGRectMake (0,0,320,420) ? Or you can give your favorite value for that frame :)



回答3:

You may need to remove all dummy components such as statusbar and other bars in IB from the view. From that point you can edit the size of the view within IB.