简体   繁体   English

旋转后在iPad上更改背景图片

[英]Change background image on ipad after rotation

I show bg of my app using UIImageView . 我使用UIImageView显示应用程序的背景色。 To support correct interface rotation i write layoutSubviews method. 为了支持正确的接口旋转,我编写layoutSubviews方法。 In this metod .frame property changes correctly, but when i try change image of UIImageView just like this: 在此方法中,.frame属性正确更改,但是当我尝试更改UIImageView图像时,如下所示:

view = [self viewWithTag:1002];    
 ((UIImageView *)view).image = [UIImage imageNamed:@"portrait.png"];

nothing changed. 没有改变。 Did I do something wrong or is it impossible to change image of UIImageView in layoutSubviews method? 我做错了什么还是无法在layoutSubviews方法中更改UIImageView图像?

try this 尝试这个

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {

    // change background here

    }

You manage the rotation of your views by using a UIViewController (be sure to check out the docs here: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html ). 您可以使用UIViewController管理视图的旋转(请确保在此处签出文档: http : //developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html ) 。 If you implemented a UIViewController for your UIImageView you could set the image in the viewDidLoad: method. 如果为UIImageView实现了UIViewController ,则可以在viewDidLoad:方法中设置图像。

You shouldn't override the layoutSubviews: method to perform this action. 您不应覆盖layoutSubviews:方法来执行此操作。 layoutSubviews: is to do what it says in the name - it should perform the positioning and sizing of any subiews that this view currently has. layoutSubviews:按照名称中的说明进行操作-它应该执行该视图当前具有的所有子子视图的定位和大小调整。 You shouldn't need to override this method unless you wanted to do some special custom laying out of the subviews and is called in the run loop when the UIView is set to "requires resizing". 除非您想对子视图进行一些特殊的自定义布局,并且在UIView设置为“需要调整大小”时在运行循环中被调用,否则您不需要重写此方法。

I strongly suggest you set your image from from some other location in the code. 我强烈建议您从代码中的其他位置设置图像。 If you want the image to change when the view is rotated, you could set the image in the - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation method of a UIViewController subclass. 如果要在旋转视图时更改图像,则可以在UIViewController子类的- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation方法中设置图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM