2010-03-15 87 views
2

我正在嘗試使UIScrollView自動調整大小並在橫向模式下正確顯示。肖像模式正常工作。 這裏是我的代碼:UIScrollView在橫向模式下的幀尺寸問題

- (void)viewDidLoad { 
[super viewDidLoad]; 

//=== load all the custom view 
NSMutableArray *controllers = [[NSMutableArray alloc] init]; 

int page = 0; 
[controllers addObject:[self loadScrollView:[[Page1ViewController alloc] initWithNibName:@"Page1ViewController" bundle:nil] withPage:page++]]; 
[controllers addObject:[self loadScrollView:[[Page2ViewController alloc] initWithNibName:@"Page2ViewController" bundle:nil] withPage:page++]]; 
[controllers addObject:[self loadScrollView:[[Page3ViewController alloc] initWithNibName:@"Page3ViewController" bundle:nil] withPage:page++]]; 

self.viewControllers = controllers; 
[controllers release]; 
//=== automaticaly define number of pages 
_numberOfPages = [self.viewControllers count]; 

// a page is the width of the scroll view 
scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
scrollView.pagingEnabled = YES; 
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * _numberOfPages, scrollView.frame.size.height); 
scrollView.showsHorizontalScrollIndicator = NO; 
scrollView.showsVerticalScrollIndicator = NO; 
scrollView.scrollsToTop = NO; 
scrollView.delegate = self; 

// First, set the number of pages 
pageControl.numberOfPages = _numberOfPages; 
pageControl.currentPage = 0; 

}

於是,我走進了每個視圖控制器榫文件和背景設置成不同的顏色,以測試它的工作。不幸的是,在橫向模式下,寬度和高度與縱向模式下保持一致。

我不知道如何解決這個問題。希望任何人都可以幫助我。 非常感謝提前。

回答

3

我有同樣的問題,但只有當我在Landscape中打開視圖時,它是在Portrait中的.xib中設計的。

如果我以縱向打開,然後旋轉設備,它的尺寸正確。

我發現,如果我在viewWillAppear中加載我的視圖控制器數組,而不是在viewDidLoad中,它始終正確調整大小。