当屏幕方向改变时,UIScrollview的内容大小会发生变化。

4

我有一个带分页的滚动视图。在viewDidLoad中,如果当前方向是横向,则设置其内容大小的高度为440。

 if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])) 
    {
        [scroll      setContentSize:CGSizeMake(self.scroll.frame.size.width*numberOfPages,340)];


    }
    else if (UIDeviceOrientationIsLandscape([[UIDevice currentDevice] orientation]))

    {
        [scroll setFrame:CGRectMake(0,0,480,480)];
        [scroll setContentSize:CGSizeMake(self.scroll.frame.size.width*numberOfPages, 440)];


    }

一切正常,滚动视图滚动平稳,且没有斜向滚动。

但是当屏幕方向改变时,

我必须重新设置滚动视图的框架和内容大小,我按以下方式进行设置

-(void)orientationChanged:(id)object
{
if(UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation]))
{
    self.scroll.frame = [[UIScreen mainScreen]bounds];

    [scroll setContentSize:CGSizeMake(self.scroll.frame.size.width*numberOfPages, 340)];
}


else
{
 self.scroll.frame = CGRectMake(0,0,480,480);
        [scroll setContentSize:CGSizeMake(self.scroll.frame.size.width*numberOfPages, 600)];
}


}

我不明白为什么在横屏模式下我需要将内容大小的高度设置为600,而且还不够用。这还会增加一个问题,即滚动视图开始对角线滚动,这看起来很奇怪。有人能帮我理解我错在哪里了吗?

我已经将滚动视图的自动调整掩码设置为

[scroll setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleHeight];

但是更改它并没有帮助。


我只想解决我的ScrollView的对角线滚动问题,我不关心其他问题,我也不在乎是否必须将内容大小高度设置为1000。但我想了解背后的原因。当视图已经存在时,仅当视图更改其方向为横向时才会出现对角线滚动。它不会在视图第一次以横向模式出现时发生。 - Vishal Singh
我认为当屏幕方向改变时,您不需要每次更改内容大小,只需要更改框架大小即可。即使您使用自动调整大小,也不需要这样做。 - Dinesh Raja
尝试像这样设置滚动视图的自动调整掩码:[scroll setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth]; - Dinesh Raja
我必须根据我的要求设置其内容大小。如果我不将内容大小的高度增加到600或更高,当方向变为横向时,我将无法垂直滚动并查看滚动视图框架底部的内容。而且,由于我有分页,我不能给scrollview灵活的宽度,因为给予灵活的宽度会导致页面重叠,因为当你给予灵活的宽度时,scrollview会在两侧拉伸自己。这就是为什么每次方向改变时我都要设置scrollview的框架。 - Vishal Singh
2个回答

5
  1. Don't use UIDeviceOrientation. Use UIInterfaceOrientation instead. DeviceOrientation has two extra options you don't need here. (UIDeviceOrientationFaceUp and UIDeviceOrientationFaceDown)

  2. Return Yes from shouldAutorotateToInterfaceOrientation

  3. Now willRotateToInterfaceOrientation: duration: will be called every time you rotate your device.

  4. Implement this method like this.

    -(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
    {
    CGRect frame;
    int pageNumber = 2;
    int statusBarHeight = 20;
    
    if ((toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)) {
        frame = CGRectMake(0, 0, 480, 320 - statusBarHeight);
    } else {
        frame = CGRectMake(0, 0, 320, 480 - statusBarHeight);
    }
    
    scrollView.frame = frame;
    scrollView.contentSize = CGSizeMake(frame.size.width * 2, frame.size.height);
    } 
    

    Let,

    pageNumber = 2

    statusBarHeight = 20


在第二步中提到的 shouldAutorotateToInterfaceOrientation 方法在哪里? - Josh
@Josh shouldAutorotateToInterfaceOrientation 已经被弃用了。这是一个 UIViewController 的方法。现在应该使用 shouldAutorotatesupportedInterfaceOrientations 代替。 - Warif Akhand Rishi

2

你的代码存在一个问题。为什么要这样设置框架大小?你只有 320像素宽度 的屏幕尺寸。当屏幕变成 横向 时,高度只有 320像素。但是你将滚动条的 高度 设置为 480像素,导致它 超出屏幕范围 并且 开始对角线滚动

self.scroll.frame = CGRectMake(0,0,480,480);

不要使用那个框架大小,改为这样

self.scroll.frame = CGRectMake(0,0,480,320);

你需要根据滚动视图中任意方向上的内容来设置内容大小。


1
谢谢你的回答,但我通过创建父级ScrollView来实现垂直滚动,并将需要水平滚动的ScrollView作为parentScroll的子视图添加解决了我的问题。现在它运行得很好,但我认为你的答案也可以解决问题。 - Vishal Singh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接