iOS - 使用自动布局在旋转时更改背景图像

标签 ios ipad autolayout

我是 iOS 开发的新手,刚刚继承了一堆 iOS 代码。

我正在尝试开始使用自动布局和约束,以便更轻松地支持不同的屏幕尺寸和 iOS 版本。我发现一旦我将 Storyboard更改为使用自动布局,当我将 iPad 旋转到横向 View 时背景图像不再改变。我不确定这是否是阻止此更改的自动布局,或者是否有其他方法可以切换背景图像。我很感激你的帮助。

这是我的旋转方法。

- (void)didRotate
{
    UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;

    if ([[UIDevice currentDevice] userInterfaceIdiom] != UIUserInterfaceIdiomPhone)
    {
        if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight)
        {
            [backgrndView setBackgroundImage:[UIImage imageNamed:@"menu_ipad2.png"] forState:UIControlStateNormal] ;
        }
        else if (orientation == UIDeviceOrientationPortraitUpsideDown|| orientation == UIDeviceOrientationPortrait)
        {
            [backgrndView setBackgroundImage:[UIImage imageNamed:@"menu_ipad.png"] forState:UIControlStateNormal] ;
        }

        [backgrndView sizeToFit];
    }
}

最佳答案

向基础 View (这是一个按钮)添加新的值为零的左、右和上约束解决了这个问题。

关于iOS - 使用自动布局在旋转时更改背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23568862/

相关文章:

ios - 添加导航 Controller 可防止在 Root View Controller 中调用 updateViewConstraints

ios - 提交/验证需要 Info.plist 的应用内购买

ios - Xcode 自定义字体未显示在应用程序中

iphone - iPad 应用程序 UITableView 委托(delegate)方法没有被调用

ios - 自动布局 View 层次结构

ios - 如何将边缘设置为零?

ios - 努力将 View Controller 推到UITabBarController

iphone - 重用带有 subview 的单元格(以编程方式)

iphone - 将 HTML <Tag> 解析为 ios

iPad UIActionSheet 点击外部即可关闭 - UIActionSheet 的状态?