自定义视图
自定义视图的步骤
1.创建一个继承自UIView的类LTView
2.重写新类的初始化方法
先创建两个属性:
// 声明成属性 方便外部取出
@property (nonatomic, retain) UILabel *label;
@property (nonatomic, retain) UITextField *textField;
再重写UIView父类的初始化方法
- (instancetype)initWithFrame:(CGRect)frame
{
// 重写LTView的同时 把label也加上去
self = [super initWithFrame:frame];
if (self) {
// 动态获取宽度
CGFloat width = frame.size.width;
// 动态获取高度
CGFloat height = frame.size.height;
self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width / 3, height)];
self.label.backgroundColor = [UIColor whiteColor];
self.label.textColor = [UIColor blackColor];
[self addSubview:self.label];
[_label release];
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(width / 3 + 20, 0, width - 20 - width / 3, height)];
self.textField.borderStyle = UITextBorderStyleLine;
self.textField.backgroundColor = [UIColor whiteColor];
self.textField.textColor = [UIColor blackColor];
self.textField.borderStyle = UITextBorderStyleRoundedRect;
[self addSubview:self.textField];
[_textField release];
}
return self;
}
3.把想添加的视图 封装到新类中(初始化到新类中)
例如: loginView
4.为了方便外部进行赋值或者取值 把添加的视图写成属性(别忘了dealloc释放)
#import <UIKit/UIKit.h>
#import "LTView.h"
@interface LoginView : UIView
@property (nonatomic, retain)LTView *userNameLTView;
@property (nonatomic, retain)LTView *passWordLTView;
@property (nonatomic, retain)UIButton *loginButton;
@property (nonatomic, retain)UIButton *registerButton;
@property (nonatomic, retain)UIButton *findPassWordButton;
@end
然后在.m
中重写初始化方法
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.userNameLTView = [[LTView alloc] initWithFrame:CGRectMake((frame.size.width- 300) / 2, 100, 300, 50)];
[self addSubview:self.userNameLTView];
[_userNameLTView release];
self.userNameLTView.label.text = @"用户名";
self.userNameLTView.textField.placeholder = @"请输入用户名";
self.passWordLTView = [[LTView alloc] initWithFrame:CGRectMake(self.userNameLTView.frame.origin.x, self.userNameLTView.frame.origin.y + self.userNameLTView.frame.size.height, self.userNameLTView.frame.size.width, self.userNameLTView.frame.size.height)];
[self addSubview:self.passWordLTView];
[_passWordLTView release];
self.passWordLTView.label.text = @"密码";
self.passWordLTView.textField.placeholder = @"请输入密码";
NSArray *arr = [NSArray arrayWithObjects:@"登陆",@"找回密码",@"注册", nil];
for (int i = 0; i < 3; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(50 + 100 * i, 300, 90, 50);
button.backgroundColor = [UIColor whiteColor];
[button setTitle:arr[i] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
button.tag = i + 100;
[self addSubview:button];
}
// 属性与循环的button 进行关联
self.loginButton = (UIButton *)[self viewWithTag:100];
self.registerButton = (UIButton *)[self viewWithTag:101];
self.findPassWordButton = (UIButton *)[self viewWithTag:102];
}
return self;
}
5.测试一下
自定义视图的好处: 提高工作效率 大大提高代码的复用性
如果要使用这两个类 可以将4个文件一起脱至所需工程 只要在AppDelegate中引入头文件LTView 和 LoginView 然后初始化一个LoginView的对象 添加至self.window即可显示完全视图