4

我有一個帶分頁的滾動視圖。在viewDidLoad中我檢查當前的方向是橫向的話,我設置其contentsize身高440UIScrollview方向更改時的內容大小

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


    } 

一切正常滾動視圖滾動smoothy並沒有角滾動。

但是當方向改變,

我必須設置滾動型的框架,並再次contentsize,我將其設置爲跟隨

-(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]; 

但改變它不幫助。

+0

我只是想阻止我的滾動型的角滾動的問題,我不關心其他的問題,我不關心,如果如果我必須給內容大小高度1000.但我想了解它背後的原因。對角線滾動僅在視圖已經存在時將其方向更改爲橫向時發生。當視圖僅在橫向模式下首次出現時,它不會發生。 –

+0

我想你不需要每次改變方向時改變內容。你只需要改變幀的大小。即使你使用自動調整,你也不需要這樣做。 –

+0

[scroll setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];嘗試像這樣 –

回答

2

這裏是代碼中的問題。爲什麼你要像這樣設置框架尺寸?您只有屏幕尺寸320px width。當它變成landscape時,高度將只有320px。但是您將滾動height設置爲480px,並將其設置爲goes out of the screenstart to scroll diagonally

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

而不是幀的大小,改變這樣

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

而且你需要設置內容大小取決於你的滾動視圖中有任何一個方向的內容

+1

謝謝你的答案,但我解決了我的問題,使父滾動垂直滾動和添加滾動視圖需要作爲parentScroll的子視圖水平滾動。它現在工作正常,但我認爲你的答案也可以做到這一點。 –

5
  1. 請勿使用UIDeviceOrientation。改爲使用UIInterfaceOrientationDeviceOrientation有兩個額外的選項,你不需要在這裏。 (UIDeviceOrientationFaceUpUIDeviceOrientationFaceDown

  2. 返回YesshouldAutorotateToInterfaceOrientation

  3. 現在willRotateToInterfaceOrientation: duration:將被調用每次旋轉你的設備。

  4. 像這樣實施這種方法。

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

    令,

    PAGENUMBER = 2

    statusBarHeight = 20

+0

您在步驟2中提到的shouldAutorotateToInterfaceOrientation在哪裏? – Josh

+0

@Josh'shouldAutorotateToInterfaceOrientation'現已被棄用。這是一個'UIViewController's'方法。改爲使用'shouldAutorotate'和'supportedInterfaceOrientations'。 –