LTView
自定义视图的步骤
- 创建一个继承自UIView的类
- 重写新类的初始化方法
- 把想封装的视图添加封装到 新类里面(初始化到新类中)
- 为了方便外部进行赋值取值 把添加的视图写成属性(别忘了释放内存)
- 测试一下
自定义视图的好处
提高工作效率 大大提高代码的复用性
具体实现
自定义Label-TextField视图
创建LTView类继承自LTView
// 将要创建的控件用属性写在.h文件中
@property (strong, nonatomic) UILabel *label;
@property (strong, nonatomic) UITextField *textField;
// 在.m文件中重写初始化方法 在初始化LTView的同时添加控件
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
// 获取动态高度
CGFloat height = frame.size.height;
// 获取动态宽度
CGFloat width = frame.size.width;
// 根据上面的宽度 来添加label
self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width / 3, height)];
self.label.textAlignment = NSTextAlignmentCenter;
self.label.backgroundColor = [UIColor blueColor];
// 添加到自己身上
[self addSubview:self.label];
// 释放空间
[_label release];
// 动态获取值构建frame可以适应任何尺寸的屏幕,并且当需求改变时,只需更改第一个控件,其余控件位置随之而改
// 初始化一个TextField
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(width / 3 + 20, 0, width / 3 * 2 - 20, height)];
// 设置背景色
self.textField.backgroundColor = [UIColor greenColor];
// 添加到视图上
[self addSubview:self.textField];
// 释放空间
[_textField release];
}
return self;
}
创建登陆界面LoginView利用自定义视图进行构建
// 首先引入头文件LTView.h
// 在头文件中将需要的控件写成属性
@property (nonatomic, retain) LTView *userNameLTView;
@property (nonatomic, retain) LTView *passWordLTView;
@property (nonatomic, retain) UIButton *loginButton;
@property (nonatomic, retain) UIButton *registButton;
@property (nonatomic, retain) UIButton *findPasswordButton;
// 在.m文件中实现添加控件
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
// 初始化输入账户名的l - T
self.userNameLTView = [[LTView alloc] initWithFrame:CGRectMake((kScreenWidth - 300) / 2, 100, 300, 50)];
self.userNameLTView.backgroundColor = [UIColor redColor];
[self addSubview:self.userNameLTView];
[self.userNameLTView release];
// 初始化输入密码的l - T
self.passWordLTView = [[LTView alloc] initWithFrame:CGRectMake(self.userNameLTView.frame.origin.x, self.userNameLTView.frame.origin.y + self.userNameLTView.frame.size.height + kRowHeight, self.userNameLTView.frame.size.width, self.userNameLTView.frame.size.height)];
self.passWordLTView.backgroundColor = [UIColor redColor];
[self addSubview:self.passWordLTView];
[self.passWordLTView release];
// 循环button
for (int i = 0; i < 3 ; i++) {
UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
button.frame = CGRectMake(50 + i * 100,300 , 80, 50);
button.backgroundColor = [UIColor cyanColor];
// 添加标签 方便取出button 与属性的button相对应
button.tag = i + 100;
[self addSubview:button];
}
// 属性与循环创建的button进行关联
self.loginButton = (UIButton *)[self viewWithTag:100];
self.findPasswordButton = (UIButton *)[self viewWithTag:101];
self.registButton = (UIButton *)[self viewWithTag:102];
将属性与循环创建的控件一一对应
// 除了利用tag值 还可以取出父视图上的子视图数组,利用下标进行一一对应
NSArray *array = self.subviews;
self.loginButton = array[2];
[self.loginButton setTitle:@"登陆" forState:(UIControlStateNormal)];
在window上初始化并显示
LoginView *login = [[LoginView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[login.loginButton setTitle:@"登陆" forState:(UIControlStateNormal)];
[login.registButton setTitle:@"注册" forState:(UIControlStateNormal)];
[login.findPasswordButton setTitle:@"找回密码" forState:(UIControlStateNormal)];
[login.loginButton setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
[login.registButton setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
[login.findPasswordButton setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
[self.window addSubview:login];
[login release];
重写controller的loadView⽅法
创建⾃定义视图对象,并指定为controller的view
LoginView *logview = [[LoginView alloc] initWithFrame:[UIScreen mainScreen].bounds];
logview.tag = 1000;
// 帮系统给self.view赋值
self.view = logview;
[logview release];