我想在点击按钮的过程中添加滚动视图中的元素。。如果元素添加意味着滚动视图想要变大。取决于滚动视图中添加的元素。。当我单击按钮时,应该在scrollview中一个接一个地添加这些元素。。
在视图控制器中维护一个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);
编辑:刚刚格式化的
您可以使用[self.scroll addSubview:myView]
添加UIView元素,并相应地调整内容大小。
- (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
}
希望它能帮助