添加到UIScrollView的按钮/标签仅出现在第一页上

时间:2013-04-04 11:08:26

标签: ios objective-c uiscrollview

我使用下一个代码启用了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中它们只出现在第一页 - 下一页是空白的。我该如何解决这个问题?

1 个答案:

答案 0 :(得分: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;

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