自定义视图步骤
1.创建一个继承自UIView类
2.重写新类的初始化方法
3.把想添加的视图封装到新类里(初始化到新类)
4.为了方便外部进行赋值或者取值 把添加的视图写成属性(别忘了释放)
5.测试一下
自定义视图的好处: 提高工作效率 大大提高代码的复用性
1.创建一个继承自UIView类
@interface LTView : UIView
2.把要添加的视图写成属性(别忘了释放)要添加多少视图就写多少个属性
@property (nonatomic,retain)UILabel *label;
@property (nonatomic,retain)UITextField *textField;
添加成属性就要记得释放
– (void)dealloc
{
[_label release];
[_textField release];
[super dealloc];
}
3.重写新类的初始化方法
4.把想添加的视图封装到新类里(初始化到新类)
– (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
获取动态宽度
CGFloat width = frame.size.width;
获取动态高度
CGFloat height = frame.size.height;
根据上面的宽度 在本视图上添加label
self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0,(width / 3), height)];
设置label的属性
self.label.backgroundColor = [UIColor yellowColor];
把文本视图添加到本视图上
[self addSubview:self.label];
释放
[_label release];
根据上面的宽度 在本视图上添加textField
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(width / 3 + 20, 0, (width - width / 3 - 20), height)];
设置textField的属性
self.textField.backgroundColor = [UIColor greenColor];
把输入框添加到本视图上
[self addSubview:self.textField];
释放
[_textField release];
}
return self;
}
创建好的自定义视图还可以复用
1.创建一个继承自UIView类和引入自定义视图的头文件
#import “LTView.h”
@interface LoginView : UIView
2.把要添加的视图写成属性
@property(nonatomic,retain)LTView *userNameLTView;
@property(nonatomic,retain)LTView *passwordLTView;
@property(nonatomic,retain)UIButton *loginButton;
@property(nonatomic,retain)UIButton *registerButton;
@property(nonatomic,retain)UIButton *retrieveButton;
– (void)dealloc
{
[_passwordLTView release];
[_userNameLTView release];
[_loginButton release];
[_loginButton release];
[_retrieveButton release];
[super dealloc];
}
宏定义屏幕的高
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
宏定义屏幕的高
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
宏定义 行间距
#define kRowHeight 30
3.重写初始化方法
– (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
创建一个引入的自定义视图
self.userNameLTView = [[LTView alloc] initWithFrame:CGRectMake((kScreenWidth - 300)/2 , 100, 300, 50)];
给引入的自定义视图设置属性
self.userNameLTView.backgroundColor = [UIColor magentaColor];
将引入的自定义视图添加到本自定义视图上
[self addSubview:self.userNameLTView];
释放
[_userNameLTView release];
获取上一个自定义视图的x,y,宽,高.
CGFloat width = self.userNameLTView.frame.size.width;
CGFloat height = self.userNameLTView.frame.size.height;
CGFloat x = self.userNameLTView.frame.origin.x;
CGFloat y = self.userNameLTView.frame.origin.y;
根据上一个视图的位置创建另一个自定义视图
self.passwordLTView = [[LTView alloc] initWithFrame:CGRectMake(x,y + height + 30,width, height)];
将先创建的自定义视图添加在本自定义视图
[self addSubview:self.passwordLTView];
释放
[_passwordLTView release];
循环创建Button
for (int i = 0; i < 3; i++) {
UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
使三个Button的位置平行不叠加
button.frame = CGRectMake(45 + (i * 100), 300, 90, 50);
加标签 方便取出Button 与属性Button 相对应
button.tag = i + 100;
添加到本自定义视图上
[self addSubview:button];
}
属性与循环的Button 进行关联
self.loginButton = (UIButton *)[self viewWithTag:100];
self.registerButton = (UIButton *)[self viewWithTag:101];
self.retrieveButton = (UIButton *)[self viewWithTag:102];
}
return self;
}