在方向更改时更改 UIView

Posted

技术标签:

【中文标题】在方向更改时更改 UIView【英文标题】:Changing UIView on orientation change 【发布时间】:2010-04-29 03:11:21 【问题描述】:

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

谁能指出我正确的方向?我假设这将是我在 viewDidLoad 上做的事情。

【问题讨论】:

***.com/questions/2489845/…的可能重复 【参考方案1】:

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

 #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 感谢您的建议.. 更改了答案。 :)【参考方案2】:

我实际上想出了一个非常简单的替代方法。由于我只是在更改背景图像,因此添加此..

`

- (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 的背景。唯一的缺点是,当前的背景图像在 Interface builder 中不可见,因为它是通过代码处理的。

【讨论】:

【参考方案3】:

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"]];

再次感谢马杜普

【讨论】:

【参考方案4】:

您可以通过查看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")
    
  

【讨论】:

以上是关于在方向更改时更改 UIView的主要内容,如果未能解决你的问题,请参考以下文章

将 CALayer 添加到 UIView 并能够在方向更改时重绘

在方向更改时自动缩放 UIView 以按比例缩放以适合父视图

更改设备方向时重绘自定义 uiview

在运行时和方向更改期间居中和 sizeToFit 子视图

更改 iPhone 中的方向时如何更改视图控制器的视图?

当我们在运行时更改模拟器方向时,UIViews 重叠