0

我使用下面的代码启用了 UIScrollView 分页选项:

- (void)viewDidLoad
{
    [super viewDidLoad];

    for (int i = 0; i < 6; i++) {
        CGRect frame;
        frame.origin.x = self.scrollView.frame.size.width * i;
        frame.origin.y = 0;
        frame.size = self.scrollView.frame.size;
        [scrollView scrollRectToVisible:frame animated:YES];

        UIView *subview = [[UIView alloc] initWithFrame:frame];
        itemName.text = @"Samara";
        [self.scrollView addSubview:subview];
        [subview release];
    }

    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * 6, 1.0);
}

我有许多元素(按钮、标签),在情节提要中添加到此 ScrollView 元素中,但在 UI 中它们仅出现在第一页上 - 下一页是空白的。我该如何解决这个问题?

4

1 回答 1

1

只需阅读您的代码示例 - 这是如何制作您的滚动视图页面,您在这些滚动视图中添加的内容取决于您:

//Set your scrollView frame
[self.scrollView setFrame:CGRectMake(0,0,320,460];

//Will use this value to set the content width after the loop
CGFloat scrollWidth = 0;
CGFloat pageWidth = self.scrollView.frame.size.width;
CGFloat pageHeight = self.scrollView.frame.size.height;
for (int i = 0; i < 6; ++i)
{
    //Create the page and make the origin.x i * pageWidth
    UIView *page = [[UIView alloc] initWithFrame:CGRectMake((i * pageWidth),0,pageWidth,pageHeight)];
    scrollWidth += page.width;
    //Add the page
    [self.scrollView addSubview:page];
}
//set the content size to the right edge of the last page (scrollWidth)
[self.scrollView setContentSize:CGSizeMake(scrollWidth,pageHeight)];
//Scroll to the last page - assuming you wanted this by looking at the code in your loop
[self.scrollView scrollRectToVisible:CGRectMake((scrollWidth - pageWidth),0,pageWidth,pageHeight)];
//Enable paging and scrolling (scrollEnabled should be YES by default)
self.scrollView.pagingEnabled = YES;
self.scrollView.scrollEnabled = YES;

根据您的代码,我能做到的最好 - 希望它有所帮助

于 2013-04-04T11:41:37.753 回答