【问题标题】:Landscape views only loading properly from portrait and after rotating, why?横向视图只能从纵向和旋转后正确加载,为什么?
【发布时间】:2013-09-24 19:27:28
【问题描述】:

这里是新的 iOS 开发人员。我有多个视图,需要以纵向和横向显示不同的图像。我目前已经成功实现了这一点,并且纵向图像加载良好,并且在旋转时,横向图像也加载良好。但是,如果设备处于横向,然后切换到另一个视图,则加载不正确 - 错误的大小、分辨率、对齐方式等。我处理方向更改的代码如下:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
    {
        if((self.interfaceOrientation == UIDeviceOrientationLandscapeLeft) || (self.interfaceOrientation == UIDeviceOrientationLandscapeRight))
        {
            _image1.image = [UIImage imageNamed:@"Landscape.png"];
        }
        else if((self.interfaceOrientation == UIDeviceOrientationPortrait) || (self.interfaceOrientation == UIDeviceOrientationPortraitUpsideDown))
        {
            _image1.image = [UIImage imageNamed:@"Portrait.png"];
        }
}

我相信这是因为该方法仅在轮换时调用。例如,如果我旋转不正确的初始横向视图,它会再次显示正确的图像。当初始方向为横向时,有没有办法让该方法运行并加载正确的横向视图?还是一种强制显示正确图像的方法?非常感谢。

【问题讨论】:

    标签: uiimageview orientation ios7 landscape


    【解决方案1】:

    我终于通过添加方向检查器解决了这个问题。我在 .h 中添加了以下内容:

    @property (nonatomic, readonly) UIDeviceOrientation *orientation;
    

    然后我在 viewDidLoad 方法中将此添加到我的 .m 文件中:

    if(([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) || ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)) {
    _image1.image = [UIImage imageNamed:@"Landscape.png"];
    }
    

    这会检查初始方向是否为横向。如果是,它会加载我的 Landscape.png 图像。否则,由于默认图像是我在情节提要中设置的 Portrait.png,如果方向已经是纵向,则会加载该图像。干杯!

    编辑:不建议使用上述代码,因为使用它时可能会遇到问题,例如使用方向锁定的设备。我将其更改为检查状态栏的方向,而不是设备的方向,如下所示:

    if(([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft) || 
    ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight)) { 
    _image1.image = [UIImage imageNamed:@"Landscape.png"];
    }
    

    你不需要在.h中声明任何变量,只需在viewDidLoad方法中添加以上内容即可。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多