方向改变时的UIScrollview内容大小 [英] UIScrollview content size when orientation changes

查看:138
本文介绍了方向改变时的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)];


    }

一切正常,scrollview滚动平滑,没有对角线滚动.

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

但是当方向改变时,

我必须再次设置scrollview的框架和contentsize,并且将其设置为

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,这还不够.并且它增加了另一个问题,即scrollview开始对角滚动,我不想要它,因为它看起来太奇怪了.谁能帮助我了解我在哪里以及丢失了什么?

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?

我已将scrollview的自动调整大小蒙版设置为

i have set scrollview's autoresizing mask as

[scroll setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleHeight];

但是更改它无济于事.

推荐答案

这是您的代码中的问题.为什么要这样设置帧大小?您只有320px width的屏幕大小.并且当它更改为landscape时,高度将仅为320px.但是,您将滚动height设置为480px,并将其设置为goes out of the screenstart to scroll diagonally.

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);

像这样改变帧大小

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内容大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆