Ios 动态生成按钮
我想动态生成几个按钮,数字将由背景给出。当我得到它时,我必须用它来创建相应的数字按钮,每个按钮的大小和间距都是相同的,如果按钮不能包含在一行中,它将换行。最小宽度将是一个常数,但实际长度将根据按钮的标题文本。 我的代码在下面,但它不能换行,我也不知道如何使用文本来确定按钮的长度,感谢您的指导Ios 动态生成按钮,ios,objective-c,uibutton,Ios,Objective C,Uibutton,我想动态生成几个按钮,数字将由背景给出。当我得到它时,我必须用它来创建相应的数字按钮,每个按钮的大小和间距都是相同的,如果按钮不能包含在一行中,它将换行。最小宽度将是一个常数,但实际长度将根据按钮的标题文本。 我的代码在下面,但它不能换行,我也不知道如何使用文本来确定按钮的长度,感谢您的指导 - (void)viewDidLoad { [super viewDidLoad]; CGFloat testHeight = 50; CGFloat testWidth =
- (void)viewDidLoad {
[super viewDidLoad];
CGFloat testHeight = 50;
CGFloat testWidth = 100;
CGFloat spaceing = 10;
int number = 5;
for (int i = 0; i < number; i++) {
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(spaceing + testWidth * i + spaceing * i , 100 , testWidth, testHeight )];
[button setBackgroundColor:[UIColor redColor]];
[self.view addSubview:button];
}
}
-(void)viewDidLoad{
[超级视图下载];
CGFloat测试高度=50;
CGFloat testWidth=100;
CGFloat间隔=10;
整数=5;
对于(int i=0;i
您可以使用UICollectionView
来完成此操作,但仅使用UIButtons
数组就不难实现。您可以使用sizeToFit
根据按钮标题调整按钮大小。要使按钮自动换行,您只需检查它是否会超出要添加按钮的视图的最大x值,在您的示例中是self.view
例如,你可以说:
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *buttonStrings = @[@"how", @"now", @"brown", @"cow", @"the", @"quick", @"brown", @"fox"];
NSMutableArray *buttons = [[NSMutableArray alloc]initWithCapacity:[buttonStrings count]];
for (NSString *string in buttonStrings)
{
UIButton *button = [self buttonForString:string];
[buttons addObject:button];
}
[self layoutButtonArray:buttons inView: self.view];
}
// takes an array of buttons and adds them as subviews of view
- (void)layoutButtonArray:(NSArray<UIButton *> *)buttons inView:(UIView *)view
{
CGFloat spacing = 10.0;
CGFloat xOffset = spacing;
CGFloat yOffset = spacing;
for (UIButton *button in buttons)
{
if((xOffset + button.bounds.size.width + spacing) > CGRectGetMaxX(view.bounds))
{
xOffset = spacing;
yOffset += (button.bounds.size.height + spacing);
}
button.frame = CGRectMake(xOffset, yOffset, button.bounds.size.width, button.bounds.size.height);
[view addSubview:button];
xOffset += (button.bounds.size.width + spacing);
}
}
// Takes a string returns a button sized to fit
- (UIButton *) buttonForString:(NSString *)string
{
UIButton *button = [[UIButton alloc]initWithFrame:CGRectZero];
button.backgroundColor = [UIColor redColor];
[button setTitle:string forState:UIControlStateNormal];
[button sizeToFit];
// if you want to have a minimum width you can add that here
button.frame = CGRectMake(button.frame.origin.x, button.frame.origin.y, MAX(button.frame.size.width, kMinWidth), button.frame.size.height);
return button;
}
-(void)viewDidLoad{
[超级视图下载];
NSArray*按钮字符串=@[“如何”,“现在”,“棕色”,“奶牛”,“快速”,“棕色”,“狐狸];
NSMutableArray*按钮=[[NSMutableArray alloc]initWithCapacity:[buttonStrings count]];
用于(NSString*按钮字符串中的字符串)
{
UIButton*button=[self-buttonForString:string];
[按钮添加对象:按钮];
}
[self LayoutButtonRay:查看中的按钮:self.view];
}
//获取按钮数组并将其添加为视图的子视图
-(void)布局按钮查看中的按钮:(u视图*)视图
{
CGFloat间距=10.0;
CGFloat xOffset=间距;
CGFloat yOffset=间距;
用于(UIButton*按钮中的按钮)
{
if((xOffset+button.bounds.size.width+space)>CGRectGetMaxX(view.bounds))
{
xOffset=间距;
yOffset+=(button.bounds.size.height+间距);
}
button.frame=CGRectMake(xOffset、yOffset、button.bounds.size.width、button.bounds.size.height);
[查看添加子视图:按钮];
xOffset+=(button.bounds.size.width+间距);
}
}
//获取字符串返回大小适合的按钮
-(UIButton*)按钮字符串:(NSString*)字符串
{
UIButton*button=[[UIButton alloc]initWithFrame:CGRectZero];
button.backgroundColor=[UIColor redColor];
[按钮设置标题:状态字符串:UIControlStateNormal];
[button sizeToFit];
//如果你想有一个最小宽度,你可以在这里添加
button.frame=CGRectMake(button.frame.origin.x,button.frame.origin.y,MAX(button.frame.size.width,kMinWidth),button.frame.size.height);
返回按钮;
}
您可以使用UICollectionView
来完成此操作,但仅使用UIButtons
数组就不难实现。您可以使用sizeToFit
根据按钮标题调整按钮大小。要使按钮自动换行,您只需检查它是否会超出要添加按钮的视图的最大x值,在您的示例中是self.view
例如,你可以说:
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *buttonStrings = @[@"how", @"now", @"brown", @"cow", @"the", @"quick", @"brown", @"fox"];
NSMutableArray *buttons = [[NSMutableArray alloc]initWithCapacity:[buttonStrings count]];
for (NSString *string in buttonStrings)
{
UIButton *button = [self buttonForString:string];
[buttons addObject:button];
}
[self layoutButtonArray:buttons inView: self.view];
}
// takes an array of buttons and adds them as subviews of view
- (void)layoutButtonArray:(NSArray<UIButton *> *)buttons inView:(UIView *)view
{
CGFloat spacing = 10.0;
CGFloat xOffset = spacing;
CGFloat yOffset = spacing;
for (UIButton *button in buttons)
{
if((xOffset + button.bounds.size.width + spacing) > CGRectGetMaxX(view.bounds))
{
xOffset = spacing;
yOffset += (button.bounds.size.height + spacing);
}
button.frame = CGRectMake(xOffset, yOffset, button.bounds.size.width, button.bounds.size.height);
[view addSubview:button];
xOffset += (button.bounds.size.width + spacing);
}
}
// Takes a string returns a button sized to fit
- (UIButton *) buttonForString:(NSString *)string
{
UIButton *button = [[UIButton alloc]initWithFrame:CGRectZero];
button.backgroundColor = [UIColor redColor];
[button setTitle:string forState:UIControlStateNormal];
[button sizeToFit];
// if you want to have a minimum width you can add that here
button.frame = CGRectMake(button.frame.origin.x, button.frame.origin.y, MAX(button.frame.size.width, kMinWidth), button.frame.size.height);
return button;
}
-(void)viewDidLoad{
[超级视图下载];
NSArray*按钮字符串=@[“如何”,“现在”,“棕色”,“奶牛”,“快速”,“棕色”,“狐狸];
NSMutableArray*按钮=[[NSMutableArray alloc]initWithCapacity:[buttonStrings count]];
用于(NSString*按钮字符串中的字符串)
{
UIButton*button=[self-buttonForString:string];
[按钮添加对象:按钮];
}
[self LayoutButtonRay:查看中的按钮:self.view];
}
//获取按钮数组并将其添加为视图的子视图
-(void)布局按钮查看中的按钮:(u视图*)视图
{
CGFloat间距=10.0;
CGFloat xOffset=间距;
CGFloat yOffset=间距;
用于(UIButton*按钮中的按钮)
{
if((xOffset+button.bounds.size.width+space)>CGRectGetMaxX(view.bounds))
{
xOffset=间距;
yOffset+=(button.bounds.size.height+间距);
}
button.frame=CGRectMake(xOffset、yOffset、button.bounds.size.width、button.bounds.size.height);
[查看添加子视图:按钮];
xOffset+=(button.bounds.size.width+间距);
}
}
//获取字符串返回大小适合的按钮
-(UIButton*)按钮字符串:(NSString*)字符串
{
UIButton*button=[[UIButton alloc]initWithFrame:CGRectZero];
button.backgroundColor=[UIColor redColor];
[按钮设置标题:状态字符串:UIControlStateNormal];
[button sizeToFit];
//如果你想有一个最小宽度,你可以在这里添加
button.frame=CGRectMake(button.frame.origin.x,button.frame.origin.y,MAX(button.frame.size.width,kMinWidth),button.frame.size.height);
返回按钮;
}
你要找的是“按钮网格”搜索。你要找的是“按钮网格”搜索。