在运行时自动调整 UIButton 的大小。

Posted

技术标签:

【中文标题】在运行时自动调整 UIButton 的大小。【英文标题】:Auto resize UIButton at run time. 【发布时间】:2013-09-23 16:19:37 【问题描述】:

我想在运行时添加按钮时调整现有 UIButtons 的大小,就像 Safari iPad 添加选项卡并且在添加新选项卡时调整所有选项卡的大小一样。我想使用 aotolayout 或 autoresizingmask 来做到这一点。

谢谢

【问题讨论】:

【参考方案1】:

创建一个可变集合来保存对按钮的引用。如果您需要以某种方式对它们进行排序,请创建 NSMutableArray,如果不需要,请创建 NSMutableSet。在头文件中声明像NSMutableSetbuttonSet这样的ivar,然后在类实现中:

float x=0,y=0;
buttonsSet = [[NSMutableSet alloc] init];
for( NSMutableDictionary *dict in places) 
    NSLog(@"x: %f",x);
    x=x+25;
    y=y+25;// Vary these depending on where you want the buttons to be
    UIButton *button = [[[UIButton alloc] initWithFrame:CGRectMake(x,y,25,25)] autorelease];
    button.backgroundColor=[UIColor redColor];
    [buttonsSet addObject:button];
    [self addSubview:button];

这应该可以满足您的需求。

【讨论】:

感谢 abhishek 的回答,但我正在寻找通过自动布局或自动调整蒙版的方法。无论如何 +1 为您的答案。

以上是关于在运行时自动调整 UIButton 的大小。的主要内容,如果未能解决你的问题,请参考以下文章

运行时自动布局的 UIButton 大小

在模拟器上运行时 UIButton 宽度不填充工具栏

UIView 使用自动布局调整大小,但在运行时获取其帧大小不正确 ios

图像在运行时自动调整大小但不在界面生成器上

UIButton的imageView在调整大小时变得不隐藏

当 UILabel 动态调整大小时,在 UIView 中移动 UIButton。 iOS