美文网首页SO 问题来了UIButton
button(selected、enabled、……)相关内容

button(selected、enabled、……)相关内容

作者: 失忆的程序员 | 来源:发表于2016-06-17 10:49 被阅读104次

    layer.cornerRadius =button.width/2


    btn.frame = CGRectMake(x, y, width, height);

    [btn setTitle: @"search" forState: UIControlStateNormal];

    //设置按钮上的自体的大小

    //[btn setFont: [UIFont systemFontSize: 14.0]];    //这种可以用来设置字体的大小,但是可能会在将来的SDK版本中去除改方法

    //应该使用

    btn.titleLabel.font = [UIFont systemFontOfSize: 14.0];

    [btn seBackgroundColor: [UIColor blueColor]];

    //最后将按钮加入到指定视图superView

    [superView addSubview: btn];

    ==========================================================

    tvnamelabel=[[UIButton alloc]initWithFrame:CGRectMake(5,5,200,40)];

    这样初始化的button,文字默认颜色是白色的,所有如果背景也是白色的话,是看不到文字的,

    btn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft ;

    //设置文字位置,现设为居左,默认的是居中

    [btn setTitle:@“title”forState:UIControlStateNormal];

    // 添加文字

    有些时候我们想让UIButton的title居左对齐,我们设置

    btn.textLabel.textAlignment = UITextAlignmentLeft

    是没有作用的,我们需要设置

    btn.contentHorizontalAlignment = UIControlContentHorizonAlignmentLeft;

    但是问题又出来,此时文字会紧贴到做边框,我们可以设置
    btn.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);
    使文字距离做边框保持10个像素的距离。
    =======================================================
    设置UIButton上字体的颜色设置UIButton上字体的颜色,不是用:
    [btn.titleLabel setTextColor:[UIColorblackColor]];
    btn.titleLabel.textColor=[UIColor redColor];
    而是用:
    [btn setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];


    selected

    设置好selected状态下的图片和文字的时候,当点击的时候发现selected状态下的文字不显示,

    解决方法:
    重新设置selected状态下文字的颜色,不要和normal状态下的颜色一样。

    enabled

    UIButton *btn = [[UIButton alloc] init];
       btn.enabled = NO;
       btn.userInteractionEnabled = NO;

       UIControlStateNormal;
       UIControlStateHighlighted;
       // 注意点: 只有设置按钮的enabled = NO才能够有达到这个状态(UIControlStateDisabled);
       UIControlStateDisabled;



    一、按钮的状态
    1.UIControlStateNormal
    1> 除开UIControlStateHighlighted、UIControlStateDisabled、UIControlStateSelected以外的其他情况,都是normal状态
    2> 这种状态下的按钮【可以】接收点击事件

    2.UIControlStateHighlighted
    1> 【当按住按钮不松开】或者【highlighted = YES】时就能达到这种状态
    2> 这种状态下的按钮【可以】接收点击事件

    3.UIControlStateDisabled
    1> 【button.enabled = NO】时就能达到这种状态
    2> 这种状态下的按钮【无法】接收点击事件

    4.UIControlStateSelected
    1> 【button.selected = YES】时就能达到这种状态
    2> 这种状态下的按钮【可以】接收点击事件

    二、让按钮无法点击的2种方法
    1> button.enabled = NO;
    *【会】进入UIControlStateDisabled状态
    2> button.userInteractionEnabled = NO;

    *【不会】进入UIControlStateDisabled状态,继续保持当前状态


    right.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;

    相关文章

      网友评论

        本文标题:button(selected、enabled、……)相关内容

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