美文网首页
iOS 自定义控件的使用

iOS 自定义控件的使用

作者: xq9527 | 来源:发表于2023-03-15 15:19 被阅读0次

    前言 :

    纯属笔记学习

    image.png

    自定义button

    //
    //  MyButton.m
    //  自定义控件纯代码布局 
    //
    //  Created by xuqing on 2023/3/15.
    //
    
    #import "MyButton.h"
    @interface  MyButton()
    @property(nonatomic ,weak)UILabel * numberLable;
    @end
    
    @implementation MyButton
    
    - (void)setTitle:(NSString *)title num:(int)num{
        
     
    }
    
    - (instancetype)initWithFrame:(CGRect)frame{
        self= [super initWithFrame: frame];
        if(self){
            [self initViews];
        }
        return  self;
    }
    
    - (void)awakeFromNib{
        [super awakeFromNib];
        [self initViews];
        
    }
    -(void)initViews{
        UILabel *lable=[[UILabel alloc]init];
        lable.font=[UIFont systemFontOfSize:16];
        lable.textColor=[UIColor whiteColor];
        lable.backgroundColor=[UIColor redColor];
        [self addSubview:lable];
        self.numberLable=lable;
    }
    -(void)layoutSubviews{
        [super layoutSubviews];
        self.numberLable.frame=CGRectMake(10, 10, 16, 16);
       
    }
    @end
    
    

    在viewcontroller 里面使用

    //
    //  ViewController.m
    //  自定义空间纯代码
    //
    //  Created by xuqing on 2023/3/15.
    //
    #import "ViewController.h"
    #import "MyButton.h"
    @interface ViewController ()
    @end
    @implementation ViewController
    - (void)viewDidLoad {
        [super viewDidLoad];
        MyButton * mybutton=[[MyButton alloc]init];
        [mybutton setTitle:@"系统消息" num:1];
        [mybutton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        mybutton.frame=CGRectMake(50, 400, 130, 65);
        [self.view addSubview:mybutton];
    }
    @end
    

    最后总结

    记录今天都学习 每天进步一点好好学习 天天向上

    相关文章

      网友评论

          本文标题:iOS 自定义控件的使用

          本文链接:https://www.haomeiwen.com/subject/hlblrdtx.html