方向更改时的 UIScrollview 内容大小

UIScrollview content size when orientation changes(方向更改时的 UIScrollview 内容大小)

本文介绍了方向更改时的 UIScrollview 内容大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有分页的滚动视图.在 viewDidLoad 我检查当前方向是否为横向,然后我将其 contentsize 的高度设置为 440

I have a scrollview with pagination. In viewDidLoad i check if the current orientation is landscape then i set its contentsize's height 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)];


    }

一切正常滚动视图滚动流畅,没有对角滚动.

everything works fine scrollview scrolls smoothy and there is no diagonal scrolling.

但是当方向改变时,

我必须再次设置滚动视图的框架和内容大小,我将其设置如下

i have to set scrollview's frame and contentsize again and i am setting it as follow

-(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,这还不够.它又增加了一个问题,即滚动视图开始对角滚动,这是我不想要的,因为它看起来很奇怪.谁能帮助我了解我在哪里以及缺少什么?

i cant understand why i have to set content size's height upto 600 in landscape mode, and that too isnt enough. and it adds one more problem that scrollview starts scrolling diagonally which i dont want as it looks so weird. Can anyone help me understanding where and what i am missing?

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

i have set scrollview's autoresizing mask as

[scroll setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleHeight];

但改变它没有帮助.

推荐答案

这是您的代码中的问题.为什么要这样设置帧大小?你只有 320px width 的屏幕尺寸.而当它变为landscape时,高度将只有320px.但是您将滚动 height 设置为 480px 并且它 走出屏幕 并且 开始对角滚动.

Here it is a problem in your code. Why you are setting the frame size like this?? You have only the screen size of 320px width. And when it changes to landscape, height will be only 320px. But you are setting the scroll height as 480px and it goes out of the screen and start to scroll diagonally.

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

不是那个帧大小,而是像这样改变

Instead of that frame size, change like this

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

您需要设置内容大小,具体取决于您在任一方向的滚动视图中拥有的内容.

And you need to set the content size depend on the content you are having inside the scroll view in either orientation.

这篇关于方向更改时的 UIScrollview 内容大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:方向更改时的 UIScrollview 内容大小

基础教程推荐