iPhone - UIViewController在设备方向改变时不旋转

Ryu*_*Ryu 14 iphone cocoa-touch

我有自己的自定义UIViewController,它包含一个带有UIImageView的UIScrollView作为它的子视图.我希望在设备方向改变时使图像自动旋转,但它似乎不起作用......

在头文件中,我有;

@interface MyViewController : UIViewController <UIScrollViewDelegate> {
    IBOutlet UIScrollView   *containerView;
    UIImageView *imageView;
}
Run Code Online (Sandbox Code Playgroud)

这些组件在loadView函数中初始化,如下所示;

    containerView = [[UIScrollView alloc] initWithFrame:frame];

    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://..."]];
    UIImage *image = [[UIImage alloc] initWithData:data];
    imageView = [[UIImageView alloc] initWithImage:image];
    [image release];

    [containerView addSubview:imageView];
Run Code Online (Sandbox Code Playgroud)

我添加了以下方法,假设我需要使视图自动旋转...

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}
Run Code Online (Sandbox Code Playgroud)

MyViewController加载了我指定从URL中获取的图像,并且当我翻转设备时,正在调用shouldAutorotate ...函数和正确的UIInterfaceOrientation.

但是,didRotateFromInterfaceOrientation方法没有被调用,并且图像似乎没有自行旋转...有人可以指出我需要添加什么,或者我在这里做错了什么?

提前致谢!

law*_*nce 26

这可能不适合你,因为你没有指定UIViewController的上下文,但我刚刚在Apple文档中发现了一个重要的问题,解释了我遇到的类似问题.

标签栏控制器默认支持纵向方向,除非所有根视图控制器都支持这种方向,否则不要旋转到横向方向.当发生设备方向更改时,选项卡栏控制器将查询其视图控制器数组. 如果其中任何一个不支持方向,则标签栏控制器不会更改其方向.


fra*_*yer 1

为了让这种事情在我的应用程序中工作,我必须覆盖

- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    [self layoutSubviews];
}
Run Code Online (Sandbox Code Playgroud)

还有layoutSubviews

- (void)layoutSubviews
{
    NSLog(@"layoutSubviews called");

    ...recalc rects etc based on the new self.view.bounds...
}
Run Code Online (Sandbox Code Playgroud)

我不确定这是否是绝对必需的,但它对我有用。