您的位置:首页 > 产品设计 > UI/UE

iOS开发 UIButton 详解

2015-11-21 17:06 405 查看
iOS开发 UIButton 详解
1.UIButton的属性
//button 属性

UIButton *btn = [[UIButton alloc] init];
CGFloat btnX =100;
CGFloat btnY =100;
CGFloat btnW =50;
CGFloat btnH =20;
btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
//设置圆角
btn.layer.cornerRadius = 10;
//设置title
[btn setTitle:@"按钮" forState:UIControlStateNormal];
[btn setTitle:@"On" forState:UIControlStateHighlighted];
//设置title 字体大小
btn.titleLabel.font = [UIFont systemFontOfSize: 14.0];
//设置title 颜色
[btn setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
[btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
//设置文字位置,现设为居左,默认的是居中
// 设置aligment 属性
btn.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
btn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft ;

//设置title自适应对齐
btn.titleLabel.lineBreakMode = UILineBreakModeWordWrap;

//设置背景
[btn setBackgroundImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:@"1"] forState:UIControlStateHighlighted];

//设置文字距离边框的间距
btn.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);
//设置按钮的颜色
btn.backgroundColor =[UIColor redColor];
//action
[btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];

//这里创建一个圆角矩形的按钮
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

//    能够定义的button类型有以下6种,
//    typedef enum {
//        UIButtonTypeCustom = 0,          自定义风格
//        UIButtonTypeRoundedRect,         圆角矩形
//        UIButtonTypeDetailDisclosure,    蓝色小箭头按钮,主要做详细说明用
//        UIButtonTypeInfoLight,           亮色感叹号
//        UIButtonTypeInfoDark,            暗色感叹号
//        UIButtonTypeContactAdd,          十字加号按钮
//    } UIButtonType;

//以下是几种状态
//    enum {
//        UIControlStateNormal       = 0,         常规状态显现
//        UIControlStateHighlighted  = 1 << 0,    高亮状态显现
//        UIControlStateDisabled     = 1 << 1,    禁用的状态才会显现
//        UIControlStateSelected     = 1 << 2,    选中状态
//        UIControlStateApplication  = 0x00FF0000, 当应用程序标志时
//        UIControlStateReserved     = 0xFF000000  为内部框架预留,可以不管他
//    };

/*
* 默认情况下,当按钮高亮的情况下,图像的颜色会被画深一点,如果这下面的这个属性设置为no,
* 那么可以去掉这个功能
*/
btn.adjustsImageWhenHighlighted = NO;
/*跟上面的情况一样,默认情况下,当按钮禁用的时候,图像会被画得深一点,设置NO可以取消设置*/
btn.adjustsImageWhenDisabled = NO;
/* 下面的这个属性设置为yes的状态下,按钮按下会发光*/
btn.showsTouchWhenHighlighted = YES;


2.UIButton 的使用方法
//button 方法
// 用于测试的BOOL
isOn = NO;
if (isOn = !isOn)
{
[button setTitle:@"On" forState:UIControlStateNormal];
[button setTitle:@"On" forState:UIControlStateHighlighted];
[button setBackgroundImage:baseGreen forState:UIControlStateNormal];
[button setBackgroundImage:altGreen forState:UIControlStateHighlighted];
}else{
[button setTitle:@"Off" forState:UIControlStateNormal];
[button setTitle:@"Off" forState:UIControlStateHighlighted];
[button setBackgroundImage:baseRed forState:UIControlStateNormal];
[button setBackgroundImage:altRed forState:UIControlStateHighlighted];
}


3.iOS 设置Button的 初始选中状态
进入页面列表页面的时候可能 会有二级列表,数据默认是二级列表的第一个栏目下的数据,所以,默认情况下第一个 应该是选中的状态iOS <wbr>设置Button的 <wbr>初始选中状态
比如活动秀  就是 这个二级列表的第一个,下面数据就是它的数据,所以他显示的是选中状态 红色的。

btnMutableArray = [[NSMutableArray alloc]init]; //将button放到数组里面
NSMutableDictionary *titleDic = [[NSMutableDictionary alloc]init]; //这个是二级标题的所有信息的字典 首页返回的
for (int i = 0; i < titleMutableArray.count; i++) {
titleDic = [titleMutableArray objectAtIndex:i];
allButton = [[UIButton alloc]initWithFrame:CGRectMake(viewWidth/4*i, 0, viewWidth/4, 35)];
[allButton setTitle:titleDic[@"name"] forState:UIControlStateNormal];
classid = [titleDic[@"id"]intValue];
allButton.tag = classid;
allButton.titleLabel.font = [UIFont systemFontOfSize:14];
[allButton setTitleColor:RGBCOLOR(231, 96, 73) forState:UIControlStateSelected];
[allButton setTitleColor:RGBCOLOR(51, 51, 51) forState:UIControlStateNormal];
allButton.titleLabel.textAlignment = NSTextAlignmentCenter;
[allButton addTarget:self action:@selector(fashionButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[headerScrollerView addSubview:allButton];
[btnMutableArray addObject:allButton];
}
((UIButton *)[btnMutableArray objectAtIndex:0]).selected=YES;  // 关键是这里,设置 数组的第一个button为选中状态

//点击事件
-(void)fashionButtonPressed:(UIButton *)sender{
[myFashionMutableArray removeAllObjects];
[_myfashionTableView reloadData];
((UIButton *)[btnMutableArray objectAtIndex:0]).selected=NO; //点击其他button之后这里设置为非选中状态,否则会出现2个红色的选中状态
if (sender != allButton) {
allButton.selected = NO;
allButton = sender;
} //这个if是网上抄的,有点神奇
allButton.selected = YES;
_fashionpid = sender.tag;
pageSize = 0;
[self loaddataMethodsWithpid:_fashionpid page:pageSize number:Baby_NumofList andSord:0];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: