UIButton常用方法

1、常用属性

 第一、UIButton的定义

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

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

  typedef enum {

  UIButtonTypeCustom = 0, 自定义风格

  UIButtonTypeRoundedRect, 圆角矩形

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

  UIButtonTypeInfoLight, 亮色感叹号

  UIButtonTypeInfoDark, 暗色感叹号

  UIButtonTypeContactAdd, 十字加号按钮

  } UIButtonType;

  第二、设置frame

  button1.frame = CGRectMake(20, 20, 280, 40);

  [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 为内部框架预留,可以不管

  };

  @property(nonatomic,getter=isEnabled)BOOL enabled; // default is YES. if NO, ignores touch events and subclasses may draw differently

  @property(nonatomic,getter=isSelected)BOOL selected; // default is NO may be used by some subclasses or by application

  @property(nonatomic,getter=isHighlighted)BOOL highlighted;

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

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

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

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

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

  [buttonsetTitleColor:[UIColorredColor]forState:UIControlStateNormal];

  第七、设置按钮按下会发光

  button.showsTouchWhenHighlighted=NO;

  第八、添加或删除事件处理

  [button1 addTarget:self action:@selector(butClick:) 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; // 标题间距

 

 

 

2、UIButton自己尚未验证的用法 

 

UIButton setImage中,无法控制将图片贴进的那个imageview的大小,系统会像cell.imageview那样,这个imageview的大小会随着图片的大小来定。这个时候,可以只提供一张二倍的图片

 

例如:

 

[btn setImage:[UIImageimageNamed:@"icon.png"]

 

forState:UIControlStateNormal];

 

1、如果工程中给一张icon@2x.png图片便可。此时,iphone4中会自动用icon@2x.png这张图片,大小自然为2倍图片的一半;iphone3gs首先会去找1倍的图片,如果只找到2倍的图片,则显示的图片大小:2倍图片的一半(不是很确定)。

 

2、如果工程中只提供一张icon.png图片。此时,iphone4显示的图片大小为一倍图片大小,但会出现毛边的情况

 

posted @ 2015-10-18 10:21  寒林落木日西斜  阅读(239)  评论(0编辑  收藏  举报