UIButton, 设置button的背景图片

UIButton

UIButton *aButton = [UIButtonbuttonWithType:UIButtonTypeSystem];


    //给按钮设置frame

    aButton.frame =CGRectMake(0,0, 200,80);

    //为按钮设置标题 常用的是NormalHighlighted  标题和图片二选一

    aButton.center =self.window.center;

    [aButton setTitle:@"登录按钮"forState:UIControlStateNormal];

    [aButton setTitle:@"松开结束"forState:UIControlStateHighlighted]; //高亮状态 类似于微信的语音按钮按下去和不按时两种状态

    //为按钮设置图片

//    [aButton setImage:[UIImage imageNamed:@"logo64X64"] forState:UIControlStateNormal];

    //Render渲染  imageWithRenderingMode:  设置图片的渲染模式选择UIImageRenderingModeAlwaysOriginal 原始模式 以原始的图片来显示

    [aButton setImage:[[UIImageimageNamed:@"logo64X64"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]forState:UIControlStateNormal];


    //为按钮对象绑定对应事件类型(如下:UIControlEventTouchUpInside)的对象以及响应方法

    //为按钮添加事件  UIButton是直接继承自UIControl

    //为对象添加行为(对象和方法隶属于同一个类, target一般都是self

    //按钮的两种事件类型 1.UIControlEventTouchUpInside  2.UIControlEventTouchDown

    //按钮的响应参数如果有参数,则参数为当前被点击的参数对象 本句中的点击的参数对象是aButton(当前被点击的按钮)-类型是按钮UIButton

    [aButton addTarget:selfaction:@selector(handleButtonAction:)forControlEvents:UIControlEventTouchDown];

    

    [NSTimerscheduledTimerWithTimeInterval:0.5target:selfselector:@selector(handleButtonAction:)userInfo:nilrepeats:YES];


- (void)handleButtonAction:(UIButton *)sender {

    self.window.backgroundColor = [UIColorcolorWithRed:arc4random() %256 / 255.0green:arc4random() %256 /255.0blue:arc4random() %256 /255.0alpha:1];

    

    //创建一个警告视图

    UIAlertView *aAlertView = [[UIAlertViewalloc] initWithTitle:@"温馨提示"message:@"嘿,你该小心了,嘿嘿嘿"delegate:nilcancelButtonTitle:@"残忍的拒绝"otherButtonTitles:@"确定",nil];

    [aAlertView show];  //警告不是添加在某个父视图上,而是放在最上面

    [aAlertView release];

}


//设置button的背景图片

[aButton setBackgroundImage :buttonImage forState : UIControlStateNormal ];
[aButton setTitle:@" 注册" forState:UIControlStateNormal];

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值