iOS开发UI— Button简单使用

 

 

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self setTitle:@"按钮基本操作"];
    [self addButton];//添加按钮
    
}
#pragma mark 设置按钮
-(void)addButton{
    //UIButton:对用户的点击操作做出相应,处理用户的点击事件,直接父类则是UIControl
    //1. 初始化按钮
    UIButton *myBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    /**
     UIButtonTypeCustom = 0,  //自定义风格
     UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0),  // standard system button
     
     UIButtonTypeDetailDisclosure,// 蓝色箭头按钮,
     UIButtonTypeInfoLight,// 亮色感叹号
     UIButtonTypeInfoDark,//暗色感叹号
     UIButtonTypeContactAdd,//加号按钮
     
     UIButtonTypeRoundedRect = UIButtonTypeSystem,
     */
    //2.设置Button的frame
    [myBtn setFrame:CGRectMake(100, 100, 60, 30)];
    //3 设置背景色
    [myBtn setBackgroundColor:[UIColor cyanColor]];
    //4 设置文字
    [myBtn setTitle:@"按钮" forState:UIControlStateNormal];
    [myBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];//文字大小
    /**
     UIControlStateNormal = 0 常规状态
     UIControlStateHighlighted = 1 << 0,高亮状态
     UIControlStateDisabled = 1 << 1 ,禁用状态
     UIControlStateSelected = 1 << 2,选中状态
     UIControlStateFocused
     UIControlStateApplication = 0x00FF0000, 当应用程序标志时
     UIControlStateReserved = 0xFF000000 为内部框架预留,可以不管他
     */
    //5 设置文字颜色(默认白色)
    [myBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];//常态下
    [myBtn setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];// 高亮状态下;
    //6 改变文字的位置
    myBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);//根据自己要求随意改变文字的位置
    [myBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];//居中
    [myBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];//右对齐
    [myBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];//左对齐
    //7 设置按钮的图片
    UIImage *image1 = [UIImage imageNamed:@"******"];
    [myBtn setImage:image1 forState:UIControlStateNormal ];//常态下
    UIImage *image2 = [UIImage imageNamed:@"xxxxxx"];
    [myBtn setImage:image2 forState:UIControlStateSelected];//被选中状态下
    
    //8 为按钮添加点击事件--重要内容 action方法,行为  target对象 Control(操控者)
    [myBtn addTarget:self  action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
    
    //9 添加按钮
    [self.view addSubview:myBtn];
    
}

/**
 为按钮添加相应的事件时,事件会带一个参数.参数里面存储的数据指代的是调用addTarget:action: forControlEvents:方法的对象
 */
-(void)clickAction:(UIButton *)sender{
    //selected属性用来记录当前按钮是否处于选中状态
    if (sender.selected == NO) {
        sender.selected = YES;
        
    }else{
        sender.selected = NO;
    }
}

 

  

- (void)viewDidLoad {

    [super viewDidLoad];

    [self.view setBackgroundColor:[UIColor whiteColor]];

    [self setTitle:@"按钮基本操作"];

    [self addButton];//添加Button

    

}

#pragma mark 设置按钮

-(void)addButton{

    //UIButton:对用户的点击操作做出相应,处理用户的点击事件,直接父类则是UIControl

    //1. 初始化按钮

    UIButton *myBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    /**

     UIButtonTypeCustom = 0,  //自定义风格

     UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0),  // standard system button

     

     UIButtonTypeDetailDisclosure,// 蓝色箭头按钮,

     UIButtonTypeInfoLight,// 亮色感叹号

     UIButtonTypeInfoDark,//暗色感叹号

     UIButtonTypeContactAdd,//加号按钮

     

     UIButtonTypeRoundedRect = UIButtonTypeSystem,

     */

    //2.设置Button的frame

    [myBtn setFrame:CGRectMake(100, 100, 60, 30)];

    //3 设置背景色

    [myBtn setBackgroundColor:[UIColor cyanColor]];

    //4 设置文字

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

    [myBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];//文字大小

    /**

     UIControlStateNormal = 0 常规状态

     UIControlStateHighlighted = 1 << 0,高亮状态

     UIControlStateDisabled = 1 << 1 ,禁用状态

     UIControlStateSelected = 1 << 2,选中状态

     UIControlStateFocused

     UIControlStateApplication = 0x00FF0000, 当应用程序标志时

     UIControlStateReserved = 0xFF000000 为内部框架预留,可以不管他

     */

    //5 设置文字颜色(默认白色)

    [myBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];//常态下

    [myBtn setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];// 高亮状态下;

    //6 改变文字的位置

    myBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);//根据自己要求随意改变文字的位置

    [myBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];//居中

    [myBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];//右对齐

    [myBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];//左对齐

    //7 设置按钮的图片

    UIImage *image1 = [UIImage imageNamed:@"******"];

    [myBtn setImage:image1 forState:UIControlStateNormal ];//常态下

    UIImage *image2 = [UIImage imageNamed:@"xxxxxx"];

    [myBtn setImage:image2 forState:UIControlStateSelected];//被选中状态下

    

    //8 为按钮添加点击事件--重要内容 action方法,行为  target对象 Control(操控者)

    [myBtn addTarget:self  action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];

    

    //9 添加按钮

    [self.view addSubview:myBtn];

    

}

posted @ 2016-10-14 11:19  指头飞血  阅读(205)  评论(0)    收藏  举报