UI控件UIbutton 项目实战

//XCODE有个特点 输入中文以后 后面就不会再有提示了
//如果想要出提示,那么中文先不要出,打完该打完的话 再输中文

内容:
1.UIButton的控件基本概念
2.UIButton的创建方法
3.UIButton的类型
4.可显示图片的UIButton
注意还要添加图片 btn01 btn02

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

//创建普通按钮函数
- (void)createUIRectButton
{
    
    //注意 button一定要通过类方法创建: 类名+方法名
    //创建一个Button对象 根据类型来创建btn
    //参数:buttonWithType:<#(UIButtonType)#> 参数就是类型
    //UIButtonTypeRoundedRect: 圆角类型btn
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    
    //每一个button都是一个可以显示的控件对象
    //用参数 CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>)
    //设置按钮的 位置 与 大小
    //frame:架构
    btn.frame = CGRectMake(100, 100, 100, 40);
    
    //设置按钮的文字内容
    //Button需要显示文字 注意这里没有 .
    //参数:setTitle:<#(nullable NSString *)#> forState:<#(UIControlState)#>
    //@parameter:参数
    //p1:显示到按钮上的文字
    //p2:设置文字显示的状态类型 UIControlStateNormal:正常状态
    [btn setTitle:@"按钮01" forState:UIControlStateNormal];
    
    //按下状态 UIControlStateHighlighted:当按钮保持按下状态时 要改变此时文字就用这个参数
    //默认情况下 设置一个状态 另一种状态 按钮颜色也会变
    [btn setTitle:@"按钮按下" forState:UIControlStateHighlighted];
    
    //设置按钮的背景颜色
    //背景颜色的区域覆盖面积 就是 frame的面积大小
    btn.backgroundColor = [UIColor grayColor];
    
    //设置按钮的文字颜色
    //参数:setTitleColor:<#(nullable UIColor *)#> forState:<#(UIControlState)#>
    //参数1:按钮文字颜色 参数2:按钮状态
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    
    [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted];
    
    //另一种方法 设置按钮的风格颜色
    [btn setTintColor:[UIColor whiteColor]];
    //注意 TintColor的优先级 没有 TitleColor高 所以两个都设置的会TitleColor优先显示
    
    //设置文字的字体大小 font:字体 字形
    //是一个Label空间
    btn.titleLabel.font = [UIFont systemFontOfSize:18];
    
    //添加到视图中 并显示
    [self.view addSubview:btn];
}

//一个可以创建图片的Button按钮
- (void)createImageBtn
{
    //创建一个自定义类型的Button UIButtonTypeCustom 自定义
    //参数:buttonWithType:<#(UIButtonType)#> 用等号链接
    UIButton * btnImage = [UIButton buttonWithType:UIButtonTypeCustom];
    //frame:架构
    btnImage.frame = CGRectMake(100, 200, 100, 100);
    
    //设置加载图片 Image:影像
    //注意 图片要有扩展名
    UIImage * icon01 = [UIImage imageNamed:@"btn01.jpg"];
    
    UIImage * icon02 = [UIImage imageNamed:@"btn02.jpg"];
    
    //设置图片状态
    //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

猜你喜欢

转载自blog.csdn.net/teropk/article/details/81226885