将多个 UIButton 添加到 ScrollView
Add Multiple UIButton to a ScrollView
伙计们,我正在尝试在滚动视图中动态添加多个按钮
到目前为止,我有这个代码
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" 之间还有另外两个按钮,但里面什么也没有,有点奇怪。
你们知道我做错了什么吗?
因为你在 aButton1 中又添加了 tButton sButton iButton
到 aButton3。 show 只会在最后一个视图中显示 (aButton3)。如果想像您的代码一样向每个按钮添加 tButton sButton iButton
,则需要在每个 for 循环中初始化新的 tButton sButton iButton
。
错误代码:
tButton = title;
sButton = subtitle;
iButton = image;
它必须是这样的:
UILabel *tButton = [[UILabel alloc] initWithFrame:...];
UILabel *sButton = [[UILabel alloc] initWithFrame:...];
UIImageView *iButton = [[UIImageView alloc] initWithFrame:...];
难怪...
您没有正确设置框架。在 for
循环中,每次创建新按钮 UIButton *aButton = [UIButton new];
并将子视图添加到 multiMenuScroll
。所有这些都被添加但在同一个地方。所以,最后只出现了第三个按钮。
您需要正确设置按钮的框架。
UIButton *aButton = [[UIButton alloc] initWithFrame:<Set_Your_Frame>]; // Change X or Y value as per for loop iteration.
附带说明一下,您将标签命名为按钮后缀,例如 tButton
、sButton
。它以编程方式没有错,但他们混淆了!
vwScroll = 滚动视图的名称
tagsArray = NSString 数组。这些字符串将是标签的文本,将以水平方式添加到滚动视图
-(void)setScrollViewWithArray:(NSArray *)tagsArray{
CGFloat xAxis = 0.0f;
CGFloat gapBwLabels = 10.0f;
[vwScroll.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
for (int i = 0; i<tagsArray.count; i++) {
NSString *tagName = [tagsArray objectAtIndex:i];
CGSize sizeOfTagString = [self getSizeOfText:tagName];
CGFloat width = sizeOfTagString.width+gapBwLabels;
CGRect tagLabelframe = CGRectMake(xAxis, 2, width, 30);
UILabel *lblTag = [[UILabel alloc]initWithFrame:tagLabelframe];
[self setLabel:lblTag withBackgroundColor:[UIColor whiteColor]];
[lblTag setText:tagName];
[vwScroll addSubview:lblTag];
xAxis = xAxis + width + gapBwLabels;
}
[vwScroll setContentSize:CGSizeMake(xAxis, vwScroll.frame.size.height)];
}
伙计们,我正在尝试在滚动视图中动态添加多个按钮
到目前为止,我有这个代码
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" 之间还有另外两个按钮,但里面什么也没有,有点奇怪。 你们知道我做错了什么吗?
因为你在 aButton1 中又添加了 tButton sButton iButton
到 aButton3。 show 只会在最后一个视图中显示 (aButton3)。如果想像您的代码一样向每个按钮添加 tButton sButton iButton
,则需要在每个 for 循环中初始化新的 tButton sButton iButton
。
错误代码:
tButton = title;
sButton = subtitle;
iButton = image;
它必须是这样的:
UILabel *tButton = [[UILabel alloc] initWithFrame:...];
UILabel *sButton = [[UILabel alloc] initWithFrame:...];
UIImageView *iButton = [[UIImageView alloc] initWithFrame:...];
难怪...
您没有正确设置框架。在 for
循环中,每次创建新按钮 UIButton *aButton = [UIButton new];
并将子视图添加到 multiMenuScroll
。所有这些都被添加但在同一个地方。所以,最后只出现了第三个按钮。
您需要正确设置按钮的框架。
UIButton *aButton = [[UIButton alloc] initWithFrame:<Set_Your_Frame>]; // Change X or Y value as per for loop iteration.
附带说明一下,您将标签命名为按钮后缀,例如 tButton
、sButton
。它以编程方式没有错,但他们混淆了!
vwScroll = 滚动视图的名称 tagsArray = NSString 数组。这些字符串将是标签的文本,将以水平方式添加到滚动视图
-(void)setScrollViewWithArray:(NSArray *)tagsArray{
CGFloat xAxis = 0.0f;
CGFloat gapBwLabels = 10.0f;
[vwScroll.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
for (int i = 0; i<tagsArray.count; i++) {
NSString *tagName = [tagsArray objectAtIndex:i];
CGSize sizeOfTagString = [self getSizeOfText:tagName];
CGFloat width = sizeOfTagString.width+gapBwLabels;
CGRect tagLabelframe = CGRectMake(xAxis, 2, width, 30);
UILabel *lblTag = [[UILabel alloc]initWithFrame:tagLabelframe];
[self setLabel:lblTag withBackgroundColor:[UIColor whiteColor]];
[lblTag setText:tagName];
[vwScroll addSubview:lblTag];
xAxis = xAxis + width + gapBwLabels;
}
[vwScroll setContentSize:CGSizeMake(xAxis, vwScroll.frame.size.height)];
}