自定义视图步骤
一般自定义的视图会继承于UIView。以下是自定义视图的要点:
1.创建一个UIView子类
2.在类的初始化方法中添加姿势图
3.类的.h文件提供了一些接口(方法),便于外界操作姿势图。
lable-textField视图
我们将LTView作为一个容器,在LTView的初始化方法中创建并添加lable和textField。
创建一个继承与UIView的LTView类,代码如下
//LTView.h
#import <UIKit/UIKit.h>
//声明自定义初始化方法
-(instancetype) initWithFrame:(CGRect) frame
Title:(NSString *)title
Placeholder:(NSString *)placeholder;//占位符
//声明属性
@property(nonatomic) BOOL secureTextEntry;
@property(nonatomic,assign) NSString *text;
@property(nonatomic,assign) id<UITextFieldDelegate>delegate;
@end
//LTView.m
//延展
@interface LTView()<UITextFieldDelegate>
#pragme mark - 声明私有属性
@property(nonatomic,strong)UILabel *label;
@property(nonatomic,strong)UITextField *textField
@end
@implementation LTView
//重写初始化
- (instancetype) initWithFrame:(CGRect) frame
{
self = [super initWithFrame:frame];
if(self)
{
//1.添加label
self.label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,frame.size.width * 0.3,frame.size.height)];
_label.backgroundColor = [UIColor cyanColor];
_label.textAlignment = NSTextAlignmentCenter;
[self addSubview:self.label];
//2.添加textField
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(_label.frame.size.width,0,frame.size.width * 0.7,frame.size.height)];
_textField.backgroundColor = [UIColor lightGrayColor];
[self addSubview:_textField];
}
return self;
}
- (instancetype) initWithFrame:(CGRect) frame
Title:(NSString *)title
Placeholder:(NSString *)placeholder
{
self = [self initWithFrame:frame];
if(self)
{
self.label.text = title;
self.textField.placeholder = placeholder;
}
return self;
}
//重写属性的setter方法,目的只是将外界的值设置给textField
- (void)setSecureTextEntry:(BOOL)secureTextEntry
{
_textField.secureTextEntry = secureTextEntry;
}
- (void)setText:(NSString *)text
{
_textField.text = text;
}
- (NSString *)text
{
return _textField.text;
}
- (void)setDelegate:(id<UITextFieldDelegate>)delegate
{
_textField.delegate = delegate;
}
@end
//AppDelegate.m
#import "AppDelegate.h"
#import "LTview.h"
@interface AppDelegate ()<UITextFieldDelegate>
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
//添加LTView
LTview *userNameLTView =[[LTview alloc]initWithFrame:CGRectMake(50, 50, self.window.frame.size.width - 100, 35) Title:@"用户名" Placeholder:@"手机号/邮箱"];
[self.window addSubview:userNameLTView];
LTview *pwdLTView = [[LTview alloc]initWithFrame:CGRectMake(50, 100, self.window.frame.size.width - 100, 35) Title:@"密码" Placeholder:@"请输入密码"];
pwdLTView.secureTextEntry = YES;
[self.window addSubview:pwdLTView];
pwdLTView.delegate = self;
//添加Button
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(50, 200, self.window.frame.size.width - 100, 35);
[button setTitle:@"登录" forState:UIControlStateNormal];
//绑定方法
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:button];
return YES;
}
- (void)buttonAction:(UIButton *)sender
{
//在这里面实现你想实现的方法
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
//输入完成后,摁以下Enter键,键盘就会下去
[textField resignFirstResponder];
returen YES;
}