【发布时间】:2013-01-08 12:31:04
【问题描述】:
我正在使用以下代码添加一系列带有数组的 UIButton。我想知道是否有办法在每个按钮之间添加一些间距,因为它们目前靠得太近了。
这是我的代码,感谢您的帮助:
// Create buttons for the sliding category menu.
buttonArray = [NSMutableArray array];
myImages = [NSArray arrayWithObjects:@"category-cafe-unsel.png", @"category-food-unsel.png", @"category-clothing-unsel.png", @"category-health-unsel.png", @"category-tech-unsel_phone.png" , @"category-tech2-unsel.png", @"catefory-theatre-unsel.png", @"category-travel-unsel.png", nil];
myImagesSel = [NSArray arrayWithObjects:@"category-cafe-sel.png", @"category-food-sel.png", @"category-clothing-sel.png", @"category-health-sel.png", @"category-tech-sel_phone.png" , @"category-tech2-sel.png", @"catefory-theatre-sel.png", @"category-travel-sel.png", nil];
// only create the amount of buttons based on the image array count
for(int i = 0;i < [myImages count]; i++)
{
// Custom UIButton
btn.tag = i+1;
btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0.0f, 20.0f, 52.0f, 52.0f)];
[btn setTitle:[NSString stringWithFormat:@"Button %d", i] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:[myImages objectAtIndex:i]] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:[myImagesSel objectAtIndex:i]] forState:UIControlStateSelected];
NSLog(@"The button title is %@ ", btn.titleLabel.text);
[btn addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[buttonArray addObject:btn];
// NSLog(@"Button tag is: %d",btn.tag);
}
UIImageView *blackColor = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"blackColor.png"]];
// initialize the slide menu by passing a suitable frame, background color and an array of buttons.
slideMenuView = [[SlideMenuView alloc] initWithFrameColorAndButtons:CGRectMake(0.0f, 0.0f, 320.0f, 80.0f) backgroundColor:[UIColor clearColor] buttons:buttonArray];
【问题讨论】:
-
您已将所有按钮的框架设置为相同,这就是为什么您可能会使按钮相互重叠
-
谢谢。它们没有重叠,只是太近了。我只需要每个之间有一个 5 像素的空间。知道我该怎么做吗? :)
-
水平还是垂直?此外,如果您使用上面的代码,按钮将重叠,正如@Leena 所说的那样
-
我认为间距将取决于 slideMenuView 并且您在 slideMenuView 中绘制按钮......
-
按钮之间需要水平或垂直间隙吗?
标签: iphone objective-c ios uibutton