2012-01-31 26 views
0

我建立風景模式僅的應用程序,我有一個問題是,不與寬度在風景模式在iPhone填滿顏色,但我不明白我該怎麼做,所以請給我一些想法來開發這個功能。iPhone:UIScrollView的分頁在風景模式下問題

參見提前https://github.com/cwalcott/UIScrollView-Paging

感謝。

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad 
{ 
    pageControlBeingUsed = NO; 

    NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil]; 
    for (int i = 0; i < colors.count; i++) 
    { 
     CGRect frame; 
     frame.origin.x = self.scrollView.frame.size.width * i; 
     frame.origin.y = 0; 
     frame.size = self.scrollView.frame.size; 

     UIView *subview = [[UIView alloc] initWithFrame:frame]; 
     subview.backgroundColor = [colors objectAtIndex:i]; 
     [self.scrollView addSubview:subview]; 
     [subview release]; 
    } 

    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * colors.count, self.scrollView.frame.size.height); 

    self.pageControl.currentPage = 0; 
    self.pageControl.numberOfPages = colors.count; 

    [super viewDidLoad]; 
} 

- (void)scrollViewDidScroll:(UIScrollView *)sender 
{ 
    if (!pageControlBeingUsed) 
    { 
     // Switch the indicator when more than 50% of the previous/next page is visible 
     CGFloat pageWidth = self.scrollView.frame.size.width; 
     int page = floor((self.scrollView.contentOffset.x - pageWidth/2)/pageWidth) + 1; 
     self.pageControl.currentPage = page; 
    } 
} 

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView 
{ 
    pageControlBeingUsed = NO; 
} 

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 
{ 
    pageControlBeingUsed = NO; 
} 

- (IBAction)changePage 
{ 
    // Update the scroll view to the appropriate page 
    CGRect frame; 
    frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage; 
    frame.origin.y = 0; 
    frame.size = self.scrollView.frame.size; 
    [self.scrollView scrollRectToVisible:frame animated:YES]; 

    // Keep track of when scrolls happen in response to the page control 
    // value changing. If we don't do this, a noticeable "flashing" occurs 
    // as the the scroll delegate will temporarily switch back the page 
    // number. 
    pageControlBeingUsed = YES; 
} 

enter image description here

回答

0

我認爲之所以會 self.scrollView.frame.size回報(768,1024)在viewDidLoad中:方法,但實際上你需要(1024,768)。爲了解決這個問題,

1)在IB中,屬性檢查器將方向改變爲橫向模式。 或 2)改變

frame.size = self.scrollView.frame.size; 

frame.size = CGSizeMake(1024,768); 
+0

我已經設置了框架大小,但不能解決我的問題,所以有任何其他的想法? – 2012-01-31 07:10:15

+0

您是否嘗試更改x值,我的意思是frame.origin.x正確,以便它將爲0,1024,2048。 – rakeshNS 2012-01-31 07:33:13

+1

感謝您的幫助 – 2012-01-31 08:39:08

2

僅通過解決我的問題在Interface Builder去除內子視圖中的UIScrollView的自動調整大小屬性

+0

你好。我和你有同樣的問題,我遵循你的回答。問題是,只有一半的視圖有一個顏色,而另一個是黑色的。你是如何解決這個問題的? – Jahm 2013-03-03 14:02:25

+0

@Jahm我認爲它的顯示UIWindow顏色,所以你嘗試只是[UIColor clearColor] – 2013-03-04 06:25:40