【问题标题】:UIImageView does not follow device orientationUIImageView 不遵循设备方向
【发布时间】:2012-09-26 18:07:23
【问题描述】:

我有一个带有几个子视图的滚动视图。子视图定义如下:

CGRect screenRect = [[UIScreen mainScreen] bounds];
CGRect rect = imageView.frame;
rect.size.height = screenRect.size.height;
rect.size.width = screenRect.size.width;
imageView.frame = screenRect;
[scrollView addSubview:imageView];

在我将界面方向更改为横向之前,这可以正常工作。 imageView 仍处于纵向模式,将图像切成两半。我应该怎么做才能将纵向高度和宽度更改为横向高度和宽度?

【问题讨论】:

    标签: ios uiimageview uiinterfaceorientation


    【解决方案1】:

    在您的代码中添加这一行并检查:

    [imageView setAutoresizesSubviews:YES];
    [imageView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
    
    [imageView setAutoresizesSubviews:YES];
    [imageView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
    

    也检查一下link

    【讨论】:

    • 您可能还想将autoresizesSubviews = YES 设置为imageView 的父视图(上例中为scrollView)?
    【解决方案2】:

    一些想法:

    1. 我使用self.view.bounds,而不是[[UIScreen mainScreen] bounds]。这 (a) 将排除状态栏、导航栏等内容; (b) 旋转时,宽度和高度会自动更新以反映新的尺寸。

    2. 如果您希望为您调整图像视图的大小,通常将其autoresizingMask 设置为UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleWidth

    3. 您可能需要检查您的图像视图的contentMode 并将其设置为UIViewContentModeScaleAspectFit 之类的值,以便始终显示整个图像。这样,如果您有一张肖像图像,它将始终以纵向显示而不会失真,不会被截断。

    4. 如果您不喜欢旋转设备时图像视图本身已被调整为横向的事实,您可以在 iOS5 中的 viewWillLayoutSubviews 中重置其框架(在 iOS4 中,使用 @987654328 @)。

    【讨论】:

      【解决方案3】:

      你需要监听 UIDeviceOrientationDidChangeNotification

      在此处阅读更多相关信息: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIDevice_Class/Reference/UIDevice.html

      一旦检测到更改,请再次调用相同的代码。

      【讨论】:

      • 根据我的个人经验,除非您转换到新的视图控制器以使您的视图控制器层次结构与视图层次结构不同步(例如某些[self.view addSubview:newController.view] 愚蠢),否则不需要这样做。通常你可以只使用视图控制器的事件方法responding to rotation events
      猜你喜欢
      • 2017-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-09
      • 2015-03-01
      • 2013-02-27
      相关资源
      最近更新 更多