每次添加按钮时,UIScrollView都需要变大

时间:2014-10-17 20:31:37

标签: ios objective-c uiscrollview

scrollView在我的故事板中创建并且是水平的。这是我为每个循环添加一个按钮的方法,每次添加一个按钮时都需要增加scrollView的大小。

- (void) populateGroups {
    NSInteger x = [self.groupNameArray count];
    for (int i = 0; i < x; i++) {
        NSString *name = [self.groupNameArray objectAtIndex:i];

        // Create Button
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(count, 0, 100, 54)];
        [button setTitle:name forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:10];
        [button.layer setBorderWidth:1.0f];
        [button.layer setBorderColor:[UIColor whiteColor].CGColor];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(groupPage:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = i;
        [self.groupScrollView addSubview:button];
        count = count + 100;

       // Increase ScrollViewSize
        self.groupScrollView.frame = CGRectMake(0, 0, frameWidth, 80);
        frameWidth = frameWidth + 100;
    }
}

当我尝试向视图添加两个按钮时,scrollView保持不变。

1 个答案:

答案 0 :(得分:1)

您必须增加滚动视图的内容大小,如:

self.groupScrollView.contentSize = CGSizeMake(frameWidth, 80);

并且在循环之后做得更好。