timpone
timpone

Reputation: 19969

sizing of contained view controller in iOS clipping 91 pixels

I am experimenting with a contained view controller but am having problems with even the simplest form. Basically, it is cutting off the bottom 90 pixels and I am not sure why.

I have included my code and a screenshot with a similarly sized UIView next to it. This 90 pixel clipping consistent across different sized (ie if the height is made 200px, then it shows as 110 pixels). Any help would be appreciated as this has become very frustrating. Why is it being sized at this size and how do I fix?

thx in advance

- (void)viewDidLoad
{
  // this is the left one below that is the problem
   [super viewDidLoad];
   self.myView=[[JtViewController alloc] initWithNibName:@"JtViewController" bundle:nil];
   self.myView.view.frame=CGRectMake(10.0f,10.0f,100.0f,100.0f);
   self.myView.view.backgroundColor=[UIColor orangeColor];
   [self addChildViewController:self.myView];                 // 1
   [self.view addSubview:self.myView.view];
   [self.myView didMoveToParentViewController:self.myView];          // 3


   // this one is ok
   self.testView=[[UIView alloc] init];
   self.testView.frame=CGRectMake(150.0f,10.0f,100.0f,100.0f);
   self.testView.backgroundColor=[UIColor whiteColor];
   [self.view addSubview:self.testView];

enter image description here

edit 1

I've updated the code so the first 4 comments are effectively irrelevant

edit 2

@implementation JtViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Upvotes: 0

Views: 194

Answers (2)

Mark Kryzhanouski
Mark Kryzhanouski

Reputation: 7241

This is the common mistake to setup subview's bounds in - (void)viewDidLoad

- (void)viewDidLoad; This is an exceptionally good place to put a lot of setup code. But be careful because the geometry of your view (its bounds) is not set yet. If you need to initialize something based on the geometry of the view, use the - (void)viewWillAppear:(BOOL)animated; method

- (void)viewWillAppear:(BOOL)animated; When this is called, your bounds has been set (via your frame by your superview or some such). Your view will probably only get “loaded” once, but it might appear and disappear a lot. So don’t put something in this method that really wants to be in viewDidLoad. Otherwise, you might be doing something over and over unnecessarily. Use this to optimize performance by waiting until this method (i.e. just before view appears) to kick off an expensive operation (might have to put up a spinning “loading” icon though).

For better understanding please refer to Stanford lecture and Apple doc Responding to Display-Related Notifications

So just move setting subview frame to - (void)viewWillAppear:(BOOL)animated; method

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.myView.view.frame=CGRectMake(10.0f,10.0f,100.0f,100.0f);
}

P.S I've noticed another mistake (maybe it just typo) in line:

[self.myView didMoveToParentViewController:self.myView];

Should be:

[self.myView didMoveToParentViewController:self];

Upvotes: 2

timpone
timpone

Reputation: 19969

I was using Interface Builder.

So the fix seemed to be to delete the window from the contained ViewController and just add a UIView in and set that to the File's Owner's view. Probably learned to never worry about UIWindow but in this case, you have to worry about UIWindow.

Upvotes: 1

Related Questions