当屏幕方向改变时如何更改UIView?

13

大家好。我有一个相当简单的问题。我正在开发一款“富有”iPad应用程序,并且我有两个专门为横向和纵向设计的背景图片。我希望这个ImageView能够根据设备的方向自动切换(就像苹果的大多数iPad应用程序一样)。

有人能够指点我吗?我假设这是在viewDidLoad中完成的某些操作。


可能是重复的问题http://stackoverflow.com/questions/2489845/rotate-uiviewcontroller-to-counteract-changes-in-uiinterfaceorientation/2490719#2490719 - Madhup Singh Yadav
4个回答

21

你可以做的最好的事情是根据你的界面方向更改子视图框架的框架。你可以这样做:

 #pragma mark -
 #pragma mark InterfaceOrientationMethods

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (UIInterfaceOrientationIsPortrait(interfaceOrientation) || UIInterfaceOrientationIsLandscape(interfaceOrientation));
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    if(UIInterfaceOrientationIsPortrait(toInterfaceOrientation)){
        //self.view = portraitView;
        [self changeTheViewToPortrait:YES andDuration:duration];

    }
    else if(UIInterfaceOrientationIsLandscape(toInterfaceOrientation)){
        //self.view = landscapeView;
        [self changeTheViewToPortrait:NO andDuration:duration];
    }
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------

- (void) changeTheViewToPortrait:(BOOL)portrait andDuration:(NSTimeInterval)duration{

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:duration];

    if(portrait){
        //change the view and subview frames for the portrait view
    }
    else{   
        //change the view and subview  frames for the landscape view
    }

    [UIView commitAnimations];
}

希望这个能帮到你。

UIInterfaceOrientationIsPortrait()UIInterfaceOrientationIsLandscape()对于这些情况的可读性确实有很大提高...但是,无论如何都是个好的解决方案! - Nate
@Nate 感谢你的建议。已经修改了答案。 :) - Madhup Singh Yadav

9
我实际上找到了一个非常简单的替代方法。由于我只是更改背景图片,所以添加这个...
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation duration:(NSTimeInterval)duration {
    if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation ==
        UIInterfaceOrientationPortraitUpsideDown) { 
        [brownBackground setImage:[UIImage imageNamed:@"Portrait_Background.png"]];
    } else {
        [brownBackground setImage:[UIImage imageNamed:@"Landscape_Background.png"]];
    }
}

根据方向改变已声明的UIImageView的背景。唯一的缺点是,当前的背景图像在界面构建器中不可见,因为它是通过代码处理的。


7

对于Madhup的方法,这里有一个小修改,很棒。我发现需要在viewDidLoad中添加以下内容,以设置竖屏或横屏的初始背景图片:

// set background image
if (self.interfaceOrientation == UIInterfaceOrientationPortrait || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"portraitBG.png"]];
} else {
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"landscapeBG.png"]];
}

再次感谢Madhup。

1

如果您正在编写一个小型的自我感知控件,可以通过监视 bounds.width > bounds.height 来完全封装它在您的 UIView 中。

class MyView: UIView {
  override func layoutSubviews() {
    super.layoutSubviews()
    if bounds.height > bounds.width {
      println("PORTRAIT. some bounds-impacting event happened")
    } else {
      println("LANDSCAPE")
    }
  }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接