如何在按钮单击期间在scrollview中添加数组元素

时间:2014-09-26 07:31:22

标签: ios iphone

我想在按钮单击期间在滚动视图中添加元素..如果元素添加意味着滚动视图想要变大。取决于滚动视图中的添加元素..当我点击按钮时,元素应该在scrollview中逐个添加..

3 个答案:

答案 0 :(得分:1)

在viewcontroller中维护一个height index

float = scrollViewheight;

添加任何对象时,请将UILabel添加到scrollView,然后将increase scrollViewheight添加为此

UILabel *lblText = [UILabel new];
lblText.frame = CGRectMake(20,20,280,22);

[scrollView addSubview:lblText];

//update scrollViewheight
scrollViewheight = lblText.frame.origin.y+lblText.frame.size.height;

//update scrollView's content size
[scrollView setContentSize:CGSizeMake(0,scrollViewheight);

添加new object时使用scrollViewheight

UILabel *lblText1 = [UILabel new];
lblText1.frame = CGRectMake(20,scrollViewheight+20,280,22);

[scrollView addSubview:lblText1];

//update scrollViewheight
scrollViewheight = lblText1.frame.origin.y+lblText1.frame.size.height;

//update scrollView's content size
[scrollView setContentSize:CGSizeMake(0,scrollViewheight);

编辑:刚刚格式化

答案 1 :(得分:0)

您可以使用[self.scroll addSubview:myView]添加UIView元素,并相应调整内容大小。

答案 2 :(得分:0)

- (void)buttonClicked:(UIButton *)button {
    // adds new view to scrollView
    [self.scrollView addSubview:anotherView];
    int padding = 10;
    // set scrollView content size
    [self.scrollView setContentSize:CGSizeMake(self.scrollView.contentView.size.width, self.scrollView.frame.size.height + padding + anotherView.frame.size.height)]; 
    [self.scrollView setContentOffset:CGPointMake(0, anotherView.frame.origin.y) animated:YES]; // scrolls to new view
}

希望有所帮助