如何更新UIImageView的contentMode?

时间:2010-06-03 10:58:59

标签: iphone uiimageview

在最初的UIImageView我已将其设置为

mCurrentImageView.contentMode = UIViewContentModeLeft;

因此,如果设备旋转,我将更改contentMode 但它没有效果。我该如何解决?

if ([UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeLeft || [UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeRight) {
        mCurrentImageView.contentMode = UIViewContentModeScaleAspectFit;
    }

1 个答案:

答案 0 :(得分:3)

[UIDevice orientation]不会返回这些标识符以进行定位。从文档中,该消息将返回以下内容:

typedef enum {
   UIDeviceOrientationUnknown,
   UIDeviceOrientationPortrait,
   UIDeviceOrientationPortraitUpsideDown,
   UIDeviceOrientationLandscapeLeft,
   UIDeviceOrientationLandscapeRight,
   UIDeviceOrientationFaceUp,
   UIDeviceOrientationFaceDown
} UIDeviceOrientation;

要更改内容模式,您应该在willRotate消息的视图控制器代码中执行某些操作:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        mCurrentImageView.contentMode = UIViewContentModeScaleAspectFit;
    }
}