自定义视图

自定义视图:系统标准UI之外,自己组合⽽而出的新的视图。
iOS提供了很多UI组件,借助它们,我们可以做各种程序。尽管如此,实际开发中,我们还需⾃自定义视图。积累自己的代码库。方便开发。自己封装的视图,能像系统UI
控件一样,用于别的项目中,能大大降低开发成本,提高开发效率。
自定义视图步骤:
根据需求的不同,⾃自定义视图继承的类也有所不同。一般自定义的视图会继承于UIView。以下是⾃自定义视图的要点:
1、创建⼀一个UIView子类
2、在类的初始化方法中添加子视图
3、类的.h
文件提供一些接口(方法),便于外界操作子视图。

LTView:

LTview.h
@interface LTView : UIView
//自定义初始化方法
- (instancetype)initWithFrame:(CGRect)frame labelText:(NSString *)text placeholder:(NSString *)holder
delegate:(id <UITextFieldDelegate>)delegate;
//id() ,instancetype
@property(nonatomic ,retain)UITextField *textField;
@end

LTView.m

#import "LTView.h"
#define kWidth frame.size.width
#define kHeight frame.size.height
@implementation LTView


- (instancetype)initWithFrame:(CGRect)frame labelText:(NSString *)text placeholder:(NSString *)holder delegate:(id <UITextFieldDelegate>)delegate
{
    self =[super initWithFrame:frame];
    if (self) {

        UILabel *label =[[UILabel alloc]initWithFrame:CGRectMake(0, 0, kWidth/4, kHeight)];
        label.text =text;
        [self addSubview:label];
        [label release];


        self.textField =[[UITextField alloc]initWithFrame:CGRectMake(kWidth/4, 0, 3*kWidth/4, kHeight)];
       _textField.placeholder  =holder;
        _textField.borderStyle = UITextBorderStyleRoundedRect;
        _textField.delegate  =delegate   ;

        [self addSubview:_textField];
        [text release];        
    }
    return self;
}
@end

LoginView .h
#import <UIKit/UIKit.h>
#import "LTView.h"
@interface LoginView : UIView

@property(nonatomic ,retain)LTView * username;
@property(nonatomic, retain)UIButton * button;

@end

LoginView.m
#import "LoginView.h"

@implementation LoginView
- (instancetype)initWithFrame:(CGRect)frame
{
    self =[super initWithFrame:frame];
    if (self) {
        self.username =[[LTView alloc]initWithFrame:CGRectMake(50, 100, 240, 30) labelText:@"用户名" placeholder:@"电话号码"delegate:nil];
        [self addSubview:_username];
        [_username release];

        //button
        self.button =[UIButton buttonWithType:UIButtonTypeCustom];
        _button.frame =CGRectMake(150, 150, 60, 30);

        _button.backgroundColor =[UIColor grayColor];


        [_button setTitle:@"登录"forState:UIControlStateNormal];

        [self addSubview:_button];
    }
    return self;
}

LoginViewController.h
#import <UIKit/UIKit.h>

@interface LoginViewController : UIViewController<UITextFieldDelegate>

@end
LoginViewController.m
#import "LoginViewController.h"
#import "LoginView.h"
@interface LoginViewController ()
@property(nonatomic,retain)LoginView *loginView;
@end

@implementation LoginViewController
- (void)dealloc
{
    [_loginView release];
    [super dealloc];
}

- (void )loadView//指定视图控制器直接管理的自定义视图
{
    self.loginView = [[LoginView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.view =_loginView;
    [_loginView release];
}
//创建或者初始化视图控制器直接管理的自定义视图上的所有子视图
//其他操作:设置代理,给button添加事件,网络请求,数据解析。。。
- (void)viewDidLoad {
    [super viewDidLoad];
    //每个视图控制器自带一个View,和window大小一致
    // Do any additional setup after loading the view.

    self.view.backgroundColor =[UIColor yellowColor];
    //设置代理

    [(LoginView *)self.view username].textField.delegate =self  ;

    //添加事件
    [_loginView.button addTarget:self action:@selector(clickButton) forControlEvents:UIControlEventTouchUpInside];


}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (void)clickButton
{
    NSLog(@"我被点急了");
}


#pragma mark -----检测屏幕旋转
//表示支持方向
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;//支持所有方向

}

//暂停音乐、关闭视图交互等
//屏幕将要旋转,(音乐或视频暂停;视图的交互关掉)
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{

}
//添加自定义视图动画
- (void)willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{

}

//播放音乐,打开视图交互
//屏幕旋转完毕(音乐或视频取消暂停;视图的交互打开)
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{

}

//获取当前屏幕的方向

#define Orientation [UIApplication sharedApplication].statusBarOrientation


//重新布局

- (void)layoutSubviews
{
    if (Orientation == UIDeviceOrientationLandscapeLeft ||Orientation ==UIDeviceOrientationLandscapeRight) {

        self.username.frame =CGRectMake(200, 100, 240, 30);
        self.button.frame = CGRectMake(250, 250, 60, 30);
    } else {
        self.username.frame =CGRectMake(50, 100, 240, 30);
        self.button.frame = CGRectMake(150, 150, 60, 30);
    }// [UIApplication sharedApplication].statusBarOrientation
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值