首页 > 其他 > 详细

UI基础 自定义视图

时间:2020-07-22 00:53:59      阅读:88      评论:0      收藏:0      [点我收藏+]

 

RootViewControlle.m
#import "RootViewController.h"
#import "MyView.h"
@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 第一步 新建一个类继承UIView
//     第二步 分析新视图由哪一些旧视图构成 并且把它声明称属性
//    第三步 重写初始化方法 在方法里面完成布局
    // 使用自定义视图
    MyView* view=[[MyView alloc] initWithFrame:CGRectMake(10, 100, 306, 50)];
    [self.view addSubview:view];
    
//    view.label.text=@"用户名";
//    view.tf.placeholder=@"请输入用户名";
//
    
    
    [view setLabelText:@"hah"];
    
}

@end

 

 

MyView.h

#import <UIKit/UIKit.h>



@interface MyView : UIView
//声明一个方法 专门设置label的文字
-(void)setLabelText:(NSString *)string;

@end

 

MyView.m

#import "MyView.h"

//延展
@interface MyView()
@property(nonatomic,strong)UILabel* label;
@property(nonatomic,strong)UITextField* tf;

@end


@implementation MyView
//重写初始化方法 完成布局
-(id)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if(self){
        
        _label=[[UILabel alloc]initWithFrame:CGRectMake(5, 5, 80, 40)];
        _tf=[[UITextField alloc] initWithFrame:CGRectMake(90, 5, 240, 40)];
        
        _label.backgroundColor=[UIColor purpleColor];
        _tf.backgroundColor=[UIColor redColor];
        self.backgroundColor=[UIColor cyanColor];
        
        
        [self addSubview:_label];
        [self addSubview:_tf];
        
        
    }
    return self;
    }

//实现方法
-(void)setLabelText:(NSString *)string
{
    _label.text=string;
    
}



@end

 

UI基础 自定义视图

原文:https://www.cnblogs.com/zhangqing979797/p/13357898.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!