CustomView

一丶

#import "AppDelegate.h"

#import "LTView.h"


@interface AppDelegate ()


@end


@implementation AppDelegate



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    // Override point for customization after application launch.

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    [_window release];

    

    // 自定义View

    LTView *ltView = [[LTView alloc] initWithFrame:CGRectMake(30, 50, 295, 50)];

    ltView.myTextField.placeholder = @"请输入用户名";

    ltView.myLabel.backgroundColor = [UIColor magentaColor];

    [self.window addSubview:ltView];

    [ltView release];

    

    return YES;

}



- (void)dealloc

{

    [_window release];

    [super dealloc];

}

二,自定义View

#import <UIKit/UIKit.h>


@interface LTView : UIView


// 自定义控件一般来说会把自带的视图写成属性,方便外部使用

@property (nonatomic, retain)UILabel *myLabel;

@property (nonatomic, retain)UITextField *myTextField;


@end

#import "LTView.h"


@implementation LTView


- (instancetype)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        // view的初始化方法中,对所有的子视图进行初始化创建

        self.myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width / 3, frame.size.height)];

        self.myLabel.backgroundColor = [UIColor orangeColor];

        [self addSubview:self.myLabel];

        [_myLabel release];

        

        

        self.myTextField = [[UITextField alloc] initWithFrame:CGRectMake(frame.size.width / 3 + 20, 0, frame.size.width * 2 / 3 - 20, frame.size.height)];

        self.myTextField.placeholder = @"请输入";

        self.myTextField.borderStyle = UITextBorderStyleRoundedRect;

        self.myTextField.clearButtonMode = UITextFieldViewModeAlways;

        self.myTextField.backgroundColor = [UIColor whiteColor];

        [self addSubview:self.myTextField];

        [_myTextField release];

        

    }

    return self;

}


- (void)dealloc

{

    [_myLabel release];

    [_myTextField release];

    [super dealloc];

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值