水平分页的UIScrollView,每个视图之间有间隙 - 视图不居中

时间:2012-06-01 08:21:03

标签: objective-c gaps-in-visuals

Apple的WWDC 2010教程视频“使用滚动视图设计应用程序”适用于iOS 4,但在iOS 5中,页面不居中:

enter image description here

似乎

pagingScrollViewFrame.origin.x -= PADDING;

在iOS 5中不起作用。

我的代码如下:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

#define PADDING  10

- (void)loadView 
{    

    // Step 1: make the outer paging scroll view
    CGRect pagingScrollViewFrame = [[UIScreen mainScreen] bounds];
    pagingScrollViewFrame.origin.x -= PADDING;
    pagingScrollViewFrame.size.width += (2 * PADDING);

    pagingScrollView = [[UIScrollView alloc] initWithFrame:pagingScrollViewFrame];
    pagingScrollView.pagingEnabled = YES;
    pagingScrollView.backgroundColor = [UIColor whiteColor];
    pagingScrollView.showsVerticalScrollIndicator = NO;
    pagingScrollView.showsHorizontalScrollIndicator = NO;
    pagingScrollView.contentSize = CGSizeMake(pagingScrollView.bounds.size.width * 6, pagingScrollView.bounds.size.height);
    pagingScrollView.delegate = self;
    self.view = pagingScrollView;

    for(int i = 0; i < 6; i++)
    {
        UIView *view = [[UIView alloc] init];
        view.backgroundColor = [UIColor blueColor];

        CGRect bounds = pagingScrollView.bounds;
        CGRect pageFrame = bounds;
        pageFrame.size.width -= (2 * PADDING);
        pageFrame.origin.x = (bounds.size.width * i) + PADDING;
        view.frame = pageFrame;
        [pagingScrollView addSubview:view];
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [[UIApplication sharedApplication] setStatusBarHidden:YES   
                                    withAnimation:UIStatusBarAnimationSlide];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end

我该怎么办?

1 个答案:

答案 0 :(得分:1)

pageFrame.origin.x = (bounds.size.width * i) + PADDING;

将此部分更改为

pageFrame.origin.x = (bounds.size.width + PADDING) * i;
相关问题