自定义视图

本文详细介绍如何在iOS应用中创建自定义视图,包括继承UIView类、定义属性、重写初始化方法及布局子控件的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

创建自定义视图可以分为四步:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值