LinearLayout之初级学习 (二) ScrollView
来源:互联网 发布:域名认证和备案的区别 编辑:程序博客网 时间:2024/05/17 00:10
上一篇文章
这一片文章主要介UIScrollView 的自动布局
第一步为UIScrollView 初始化,
UIScrollView *scrollView = [UIScrollView new];
scrollView.backgroundColor = [UIColor whiteColor];
self.view = scrollView ;
MyLinearLayout *linearLayout = [MyLinearLayout linearLayoutWithOrientation:MyLayoutViewOrientation_Vert];
linearLayout.wrapContentHeight = YES ;
linearLayout.myLeftMargin = linearLayout.myRightMargin = 0 ;
linearLayout.padding = UIEdgeInsetsMake(10, 10, 10, 10) ;
[self.view addSubview:linearLayout];
(1)在ScrollView 中添加一个Vertical 形式的一个子布局
效果如下
分析: 1. 子视图类型 UILabel 和 UITextField
2. 排列方式 竖直排列 (Vertical) 对应MyLinearLayout 的类型是 MyLayoutViewOrientation_Vert
3. 为Label 和TextField 添加一个共同的layout,这样比较好管理
代码实现:
先新建一个共同的Layout 如下
MyLinearLayout *vertical1 = [MyLinearLayout linearLayoutWithOrientation:MyLayoutViewOrientation_Vert];
vertical1.myMargin = 0 ;
vertical1.wrapContentWidth = NO ;
vertical1.wrapContentHeight = YES ;
[layout addSubview:vertical1];
在layout 中添加Label
UILabel *label = [[UILabel alloc] init];
label.myMargin = 0 ;
label.text = @"编号" ;
label.flexedHeight = YES ;
[label sizeToFit] ;
[vertical1 addSubview:label];
在layout 中添加UITextField
UITextField *textField = [[UITextField alloc] init];
textField.borderStyle = UITextBorderStyleRoundedRect ;
textField.myTopMargin = 10 ;
textField.myLeftMargin = textField.myRightMargin = 0 ;
textField.myHeight = 30 ;
textField.placeholder = @"这里输入编号" ;
[vertical1 addSubview:textField];
(2) 年龄和下面的整体是一个layout类型为 例如
分析:1. 三个等间距 等宽的label,横向排列(MyLayoutViewOrientation_Horz)叫暂时称为horizLayout
2. 年龄 和 horizLayout 纵向排列
先定义了一个总的Layout
MyLinearLayout *ageLayout = [MyLinearLayout linearLayoutWithOrientation:MyLayoutViewOrientation_Vert]; // 定义纵向排列
ageLayout.layer.borderColor = [UIColor blackColor].CGColor; // 定义边线的颜色
ageLayout.layer.borderWidth = 1 ; // 定义边线的宽度
ageLayout.layer.cornerRadius = 5 ; // 定义圆角的大小
ageLayout.myTopMargin = 10 ; // 定义上边界
ageLayout.myLeftMargin = ageLayout.myRightMargin = 0 ; // 定义左边界 和 右边界
ageLayout.wrapContentHeight = YES ; // 定义约束的高度是自适应的
ageLayout.padding = UIEdgeInsetsMake(5, 5, 5, 5) ; // 定义约束的内边距
添加一个labe
UILabel *age = [[UILabel alloc] init];
age.text = @"年龄" ;
[age sizeToFit]; // 自适应宽度
age.flexedHeight = YES ; // 自适应宽度
age.myLeftMargin = age.myTopMargin = 0 ; // 定义左边界和 上边界
[ageLayout addSubview:age];
在添加一个横向布局的约束用来装三个label
MyLinearLayout *coLayout = [MyLinearLayout linearLayoutWithOrientation:MyLayoutViewOrientation_Horz];
coLayout.myTopMargin = 5 ;
coLayout.subviewMargin = 5 ;
coLayout.myLeftMargin = coLayout.myRightMargin = 0 ;
coLayout.wrapContentHeight = YES ;
[ageLayout addSubview:coLayout];
添加三个label 设置weight = 1 这样label 的宽度会根据margin 来均分剩余的宽度
UILabel *l1 = [[UILabel alloc] init];
l1.text = @"20" ;
l1.flexedHeight = YES ;
l1.textAlignment = NSTextAlignmentCenter ;
l1.weight = 1;
l1.backgroundColor = [UIColor redColor];
[coLayout addSubview:l1];
UILabel *l2 = [[UILabel alloc] init];
l2.text = @"30" ;
l2.weight = 1;
l2.textAlignment = NSTextAlignmentCenter ;
l2.flexedHeight = YES ;
l2.backgroundColor = [UIColor redColor];
[coLayout addSubview:l2];
UILabel *l3 = [[UILabel alloc] init];
l3.text = @"40" ;
l3.weight = 1 ;
l3.textAlignment = NSTextAlignmentCenter ;
l3.flexedHeight = YES ;
l3.backgroundColor = [UIColor redColor];
[coLayout addSubview:l3];
- LinearLayout之初级学习 (二) ScrollView
- Android学习-ScrollView LinearLayout View
- LinearLayout之二
- 二、Oracle初级学习之初级必备sql(一)
- Android之LinearLayout学习
- android学习之LinearLayout
- LinearLayout(二)
- Android Layout之二:LinearLayout
- IOS初级学习二
- UGUI学习笔记(二) ScrollView相关
- JAVA 反射 总结 之 初级 (二)
- c++初级之二
- Andorid学习(二)RelativeLayout布局LinearLayout布局
- Android学习(二) LinearLayout和RelativeLayout比较
- Android初级-界面编程第一篇之LinearLayout,RelativeLayout
- 机器学习算法与Python实践之(二)支持向量机(SVM)初级
- 机器学习算法与Python实践之(二)支持向量机(SVM)初级
- 机器学习算法与Python实践之(二)支持向量机(SVM)初级
- centos7.1 安装 svnmanager1.10
- cnn中权值共享理解
- Cluster vs Clustering
- 《精通CSS:高级Web标准解决方案》学习过程--V
- LintCode:最长回文子串
- LinearLayout之初级学习 (二) ScrollView
- 做企业永远不变的是做好产品与做好自已
- iptables详细解释
- RxJava学习笔记
- Java垃圾收集算法与内存泄露
- PHP学习05----PHP数据类型
- 高级渲染配置
- Service的工作过程
- position详解