代码改变世界

UIButton的详细使用

2014-12-08 17:26  梦中飞翔的鱼  阅读(193)  评论(0)    收藏  举报

 //创建圆角button

     UIButton *button = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];      

  //指定button的位置和大小

     button.frame = CGRectMake(10, 10, 75, 75);

  //给button设置标签,用来辨别点击的是哪个button,常用在委托方法中。

     button.tag = 0;

  //定义按钮标题

     [button setTitle:@"Button Title" forState:UIControlStateNormal];

  //定义按钮标题字体格式

     [button.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];

  //给button添加委托方法,即点击触发的事件。

     [button addTarget:selfaction:@selector(touchButton1:)  forControlEvents :UIControl EventTouchUp Inside];

  //给button添加图片 

     [button setImage:[UIImageimageNamed:@"blue_bg(2).png"] forState:UIControlStateNormal];

  //将button加入视图当中

     [view addSubview:button];