iOS开发基础-UIButton的使用
来源:互联网 发布:nba总决赛欧文数据统计 编辑:程序博客网 时间:2024/06/05 20:30
UIButton的使用
基本使用
1.UIButton创建.frame位置确定
2.文字内容显示
3.UIButton点击事件处理(常用3种状态)
4.事件响应
常用属性方法
5.字体颜色
6.字体大小
7.是否可以点击enable
8.点击时是否高亮showTouchWhenHeighted
9.tag标志button
制作一个custom button(图片按钮定制)
1.使用buttonwithType:创建
2.背景图片设置
3.图片设置
4.点击事件处理
5.加载到界面上
//UIButton:UIControl:UIView//所有事件驱动型的控件,父类都为UIControl//类方法创建一个UIButton type button的样式//iOS7之后,UIButtonTypeRoundedRect 无效果
Button 基本属性设置
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
/*
typedef NS_ENUM(NSInteger, UIButtonType) {
UIButtonTypeCustom = 0, //矩形
UIButtonTypeSystem
UIButtonTypeDetailDisclosure,
UIButtonTypeInfoLight,
UIButtonTypeInfoDark,
UIButtonTypeContactAdd,
//圆角形 ios7UIButtonTypeSystem UIButtonTypeRoundedRect = UIButtonTypeSystem,
};
*/
btn.backgroundColor = [UIColor redColor];//设置button的framebtn.frame = CGRectMake(10,30,300,30);
Button点击显示设置
//设置button处于常态下的标题
[btn setTitle:@”按钮1” forState:UIControlStateNormal];
//当btn被点按,处于高亮状态
[btn setTitle:@”按钮被点按” forState:UIControlStateHighlighted];
/*
typedef NS_OPTIONS(NSUInteger, UIControlState) {
UIControlStateNormal //正常状态 (不点击状态)
UIControlStateHighlighted//点击不松手
UIControlStateDisabled //不可用状态
UIControlStateSelected //选中状态
*/
//设置标题颜色[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];[btn setTitleColor:[UIColor yellowColor] forState:UIControlStateHighlighted];btn.enabled = NO; //是否可用(可以被点击) 默认YES
Button点击事件设置
//事件驱动型控件的体现
//UIControlEventTouchUpInside (按住button,并且在btn的frame范围之内松开手指)
//target-action: 当btn满足特定事件后,会让target对象来执行action方法
//注意:action 方法在target中必须存在,否则,程序会崩溃,方法加上: 会将btn本身作为参数传递过去
[btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
/*
typedef NS_OPTIONS(NSUInteger, UIControlEvents) {
UIControlEventTouchDown //按下
UIControlEventTouchUpInside//点击松开 最常用
UIControlEventValueChanged //值改变
};
*/
//设置标记
btn.tag = 100;
自定义Button背景图片设置
//开发中,UIButton,会加上贴图使用
//如果是.png图片 可以省略后缀 还可以省略@2x @3x
//苹果支持.png .jpg .jpeg 用得最多的是png png图片CPU渲染最快
//UIImage 图片类 (根据图片的名称,得到图片对象)UIImage *image = [UIImage imageNamed:@"1.png"];UIImage *bgImage = [UIImage imageNamed:@"map.png"];//设置图片//setImage setBackgroundImage 区别: 前者的贴图显示在上面,setImage btn很大时,图片不会被拉伸;setBackgroundImage 图片的大小会随着btn的frame而变化[customBtn setImage:image forState:UIControlStateNormal];[customBtn setBackgroundImage:bgImage forState:UIControlStateNormal];customBtn.tag = 101;[customBtn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];//添加到window上[self.window addSubview:customBtn];//1.设置标题阴影颜色[btn setTitleShadowColor:[UIColor grayColor] forState:UIControlStateNormal ];//阴影//2.按钮内容内边距btn.contentEdgeInsets = UIEdgeInsetsZero;//3.按钮标题内边距btn.titleEdgeInsets = UIEdgeInsetsZero;//4.点击时显示高亮btn.showsTouchWhenHighlighted = YES;//5.高亮时自适应图片btn.adjustsImageWhenHighlighted = YES;//6.设置镂空颜色btn.tintColor = [UIColor redColor];//7.某种状态的标题NSLog(@"%@",[btn titleForState:UIControlStateNormal]);//8.某种状态的标题颜色NSLog(@"%@",[btn titleColorForState:UIControlStateNormal]);//9.某种状态的标题阴影颜色NSLog(@"%@",[btn titleShadowColorForState:UIControlStateNormal]);//10.某种状态的标题前景图片NSLog(@"%@",[btn imageForState:UIControlStateNormal]);//11.某种状态的标题背景图片NSLog(@"%@",[btn backgroundImageForState:UIControlStateNormal]);//下面皆为只读属性NSLog(@"%ld",(long)btn.buttonType);//12.按钮样式NSLog(@"%@",btn.currentTitle);//13.按钮当前NSLog(@"%@",btn.currentTitleColor);//14.按钮当前标题颜色NSLog(@"%@",btn.currentTitleShadowColor);//15.按钮当前标题阴影颜色NSLog(@"%@",btn.currentImage);//16.按钮当前前景图片NSLog(@"%@",btn.currentBackgroundImage);//17.按钮当前背景图片NSLog(@"%@",btn.titleLabel);//18.按钮上的标题视图labelNSLog(@"%@",btn.imageView);//19.按钮上的图片视图imageView//以下属性在3.0后弃用//20.设置标题字体大小btn.font = [UIFont systemFontOfSize:12];//21.设置标题截断方式btn.lineBreakMode = NSLineBreakByWordWrapping;//22.设置标题阴影偏移量btn.titleShadowOffset = CGSizeZero;
- iOS开发基础-UIButton的使用
- iOS 开发之UIButton的使用
- iOS开发基础控件--UIButton
- iOS开发基础-UIButton
- iOS开发基础 :UIButton属性
- ios开发-UIButton的学习
- iOS开发基础控件--按钮(UIButton)
- iOS开发之四:常用控件--UIButton的使用
- iOS开发UI系列之UIButton的基本使用
- iOS开发学习笔记之UIButton的使用
- iOS中UIButton的使用
- IOS--UI--UIButton的使用
- ios中UIButton的使用
- 【iOS开发】使用代码创建UIButton
- 【ios开发学习 - 第四课】UIButton使用
- iOS开发从入门到精通--基础组件UIButton基础
- IOS ui基础 uibutton
- IOS开发 设置UIButton的title
- 正则表达式-匹配模式
- Rabbitmq集群高可用
- Tomacat 环境变量的设置
- python2.7:文件编码检测chardet 详解
- 扒一扒HTTPS网站的内幕
- iOS开发基础-UIButton的使用
- C++三大函数(The Big Three)
- 省市二级显示jquery
- java代码解析excel文件(包含日期、小数的处理)
- ibatis 批量插入,批量更新,批量删除,xml与Java代码实现方式
- 2015最流行的Android组件、工具、框架大全
- select选中方法
- Windows Direct3D Intro - Device Lost
- Android使用fitsSystemWindows属性实现--状态栏【status_bar】各版本适配方案