UI基础知识 -- 自定义视图

自定义视图

自定义视图的步骤

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即可显示完全视图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值