更改方向IOS后,视图边界不会更新

时间:2013-11-20 11:04:19

标签: ios objective-c uiviewcontroller orientation

当我将方向更改为viewController时,我的应用程序出现问题。如果我以纵向模式(320宽度)进入此视图控制器并更改为横向,则似乎只有320宽度的横向模式处于活动状态且其余屏幕似乎已死亡。我尝试更新view.Frame,当方向更改但它不起作用。谁能帮我这个?

- (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];
}

2 个答案:

答案 0 :(得分:0)

设置和获取视图坐标不匹配。您正在- (void)updateLayoutForNewOrientation:(UIInterfaceOrientation)orientation中设置框架,在NSLog中使用bound进行打印。

正确的获取宽度的方法是

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);

你的代码

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);

更改框架或反之亦然。

答案 1 :(得分:0)

回答我的用户说得对。我不得不改变设置x,y,宽度,高度的方式。

更改

self.view.frame=CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, self.view.bounds.size.width, self.view.bounds
                                   .size.height);

使用

self.view.frame=CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame
                                   .size.height);

并添加

[self.view setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];

在viewDidLoad方法中取得了不同。