【发布时间】:2015-09-22 10:01:13
【问题描述】:
伙计们,我正在尝试在滚动视图中动态添加多个按钮
到目前为止,我有这个代码
for (int i = 0; i < numberOfButtonInMenu; i++)
{
UIButton *aButton = [UIButton new];
UILabel *tButton = [UILabel new];
UILabel *sButton = [UILabel new];
UIImageView *iButton = [UIImageView new];
tButton = title;
sButton = subtitle;
iButton = image;
tButton.hidden = NO;
sButton.hidden = NO;
iButton.hidden = NO;
[tButton setText:Titles[i]];
[sButton setText:Subtitles[i]];
[tButton sizeToFit];
[sButton sizeToFit];
iButton.image = [UIImage imageNamed:Images[i]];
aButton.frame = CGRectMake(xCoord, yCoord, screenWidth, buttonHeight);
[aButton addTarget:self action:@selector(whatever:) forControlEvents:UIControlEventTouchUpInside];
[aButton addSubview:tButton];
[aButton addSubview:sButton];
[aButton addSubview:iButton];
[multiMenuScroll addSubview:aButton];
yCoord += buttonHeight + buffer;
}
}
我把我所有的设计都设置在这个for之前。
当我显示我的multiMenuScrollView 时,我最终只显示了最后一个按钮。
(所以对于numberOfButtonInMenu = 3;,我只会显示第三个按钮)
看起来像这样:http://i.stack.imgur.com/5UU6S.jpg
在“Explore”和“Around You”之间还有另外两个 Button,但里面什么都没有,这有点奇怪。 你们对我做错了什么有任何想法吗?
【问题讨论】:
-
所以对于 numberOfButtonInMenu = 3 你有第三个按钮显示。你什么意思?
-
只显示最后一个按钮,在正确的位置。其实另外2个也在,我可以点一下,但是都是空的。
-
你设置好multiMenuScroll的contentSize了吗?
-
abyt07 :是的,一切正常,但我只能看到最后一个按钮(其他按钮在这里,但不可见/为空)
-
@iphonic :我在
for之前这样做。label,subtitle和image,已经有了好的框架(大小和原点),编辑:我试过了,而且......好吧,和以前一样的问题,只显示最后一个按钮:/跨度>
标签: ios objective-c uibutton scrollview