UIButton的常用方法,属性

实例代码(共两段,分别展示了属性与常用方法,这两段都只是ViewController.m)

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
//创建圆角按钮
- (void) createUIRectButton
{
    //创建一个btn对象,根据类型来创建btn
    //圆角类型btn:UIButtonTypeRoundedRect
    //通过类方法来创建buttonWithType:类名+方法名
    UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    //设置button按钮的位置
    btn.frame = CGRectMake(100, 100, 100, 40);
    //设置按钮的文字内容
    //@parameter
    //P1:字符串类型,显示到按钮上的文字
    //P2:设置文字显示的状态类型:UIControlStateNormal,正常状态
    [btn setTitle:@"按钮01" forState:UIControlStateNormal];

    //P1:显示的文字
    //P2:显示文字的状态:UIControlStateHighlighted,按下状态
    [btn setTitle:@"按钮按下" forState:UIControlStateHighlighted];

    //灰色背景颜色
    btn.backgroundColor = [UIColor grayColor];

    //设置文字显示的颜色
    //P1:颜色
    //P2:状态
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    //设置按下状态的颜色
    [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted];
    //没有设置Highlighted时,就只会显示和普通状态一样的颜色

    //设置按钮的风格颜色,优先度没有前面的高,且没有状态
    [btn setTintColor:[UIColor whiteColor]];

    //titleLabel:UILabel空间
    btn.titleLabel.font = [UIFont systemFontOfSize:12];
    //默认大小为18

    //添加到视图中显示
    [self.view addSubview:btn];
}
- (void) createImageBtn
{
    //创建一个自定义类型的btn
    UIButton* btnImage = [UIButton buttonWithType:UIButtonTypeCustom];

    btnImage.frame = CGRectMake(100, 200, 100, 100);

    UIImage* icon01 = [UIImage imageNamed:@"aa.jpg"];
    UIImage* icon02 = [UIImage imageNamed:@"timg.jpeg"];

    //设置按钮图片设置
    //P1:显示的图片对象
    //P2:控件的状态
    [btnImage setImage:icon01 forState:UIControlStateNormal];
    [btnImage setImage:icon02 forState:UIControlStateHighlighted];

    [self.view addSubview:btnImage];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [self createUIRectButton];
    [self createImageBtn];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void) createBtn
{
    //
    UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    UIButton* btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    UIButton* btn3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    btn.tag = 100;
    btn2.tag = 101;
    btn3.tag = 102;
    btn.frame = CGRectMake(100, 100, 80, 40);

    [btn setTitle:@"按钮" forState:UIControlStateNormal];

    btn2.frame = CGRectMake(100, 140, 80, 40);

    [btn2 setTitle:@"按钮2" forState:UIControlStateNormal];

    btn3.frame = CGRectMake(100, 180, 80, 40);

    [btn3 setTitle:@"按钮3" forState:UIControlStateNormal];
    //向按钮添加事件函数
    //P1:谁来实现那个事件函数,实现对象就是谁
    //P2:@selector(pressBtn):函数对象,当按钮满足P3事件类型,调用函数
    //P3:UIControlEvent:事件处理函数类型
    //UIControlEventTouchUpInside当手指离开屏幕时并且手指的位置在按钮范围内触发事件函数
    //UIControlEventTouchUpOutside当手指离开屏幕时并且手指的位置在按钮范围外触发事件函数
    //UIControlEventTouchDown当手指触碰到屏幕时并且手指的位置在按钮范围内触发事件函数
    [btn addTarget:self action:@selector(pressBtn1:) forControlEvents:UIControlEventTouchUpInside];
    [btn2 addTarget:self action:@selector(pressBtn1:) forControlEvents:UIControlEventTouchUpOutside];
    [btn3 addTarget:self action:@selector(pressBtn1:) forControlEvents:UIControlEventTouchDown];

    [self.view addSubview:btn];
    [self.view addSubview:btn2];
    [self.view addSubview:btn3];
}
- (void) pressBtn1:(UIButton*) btn
{
    if (btn.tag == 100)
    {
         NSLog(@"按钮被按下1");
    }
    if (btn.tag == 101)
    {
        NSLog(@"按钮被按下2");
    }
    if (btn.tag == 102)
    {
        NSLog(@"按钮被按下3");
    }
}
- (void) pressBtn2
{
    NSLog(@"按钮被按下2");
}
- (void) pressBtn3
{
    NSLog(@"按钮被按下3");
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self createBtn];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

心得体会

  1. 首先,虽然我们叫圆角按钮,但现在圆角没有圆角了,就是普通的方形按钮
  2. 对于按钮,基本有文字和状态两个参数
    • 对于状态分为:1. 普通(未按下)2. 按下状态
  3. 在插图片的时候,我不知道为什么只能jpg和jepg对于png图片插不进去,百度的时候,并不能看懂,留给有缘人。
  4. 对于按钮触发事件时,我们又分3种状态
    • UIControlEventTouchUpInside当手指离开屏幕时并且手指的位置在按钮范围内触发事件函数
    • UIControlEventTouchUpOutside当手指离开屏幕时并且手指的位置在按钮范围外触发事件函数
    • UIControlEventTouchDown当手指触碰到屏幕时并且手指的位置在按钮范围内触发事件函数
  5. 在写触发事件函数时,我使用了- (void) pressBtn1:(UIButton*) btn函数,这样自定义函数的好处在于它将自身作为返回值又返回了,这样我们给按钮的tag属性安排一个数字编号(相当于独一无二的身份证)就可以进行判断,对于不同的按钮在一个函数中安排不同的触发,就不用每个都写。

猜你喜欢

转载自blog.csdn.net/kevinashen/article/details/81140436