首页 > 移动平台 > 详细

ios基础篇(三)——UIButton的详细介绍

时间:2015-12-09 13:50:51      阅读:249      评论:0      收藏:0      [点我收藏+]

按钮UIButton是ios开发中最常见的控件之一,下面来介绍UIButton的详细内容:

 一、UIButton的定义

      UIButton *button=[[UIButton buttonWithType:(UIButtonType);

          能够定义的button类型有以下6种,

          typedef enum {

                               UIButtonTypeCustom = 0, 自定义风格

                               UIButtonTypeRoundedRect, 圆角矩形

                               UIButtonTypeDetailDisclosure, 蓝色小箭头按钮,主要做详细说明用

                               UIButtonTypeInfoLight, 亮色感叹号

                               UIButtonTypeInfoDark, 暗色感叹号

                               UIButtonTypeContactAdd, 十字加号按钮

                               } UIButtonType;

二、设置frame

         button.frame = CGRectMake(0, 0, 100, 50);

       [button setFrame:CGRectMake(20,20,50,50)];

三、button背景色

       button1.backgroundColor = [UIColor clearColor];

        [button setBackgroundColor:[UIColor blueColor]];

四、state状态

        forState: 这个参数的作用是定义按钮的文字或图片在何种状态下才会显现

      enum {

                 UIControlStateNormal = 0, 常规状态显现

                   UIControlStateHighlighted = 1 << 0, 高亮状态显现

                   UIControlStateDisabled = 1 << 1, 禁用的状态才会显现

                   UIControlStateSelected = 1 << 2, 选中状态

                   UIControlStateApplication = 0x00FF0000, 当应用程序标志时

                 UIControlStateReserved = 0xFF000000 为内部框架预留,可以不管

                  };

五 、设置button填充图片和背景图片

       [buttonsetImage:[UIImageimageNamed:@"checkmarkControllerIcon"]forState:UIControlStateNormal];

     [buttonsetBackgroundImage:[UIImage imageNamed:@"checkmarkControllerIcon"]forState:UIControlStateNormal];

六、设置button标题和标题颜色

    [button1 setTitle:@"点击" forState:UIControlStateNormal];

      [buttonsetTitleColor:[UIColorredColor]forState:UIControlStateNormal];

七、设置按钮按下会发光

      button.showsTouchWhenHighlighted=NO;

八、添加或删除事件处理

      [button  addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

    [btn removeTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];

九、设置按钮内部图片间距和标题间距

      UIEdgeInsets insets; // 设置按钮内部图片间距

    insets.top = insets.bottom = insets.right = insets.left = 10;

      bt.contentEdgeInsets = insets;

      bt.titleEdgeInsets = insets; // 标题间距

技术分享

 

//初始化
    myButton = [[UIButton alloc] initWithFrame:(CGRect){0,50,100,80}];
    [self.view addSubview:myButton];
    //背景颜色
    myButton.backgroundColor = [UIColor blueColor];
    //圆角
    myButton.layer.cornerRadius = 25;
    myButton.layer.masksToBounds = YES;
    //高亮
    myButton.showsTouchWhenHighlighted = NO;
    //设置图片,状态
    [myButton setImage:[UIImage imageNamed:@"buttonImage"] forState:UIControlStateNormal];
    [myButton setImage:[UIImage imageNamed:@"selectedImage"] forState:UIControlStateHighlighted];
    //添加标题
    [myButton setTitle:@"Button" forState:UIControlStateNormal];
    //标题颜色
    [myButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    //点击事件
    [myButton addTarget:self action:@selector(Action) forControlEvents:UIControlEventTouchUpInside];
    

 

ios基础篇(三)——UIButton的详细介绍

原文:http://www.cnblogs.com/0320y/p/5032505.html

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