UIbutton作为简单且频繁被使用的控件,其地位在整个app开发中不可小视。在IB中使用button相对来说很简单,很容易上手,但在多视图开发中button通常会被即时生成,并设置其属性和功能,即通过代码生成。如下是我对button的理解。

  //创建圆角button

     UIButton *button = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];      

  //指定button的位置和大小

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

  //给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];

 

使用技巧:

button不仅可以用来加载图片还可以加载label来实现特定文本的显示。

UIButton的详细使用

UIButton的详细使用

[button addSubview:label];

相关文章:

  • 2022-12-23
  • 2021-07-20
  • 2021-11-27
  • 2021-04-01
  • 2021-04-14
  • 2022-01-19
猜你喜欢
  • 2022-12-23
  • 2022-02-23
  • 2021-12-26
  • 2022-12-23
  • 2021-06-22
相关资源
相似解决方案