创建自定义视图可以分为四步:
1、创建 继承于UIView 的类:cmd + n -->iOS下的Source 的 Cocoa Touch Class创建LTView类。
2、根据控件写属性:在LTView.h中写属性:
@property(nonatomic, retain)UILabel *lable;
@property(nonatomic, retain)UITextField *textField;
使用retain 需要在LTView.m中重写dealloc方法,
-(void)dealloc
{
self.lable = nil;
self.textField = nil;
[super dealloc];
}
3、重写initWithFrame方法,创建控件。
“LTView.m”
//UIView初始化方法是重写initWithFrame
-(instancetype)initWithFrame:(CGRect)frame // NSRect(在mac系统中用)改为CGRect
{
self = [super initWithFrame:frame];
if (self) {
NSLog(@"%@",NSStringFromCGRect(frame));
frame.size.height = 2 * kMargin +kHeight;
self.frame = frame;
//创建控件:布局的时候,常用数字经常使用宏,在后期修改比较方便
_lable = [[UILabelalloc]initWithFrame:CGRectMake(kMargin, kMargin, kLabelEidth, kHeight)];
//_lable.backgroundColor = [UIColorgreenColor];
[self addSubview:_lable];
[_lable release];
_textField = [[UITextField alloc]initWithFrame:CGRectMake(kMargin* 2 + kLabelEidth, kMargin, kScreenWidth - kMargin * 3 - kLabelEidth, kHeight)];
_textField.borderStyle =UITextBorderStyleRoundedRect;//边框样式
//_textField.backgroundColor = [UIColorgreenColor];
[self addSubview:_textField];
[_textField release];
}
return self;
}
4、使用自定义视图,检查布局是否正确。
代码:
// AppDelegate.m
#import"AppDelegate.h"
#import"LTView.h"
@interfaceAppDelegate ()
@end
@implementationAppDelegate
-(void)dealloc
{
self.window = nil;
[super dealloc];
}
-(BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization afterapplication launch.
self.window.backgroundColor = [UIColorredColor];
[self.window makeKeyAndVisible];
LTView *ltView = [[LTViewalloc]initWithFrame:CGRectMake(0, 100, 375, 80)];
//ltView.backgroundColor = [UIColorredColor];
ltView.lable.text = @"用户名:";
ltView.textField.placeholder = @"请输入用户名:";
[self.window addSubview:ltView];
[ltView release];
return YES;
}
// AppDelegate.h
#import<UIKit/UIKit.h>
@interfaceAppDelegate : UIResponder <UIApplicationDelegate>
@property(retain, nonatomic) UIWindow *window;
@end
// LTView.h
#import<UIKit/UIKit.h>
@interfaceLTView : UIView
@property(nonatomic, retain)UILabel *lable;
@property(nonatomic, retain)UITextField *textField;
@end
// LTView.m
#import"LTView.h"
#definekMargin 20
#definekHeight 40
#definekLabelEidth 80
#definekScreenWidth [[UIScreen mainScreen]bounds].size.width
@implementationLTView
-(void)dealloc
{
self.lable = nil;
self.textField = nil;
[super dealloc];
}
//UIView初始化方法是重写initWithFrame
-(instancetype)initWithFrame:(CGRect)frame // NSRect(在mac系统中用)改为CGRect
{
self = [super initWithFrame:frame];
if (self) {
NSLog(@"%@",NSStringFromCGRect(frame));
frame.size.height = 2 * kMargin +kHeight;
self.frame = frame;
//创建控件:布局的时候,常用数字经常使用宏,在后期修改比较方便
_lable = [[UILabelalloc]initWithFrame:CGRectMake(kMargin, kMargin, kLabelEidth, kHeight)];
//_lable.backgroundColor = [UIColorgreenColor];
[self addSubview:_lable];
[_lable release];
_textField = [[UITextFieldalloc]initWithFrame:CGRectMake(kMargin * 2 + kLabelEidth, kMargin, kScreenWidth- kMargin * 3 - kLabelEidth, kHeight )];
_textField.borderStyle = UITextBorderStyleRoundedRect;//边框样式
//_textField.backgroundColor = [UIColorgreenColor];
[self addSubview:_textField];
[_textField release];
}
return self;
}
@end