iPhone:UIScrollView在横向模式问题中进行分页

时间:2012-01-31 06:12:10

标签: iphone

我只在横向模式中构建了一个应用,我有一个问题就是在iPhone中横向模式中没有用宽度填充颜色,但我不能了解我该怎么做,所以请给我一些想法来开发这个功能。

参考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

2 个答案:

答案 0 :(得分:2)

通过在界面生成器中删除UIScrollView的自动调整属性中的内部子视图来解决我的问题。

答案 1 :(得分:0)

我认为原因是      self.scrollView.frame.size在viewDidLoad:方法中返回(768,1024),但实际上你需要(1024,768)。要解决这个问题,

1)在IB中,属性Inspecter将方向更改为横向模式。 要么 2)改变

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

frame.size = CGSizeMake(1024,768);