UIButton的自动排列
来源:互联网 发布:ubuntu灰色壁纸 编辑:程序博客网 时间:2024/06/07 02:35
最近在工作中用到了UIButton的排布,需求是根据button的不同宽度(高度相同)实现自动排布。这是第四次用到了,前面分别写了3个项目,用到了三次,感觉还是比较常用的小功能;第一次自己写了个算法,第二次和三次写成固定宽高的按钮规则排布,这次由于要适应不同宽度的需要,在原有基础上做了修改,可以适应不同的按钮宽度,布局自动计算。
目录
- 实现效果
- 基本思路
- 核心代码
实现效果
基本思路
核心问题是根据button的数量,和每一个button的宽度(宽度由文字多少决定)计算button的排布,button高度和内间距可以设为固定常数。
1. 将创建好的按钮放在数组中;
2. 解决分类和计算逻辑:先确定第一个button的位置,然后计算第二个button的位置,第二个button的位置可能在当前行,也可能在下一行。需要两个if else嵌套,在循环遍历button的过程中,动态确定每一个button的位置。
核心代码
1.创建继承于UIView的子类:ButtonLayoutView,在ButtonLayoutView.m中实现计算:
#import "ButtonsLayoutView.h"CGFloat const buttonH = 30;CGFloat const buttonInset = 20;@interface ButtonsLayoutView()/** button array */@property (strong, nonatomic) NSMutableArray *buttonArray;@end@implementation ButtonsLayoutView-(NSMutableArray *)buttonArray { if (!_buttonArray) { _buttonArray = [NSMutableArray array]; } return _buttonArray;}- (instancetype)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { [self createButtons:@[@"30 g",@"20 g",@"40 g",@"50 g",@"样例样例",@"样例样例样例",@"样例",@"30 g",@"20 g",@"40 g",@"50 g",@"样例样例",@"样例样例样例",@"样例"]]; } return self;}- (void)createButtons:(NSArray *)titles{ for (int i = 0; i < titles.count; i++) { UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.backgroundColor = [UIColor purpleColor]; [button setTitle:titles[i] forState:UIControlStateNormal]; [self addSubview:button]; // calucate frame [button sizeToFit]; CGRect rect = button.frame; rect.size.width += 2 * buttonInset; rect.size.height = buttonH; button.frame = rect; // add array [self.buttonArray addObject:button]; }}- (void)layoutSubviews { [super layoutSubviews]; for (int i = 0; i < self.buttonArray.count; i ++) { // 1.take out button UIButton *button = self.buttonArray[i]; CGRect rect = button.frame; CGRect superRect = self.frame; // 2.calucate layout if (i == 0) { // the first button rect.origin.x = buttonInset; rect.origin.y = buttonInset; } else { // the behind button UIButton *lastButton = self.buttonArray[i - 1]; CGRect lastRect = lastButton.frame; CGFloat leftButtonW = CGRectGetMaxX(lastButton.frame) + buttonInset; // over legnth CGFloat rightW = superRect.size.width - leftButtonW - buttonInset; if (rightW < rect.size.width) { // next line rect.origin.x = buttonInset; rect.origin.y = CGRectGetMaxY(lastButton.frame) + buttonInset; } else {// current line rect.origin.x = leftButtonW; rect.origin.y = lastRect.origin.y; } } button.frame = rect; } // superView Height UIButton *button = [self.buttonArray lastObject]; CGRect rect = self.frame; rect.size.height = CGRectGetMaxY(button.frame) + buttonInset; self.frame = rect;}@end
2.测试:在ViewController.m中导入ButtonLayoutView的头文件,开始使用,需要给出ButtonsLayoutView的宽度,和起点坐标,高度会自动被计算:
- (void)viewDidLoad { [super viewDidLoad]; ButtonsLayoutView *layoutView = [[ButtonsLayoutView alloc]init]; layoutView.backgroundColor = [UIColor brownColor]; CGRect rect = layoutView.frame; rect.size.width = 300; rect.origin.x = 50; rect.origin.y = 50; layoutView.frame = rect; layoutView.clipsToBounds = YES; [self.view addSubview:layoutView];}
阅读全文
0 0
- UIButton的自动排列
- UIButton 上的图文上下排列
- UIButton 的图片自动拉伸
- UIButton 文字图片排列
- uibutton图标文字排列
- uibutton图片,文字上下排列
- 关于UIButton的title后的自动宽
- Vb中控件的自动排列
- RadioButtonList 选项的水平排列 自动换行
- CSS自动竖向排列的布局方法
- RadioButtonList 选项的水平排列 自动换行
- RadioButtonList 选项的水平排列 自动换行
- 用UIButton的titleEdgeInsets属性和 imageEdgeInsets属性实现图片文字按要求排列
- iOS 横向展示瀑布流 排列不规则长度标题的UIButton
- iOS UIButton 设置图片文字垂直排列
- ios UIButton 文字左边,图片右边排列
- UIButton 图片和文字上下排列
- iOS UIButton 设置图片文字垂直排列
- ConcurrentHashMap核心方法个人理解 jdk1.8
- Docker 使用指南 (五)—— Dockerfile 详解
- MapReduce 典型代码
- HTML 5 视频
- 刘明春第十天总结
- UIButton的自动排列
- Webbbbbb
- 【bzoj1968】 [Ahoi2005]COMMON 约数研究
- 算术运算
- 【Excel-2010】瀑布图
- 适配器模式
- 杂谈:跨行业的软件思考
- 其他题目---在两个长度相等的排序数组中找到上中位数
- linux简介