zoukankan      html  css  js  c++  java
  • xcode UIButton创建、监听按钮点击、自定义按钮 、状态 、内边距

    代码创建

    //创建UIButton

            UIButton * btnType=[[UIButton alloc]init];

            //设置UIControlStateNormal状态下的文字颜色

            [btnType setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

            //设置字体大小

            btnType.titleLabel.font=[UIFont systemFontOfSize:9.0];

            //设置边框的宽度

            btnType.layer.borderWidth=1;

            //设置边框的颜色

            btnType.layer.borderColor=[[UIColor lightGrayColor]CGColor];

            //设置UIControlStateNormal的文字

            [btnType setTitle:@"按钮设置名字" forState:UIControlStateNormal];

            //设置UIControlStateNormal的图片

            [btnType setImage:[UIImage imageNamed:@"获取Assets.xcassets图片名称"] forState:UIControlStateNormal];

            //设置UIControlStateNormal背景图片

            [btnType setBackgroundImage:[UIImage imageNamed:@"获取Assets.xcassets图片名称"] forState:UIControlStateNormal];

            //设置失效状态

            btnType.enabled=NO;

    监听按钮点击

    //只要按钮触发了UIControlEventTouchUpInside事件,就调用self对象buttonClick方法

    [btn addTarget:self action:@selector(buttonClick) forCOntrolEvents:UIControlEventTouchUpInside];

    自定义按钮

    -(CGRect)titleRectForContentRect:(CGRect)contentRect{

            // 返回文字的frame   

     } 

    -  (CGRect)imageRectForContentRect:(CGRect)contentRect{

            // 返回图片的frame

     }

    按钮显示的状态

    设置不能点击 enabled 等于NO

    adjustsImageWhenDisabled 等于NO  在Disabled下要不要调整显示的图片

    adjustsImageWhenHighlighted 高亮下不要调整图片

    按钮内边距

    通过代码设置   contentEdgeINsets =   uiedgeInsetsMake 设置内边距

     contentEdgeInsets=UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>);

    设置文字  titleEdgeInsets=uiedgeInsetsMake 设置文字的内边距

    titleEdgeInsets=UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>);

    设置图片 imageEdgeInsets=uiedgeInsetsMake 设置图片的内边距

    imageEdgeInsetss=UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>);

    微小调整使用内边距     复杂的话使用自定义按钮

  • 相关阅读:
    P1352 没有上司的舞会
    P1879 [USACO06NOV]玉米田Corn Fields
    P1896 [SCOI2005]互不侵犯
    2019寒假纪中happy之旅
    JZOJ 4249. 【五校联考7day1】游戏
    JZOJ 4248. 【五校联考7day1】n染色
    JZOJ 4252. 【五校联考7day2】QYQ的图
    STM32初学Keil4编译时出现 Error:Failed to execute 'BIN40/Armcc'
    STM32初学Keil4编译时出现 Error:Failed to execute 'BIN40/Armcc'
    重踏学习Java路上_Day02(java 基础上)
  • 原文地址:https://www.cnblogs.com/fleas/p/5565117.html
Copyright © 2011-2022 走看看