2

我使用UIImageView. 为了支持正确的界面旋转,我编写了layoutSubviews方法。在这个方法中 .frame 属性正确更改,但是当我尝试更改图像时UIImageView

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

没有改变。我做错了什么还是无法更改方法UIImageView中的图像layoutSubviews

4

2 回答 2

4

试试这个

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {

    // change background here

    }
于 2011-03-07T08:57:43.317 回答
1

您可以使用UIViewController管理视图的旋转(请务必查看此处的文档:http: //developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html) . 如果您为UIImageView实现了UIViewController ,则可以在方法中设置图像。viewDidLoad:

您不应重写该layoutSubviews:方法来执行此操作。layoutSubviews:就是按照名字所说的去做——它应该执行这个视图当前拥有的任何子视图的定位和大小。除非您想对子视图进行一些特殊的自定义布局并在UIView设置为“需要调整大小”时在运行循环中调用,否则您不需要重写此方法。

我强烈建议您从代码中的其他位置设置图像。如果您希望在视图旋转时更改图像,您可以在UIViewController- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation子类的方法中设置图像。

于 2011-03-07T09:02:03.483 回答