hoya21 hoya21 - 6 months ago 75
iOS Question

View bounds dont update after changing orientation IOS

I have an issue with my app when I change orientation into a viewController. If I get into this view controller in portrait mode (320 width) and change to landscape then it seems that only 320 width of landscape mode is active and the rest screen seems to be dead. I tried to update view.Frame when the orientation changes but it didn't work. Can anyone help me with this?

- (void)updateLayoutForNewOrientation:(UIInterfaceOrientation)orientation{
if (self.interfaceOrientation == UIInterfaceOrientationPortrait || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
// Portrait

self.view.frame=CGRectMake(0,0,320,500);
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"smallerView2.png"]];

self.thumbScrollView.frame = CGRectMake(20,100, 280, 72);

self.rightThumbArrow.frame=CGRectMake(1,107, self.rightThumbArrow.bounds.size.width, self.rightThumbArrow.bounds.size.height);

self.leftThumbArrow.frame=CGRectMake(self.view.bounds.size.width-30,107, self.leftThumbArrow.bounds.size.width, self.leftThumbArrow.bounds.size.height);

self.bigImageView.frame=CGRectMake(20,200, 280, 200);

self.previousPicBar.frame=CGRectMake(19,200, 10, 198);

self.nextPicBar.frame=CGRectMake(291,200, 10, 198);

self.leftArrowBtn.frame=CGRectMake(20,294, 8, 10);

self.rightArrowBtn.frame=CGRectMake(292,294, 8, 10);



}

else
{
// Landscape



self.view.frame=CGRectMake(0, 0, 500, 320);

NSLog(@"%f %f %f %f",self.view.bounds.origin.x, self.view.bounds.origin.y, self.view.bounds.size.width, self.view.bounds.size.height);

self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"smallerView2Landscape.png"]];

self.thumbScrollView.frame = CGRectMake(30,60, 520, 60);

self.rightThumbArrow.frame=CGRectMake(10,65, self.rightThumbArrow.bounds.size.width, self.rightThumbArrow.bounds.size.height);

self.leftThumbArrow.frame=CGRectMake(self.view.bounds.size.width-45,65, self.leftThumbArrow.bounds.size.width, self.leftThumbArrow.bounds.size.height);

self.bigImageView.frame=CGRectMake(120,120, 350, 140);

self.previousPicBar.frame=CGRectMake(119,122, 10, 136);

self.nextPicBar.frame=CGRectMake(461,122, 10, 136);

self.leftArrowBtn.frame=CGRectMake(120,180, 8, 10);

self.rightArrowBtn.frame=CGRectMake(462,180, 8, 10);


}
}

- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];

[self updateLayoutForNewOrientation:self.interfaceOrientation];
}

- (void)willAnimateRotationToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration{
[self updateLayoutForNewOrientation:self.interfaceOrientation];
}

Answer

There is mismatch in setting and getting view coordinate. you are setting frame in - (void)updateLayoutForNewOrientation:(UIInterfaceOrientation)orientation and in NSLog you are printing using bound.

Correct way to get width is following

NSLog(@"x= %f y= %f w= %f h=%f",self.view.frame.origin.x,
self.view.frame.origin.y, self.view.frame.size.width,
self.view.frame.size.height);

your code

NSLog(@"%f %f %f %f",self.view.bounds.origin.x,
self.view.bounds.origin.y, self.view.bounds.size.width,
self.view.bounds.size.height);

Change bound with frame or viceversa.