[英]ObjC - Programmatically add label to UIScrollView
您如何以編程方式向UIScrollView添加標簽(或其他控件)?
我使用了這段代碼,但我無法滾動。 這讓我相信它不會將標簽添加到UIScrollView。
這是我的代碼:
for (int i = 1; i <= 30; i++)
{
UILabel *myLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, (i * 50), 200, 40)];
[myLabel setBackgroundColor:[UIColor clearColor]];
[myLabel setText:@"Dynamic"];
[self.Scroller addSubview:myLabel];
}
沒有錯誤,它確實創建了標簽,但就像我之前說的那樣,沒有滾動。
我們做錯了什么?
提前致謝。
[self.Scroller setContentSize:CGSizeMake(self.Scroller.frame.size.width, (30 * 50)];
for (int i = 1; i <= 30; i++)
{
UILabel *myLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, (i * 50), 200, 40)];
[myLabel setBackgroundColor:[UIColor clearColor]];
[myLabel setTextColor:[UIColor blackColor]];
[myLabel setText:@"Dynamic"];
[self.Scroller addSubview:myLabel];
}
CGSize newContentSize=scroll.contentSize;
newContentSize.height+=30*50;
[self.Scroller setContentSize:newContentSize];
您沒有為scrollView設置contentSize,在循環中添加此行: -
CGSize newContentSize=scroll.contentSize;
newContentSize.height+=i*50;
[self.Scroller setContentSize:newContentSize];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.