UIButtont特殊样式

UIButton *button = [UIButtonbuttonWithType:UIButtonTypeSystem];

    button.frame =CGRectMake(100, 100, 100, 30);

    //设置显示文本

    //因为按钮有多种状态(Normal,Highlighted,UIControlStateDisabled),需要给不同状态设置文本

    //UIControlState 表示控件状态

    [button setTitle:@"我是按钮,点我啊"forState:UIControlStateNormal];

    [self.viewaddSubview:button];

    

    //按钮添加点击事件处理

    //参数1:传入一个对象执行, 表示那个对象处理事件,一般传入self

    //参数2:传入方法的selector, 表示那个方法处理事件

    //参数3:传入事件类型, 最常用TouchUpInside,参数类型UIControlEvent

    //按下触发事件: UIControlEventTouchDown

    [button addTarget:selfaction:@selector(buttonClicked:)forControlEvents:UIControlEventTouchUpInside];

    

    //2.常用属性

    //设置文本颜色

    [button setTitleColor:[UIColorredColor] forState:UIControlStateNormal];

    

    //设置文本字体

    button.titleLabel.font = [UIFontsystemFontOfSize:12];

    

    //设置圆角矩形按钮

    button.backgroundColor = [UIColorwhiteColor];

    //设置圆角大小

    button.layer.cornerRadius = 10;

    //设置剪切(否则有的时候设置圆角没效果)

    button.clipsToBounds =YES;

    

    //点击位置高亮效果

    button.showsTouchWhenHighlighted =YES;

    

    //禁止点击

    button.enabled =NO;

    

    self.view.backgroundColor = [UIColorlightGrayColor];

    

    //3.图片按钮的使用

    UIButton *imageButton = [UIButtonbuttonWithType:UIButtonTypeCustom];

    imageButton.frame =CGRectMake(100, 200, 150, 30);

    //设置背景图片

    UIImage *image = [UIImageimageNamed:@"back.png"];

    [imageButton setBackgroundImage:imageforState:UIControlStateNormal];

    [self.viewaddSubview:imageButton];

    //设置前景(文本和图片)

    [imageButton setTitle:@"图片安妮"forState:UIControlStateNormal];

    [imageButton setTitleColor:[UIColorblueColor] forState:UIControlStateNormal];

    [imageButton setImage:[UIImageimageNamed:@"city_select.png"]forState:UIControlStateNormal];

    [imageButton addTarget:selfaction:@selector(imageBtnClick)forControlEvents:UIControlEventTouchUpInside];

    

    //调整按钮中文本和图片位置

    // top, left, bottom, right

    imageButton.titleEdgeInsets =UIEdgeInsetsMake(0, -100, 0, 0);

    imageButton.imageEdgeInsets =UIEdgeInsetsMake(0, 80, 0, 0);


;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值