`
折句号
  • 浏览: 10791 次
文章分类
社区版块
存档分类
最新评论

ios UIButton的创建和使用入门

 
阅读更多

 

UIButton *button  (自己起的名字) = [UIButton buttonWithType:UIButtonTypeRoundedRect];                //定义一个样式为圆角的button
button.frame = CGRectMake(35, 70, 300, 50);  //button的位置
[button setTitle:@"帮订餐" forState:UIControlStateNormal];
//设置button的title
button.titleLabel.font = [UIFont systemFontOfSize: 20.0];
    //设置按钮字体大小
button.backgroundColor = [UIColor whiteColor]; //button的背景颜色
[button.layer setCornerRadius:10.0];
//设置button圆角的半径
[button.layer setBorderWidth:0.5];//设置button边框的宽度

 

 

[button.layer setBorderColor:[UIColor grayColor].CGColor];//设置button边框的颜色

 

 

[self.view addSubview:button];
//显示button

 能够定义的button类型有以下6种,

 

 

  UIButtonTypeCustom = 0, 自定义风格

 

  UIButtonTypeRoundedRect, 圆角矩形

 

  UIButtonTypeDetailDisclosure, 蓝色小箭头按钮,主要做详细说明用

 

  UIButtonTypeInfoLight, 亮色感叹号

 

  UIButtonTypeInfoDark, 暗色感叹号

 

  UIButtonTypeContactAdd, 十字加号按钮

 

 

 

 

 

 

 

forState: 这个参数的作用是定义按钮的文字或图片在何种状态下才会显现

 

 

 

  UIControlStateNormal = 0, 常规状态显现

 

  UIControlStateHighlighted = 1 << 0, 高亮状态显现

 

  UIControlStateDisabled = 1 << 1, 禁用的状态才会显现

 

  UIControlStateSelected = 1 << 2, 选中状态

 

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

 

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

 

 

 

设置button填充图片和背景图片

  [buttonsetImage:[UIImageimageNamed:@"图片的名字 "]forState:UIControlStateNormal];

 

  [buttonsetBackgroundImage:[UIImageimageNamed:@" 图片的名字"]forState:UIControlStateNormal];

 

定义完按钮需要给按钮添加事件

 

 

[button addTarget:self action:@selector(btnPressed:) //括号内是自己起的名字forControlEvents:UIControlEventTouchUpInside];
//添加点击按钮事件
(target 目标  selector 选择器  event事件)


-(void)btnPressed:(id)sender
{
    HelpViewController *helpView = [[HelpViewController alloc]init];
    [self.navigationController pushViewController:helpView animated:YES];
    //实现点击按钮跳转到helpView
    
}

{}内是点击按钮要实现的动作

 

 

forControlEvents参数类型

 

UIControlEventTouchDown

单点触摸按下事件:用户点触屏幕,或者又有新手指落下的时候。

UIControlEventTouchDownRepeat

多点触摸按下事件,点触计数大于1:用户按下第二、三、或第四根手指的时候。

UIControlEventTouchDragInside

当一次触摸在控件窗口内拖动时。

UIControlEventTouchDragOutside

当一次触摸在控件窗口之外拖动时。

UIControlEventTouchDragEnter

当一次触摸从控件窗口之外拖动到内部时。

UIControlEventTouchDragExit

当一次触摸从控件窗口内部拖动到外部时。
 

UIControlEventTouchUpInside

所有在控件之内触摸抬起事件。

UIControlEventTouchUpOutside

所有在控件之外触摸抬起事件(点触必须开始与控件内部才会发送通知)

UIControlEventTouchCancel

所有触摸取消事件,即一次触摸因为放上了太多手指而被取消,或者被上锁或者电话呼叫打断。

UIControlEventTouchChanged

当控件的值发生改变时,发送通知。用于滑块、分段控件、以及其他取值的控件。你可以配置滑块控件何时发送通知,在滑块被放下时发送,或者在被拖动时发送。

UIControlEventEditingDidBegin

当文本控件中开始编辑时发送通知。

UIControlEventEditingChanged

当文本控件中的文本被改变时发送通知。

UIControlEventEditingDidEnd

当文本控件中编辑结束时发送通知。

UIControlEventEditingDidOnExit

当文本控件内通过按下回车键(或等价行为)结束编辑时,发送通知。

UIControlEventAlltouchEvents

通知所有触摸事件。

UIControlEventAllEditingEvents

通知所有关于文本编辑的事件。

UIControlEventAllEvents

 

通知所有事件。

 

 

批量创建按钮 把创建按钮写成类

 

- (UIButton *)create_button_with_title :(自己起的名字) (NSString *)title :(CGRect)frame
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];//button类型为圆角
    [button setFrame:frame];//button的位置
    [button setTitle:title forState:UIControlStateNormal];//button的名字
    [button addTarget:self action:@selector(btnPressedPersonName:) forControlEvents:UIControlEventTouchUpInside];//button的事件
button.layer.cornerRadius = 10.0;
//圆角的半径
button.layer.borderColor = [UIColor grayColor].CGColor;
//边框的颜色
button.layer.borderWidth = 1.0;
//边框的宽度
[self.view addSubview:button];
    return button;

    
}

 在viewDidload里调用

 

[self create_button_with_title:@"赵大" :CGRectMake(35, 100, 300,50)];
[self create_button_with_title:@"钱二" :CGRectMake(35, 150, 300,50)];

 按钮动作的实现   此例按钮的动作是一样的  所以写一个就可以  动作不一样需要分开写

 

-(void)btnPressedPersonName:(id)sender
{
    UIButton *button = (UIButton *)sender;
    [self.navigationController popViewControllerAnimated:YES];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"persRset" object:button.titleLabel.text userInfo:nil];
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics