【问题标题】:UIImageView Background Image and Rotation HelpUIImageView 背景图像和旋转帮助
【发布时间】:2011-05-02 11:06:51
【问题描述】:

在我的 iPad 应用程序中,我目前有一个 1024x1024 像素的背景图像 png,并使用以下设置它

UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background-image"]];
[self.view addSubview:imgView];
[self.view sendSubviewToBack:imgView];
[imgView release];

这可以很好地添加 UIImageView,并且视图在横向居中,但在纵向中偏离中心。

我已经尝试过一些 contentMode 设置,但没有任何运气,我要么将纵向设置为中心,要么将横向设置为中心,但不能同时设置。

有人可以帮我在旋转后让 UIImageView 在纵向和横向中居中。

提前致谢 马特

【问题讨论】:

    标签: iphone objective-c ios uiimageview uiinterfaceorientation


    【解决方案1】:

    您需要设置contentModeframe 和适当的自动调整大小掩码:

    UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background-image"]];
    imgView.frame = self.view.bounds;
    imgView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    imgView.contentMode = UIViewContentModeScaleAspectFill;
    [self.view addSubview:imgView];
    [self.view sendSubviewToBack:imgView];
    [imgView release];
    

    【讨论】:

    • 完美...不敢相信我错过了 autoresizingMask,我试图将 UIViewAutoresizingFlexibleWidth 应用于 contentMode。在过去的一天左右,这一直让我发疯。在我的应用程序的所有视图后面显示此 UIImageView 的最佳做法是什么?目前,我已将所有视图背景颜色设置为 clearColor,并将上述代码放在导航控制器上的 viewWillAppear 中。非常感谢 omz
    • 将图像视图添加到主窗口并使所有控制器透明。但自动调整大小不适用于此。您需要手动旋转窗口
    • @rahul 你有关于如何手动旋转窗口的代码示例吗?这被认为比我所做的更好吗?因为导航控制器将始终可用并且它负责旋转。
    • @Matt Price 在谷歌上找到如何设置状态栏的方向。通过旋转状态栏窗口会旋转。
    【解决方案2】:

    检查autoresizingMask 属性中的imageView

    【讨论】:

    • 感谢 7kv7,omz 给出了更详细的答案和代码示例,但非常感谢您的回复。 autoresizingMask 是关键。
    • @Matt 当然可以。快乐编码:)
    猜你喜欢
    • 1970-01-01
    • 2013-01-15
    • 1970-01-01
    • 1970-01-01
    • 2015-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多