启用页面并更改设备旋转/方向的UIScrollView(MADNESS)

jbr*_*nan 18 cocoa-touch objective-c uiscrollview uiinterfaceorientation ios

我很难做到这一点.

我有一个UIScrollView,启用了分页.它由视图控制器(MainViewController)管理,每个页面由PageViewController管理,其视图作为scrollView的子视图添加到适当的偏移量.滚动是左右,适用于标准方向的iPhone应用程序.效果很好.基本上就像Apple提供的样本一样,也像iPhone提供的Weather应用程序一样.

但是,当我试图支持其他方向时,事情并不能很好地发挥作用.我用这种方法支持MainViewController和PageViewController中的每个方向:

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

然而,当我旋转设备时,我的页面变得非常歪斜,并且有很多绘图故障,特别是如果只加载了一些页面,然后我旋转,然后滚动更多等等...非常凌乱.

我告诉我的观点是支持自动调整大小

 theView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
Run Code Online (Sandbox Code Playgroud)

但无济于事.它似乎只会拉伸和歪曲我的观点.

在我的MainViewController中,我添加了这一行,试图调整我所有页面的视图:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * ([self.viewControllers count]), self.scrollView.frame.size.height);

    for (int i = 0; i < [self.viewControllers count]; i++) {
        PageViewController *controller = [self.viewControllers objectAtIndex:i];
        if ((NSNull *)controller == [NSNull null])
            continue;

        NSLog(@"Changing frame: %d", i);
        CGRect frame = self.scrollView.frame;
        frame.origin.x = frame.size.width * i;
        frame.origin.y = 0;
        controller.view.frame = frame;
    }
}
Run Code Online (Sandbox Code Playgroud)

但它没有太多帮助(因为我懒得加载视图,所以当执行时不一定都会加载它们).

有什么方法可以解决这个问题吗?

Efr*_*ain 1

我不确定我是否正确理解你的意思..但是,有一些想法:

框架属性是一回事(A),视图内容如何显示是另一回事(B)。框架 CGRect 是超级视图(父视图)中视图的(理论上)边界。但是,您的视图不一定需要填充整个框架区域

关于(A):这里我们有UIView的autoresizingMask属性来设置当父视图调整大小时框架如何调整大小。当你改变方向时就会发生这种情况。但是,您通常可以依赖默认设置(到目前为止对我有用)。

关于(B):视图内容如何在视图框架中分布是由UIView的属性指定的contentMode。使用此属性,您可以设置长宽比需要保持不变。UIViewContentModeScaleAspectFit例如将其设置为,或其他..

请参阅此处: http: //developer.apple.com/iphone/library/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/doc/uid/TP40006816-CH3-SW99

PS:我写的是“理论”,因为你的视图内容也可能超出这些框架边界 - 它们仅在 UIView 的 ClipsToBounds 属性设置为 YES 时限制视图。我认为苹果默认将其设置为“否”是一个错误。