首页 > 其他 > 详细

UIButton总结

时间:2016-08-13 08:46:57      阅读:199      评论:0      收藏:0      [点我收藏+]

1、初始化

  UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

  [btn setFrame:CGRectMake(100, 100, 60, 60)];

  [self.view addSubview:btn];

2、常用设置

设置文字、文字颜色、图片

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

  [btn setTitleColor:[UIColor brownColor] forState:UIControlStateNormal];

      [optionButton setTitleEdgeInsets:UIEdgeInsetsMake(2, 2, 2, 2)];//文字内边距

  [btn1 setImage:[UIImage imageNamed:@"beer-cap"] forState:UIControlStateNormal];//image会覆盖文字

  [self.imageButton setAdjustsImageWhenDisabled:NO];//自适应图片的大小

设置背景图、背景颜色

  [btn1 setBackgroundImage:[UIImage imageNamed:@"beer-cap"] forState:UIControlStateNormal];

  [btn1 setBackgroundColor:[UIColor blueColor]];//背景图部分透明时配合使用

选中

    [self.cuttentOptionButton setSelected:NO];

设置tag

  [btn1 setTag:21];

  UIButton * btn2 = [self.view viewWithTag:21];//根据tag获取

隐藏

  button.hidden = YES;或  [button isHidden];

    

几个常用的只读属性,用于判断、赋值

  NSString *currentTitle;             // normal/highlighted/selected/disabled. can return nil

  UIColor  *currentTitleColor;        // normal/highlighted/selected/disabled. always returns non-nil. default is white(1,1)

  UIColor  *currentTitleShadowColor;  // normal/highlighted/selected/disabled.

  UIImage  *currentImage;             // normal/highlighted/selected/disabled. can return nil

  UIImage  *currentBackgroundImage;   // normal/highlighted/selected/disabled. can return nil

  NSAttributedString *currentAttributedTitle NS_AVAILABLE_IOS(6_0);  // normal/highlighted/selected/disabled. can return nil

3、添加事件

  [button addTarget:self action:@selector(btnMove:) forControlEvents:UIControlEventTouchUpInside];//带参,参数为按钮本身

UIButton总结

原文:http://www.cnblogs.com/ininin/p/5767163.html

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