問題描述
我有一個帶有分頁的滾動視圖.在 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 內容大小的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!