1、UIButton 概述
继承关系:UIButton -> UIControl -> UIView
控件样式:
2、UIButton 初始化
(1)使用 buttonWithType 构建按钮,已有的六种类型如下:
enum UIButtonType : Int {
case Custom // 自定义风格
case System // 圆角矩形
case DetailDisclosure // 蓝色小箭头
case InfoLight // 亮色感叹号
case InfoDark // 暗色感叹号
case ContactAdd // 十字加号
}
(2)使用 frame 自定义按钮
3、使用示例
override func viewDidLoad() {
// 1、使用已有类型构建按钮
let commonButton = UIButton.buttonWithType(UIButtonType.System) as! UIButton
// 修改按钮位置及大小
commonButton.frame = CGRectMake(self.view.frame.width/2 - 100, 100, 200, 200)
// 设置按钮背景图片
commonButton.setBackgroundImage(UIImage(named:"logo.jpg"), forState: UIControlState.Normal)
// 添加点击事件
commonButton.addTarget(self, action: "buttonActions:", forControlEvents: UIControlEvents.TouchUpInside)
// 设置按钮标签
commonButton.tag = 1
// 2、自定义按钮
let customButton = UIButton(frame: CGRectMake(self.view.frame.width/2 - 100, 400, 200, 200))
// 设置按钮标题
customButton.setTitle("custom", forState: UIControlState.Normal)
// 设置按钮标题颜色
customButton.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
// 设置按钮标题阴影
customButton.setTitleShadowColor(UIColor.blackColor(), forState: UIControlState.Normal)
// 设置按钮阴影
customButton.titleLabel?.shadowOffset = CGSizeMake(1.0, 1.0)
// 设置按钮标题字体样式
customButton.titleLabel!.font = UIFont.systemFontOfSize(18)
// 设置按钮标题换行模式
customButton.titleLabel!.lineBreakMode = .ByTruncatingTail
// 设置按钮背景色
customButton.backgroundColor = UIColor(red:0.8,green:0.8,blue:0.8,alpha:1.0)
// 设置按钮内部内容边距
customButton.contentEdgeInsets = UIEdgeInsetsMake(-100, 0, 0, 0)
// 去掉高亮状态下的图像颜色加深
customButton.adjustsImageWhenHighlighted = false;
// 去掉禁用状态下的图像颜色加深
customButton.adjustsImageWhenDisabled = false;
// 添加按钮按下发光效果
customButton.showsTouchWhenHighlighted = true;
// 添加点击事件
customButton.addTarget(self,action:"buttonActions:",forControlEvents:UIControlEvents.TouchUpInside)
// 设置按钮标签
customButton.tag = 2
self.view.addSubview(commonButton)
self.view.addSubview(customButton)
}
/// 响应按钮点击事件
func buttonActions(sender: UIButton!) {
println(sender.tag)
}
4、forState
这个参数的作用是定义按钮的文字或图片在何种状态下才会显现。有以下几种状态:
Normal(正常状态)Highlighted(按下状态)Disabled(禁用状态)Selected(选中状态(手指已经离开))Application(应用程序标志)Reserved(预留状态)
5、两种设置背景图片方式的区别
setBackGroudImage:图片会被拉伸setImage:图片保持原大小
6、结语
Github 上项目地址:UIButtonSample 文章最后更新时间:2015年3月18日10:11:03。参考资料如下:
UIButton Class Reference
UIKit User Interface Catalog: Buttons
- 大小: 22.3 KB
分享到:
相关推荐
swift学习控件篇:UIButton+UILabel
DFCountDownButton,继承UIButton,实现IOS倒计时按钮,用于注册等发送验证码的时候进行倒计时操作
CCAnimationBtn 一个UIButton子类,模仿抖音App中点赞按钮的动画效果
JUT切换按钮扩展 UIButton 的两态切换按钮描述执照麻省理工学院执照作者臼井军
CK倒计时按钮 特征 计数时按钮被禁用 倒计时时按钮自动启用 点击按钮可以自动开始计数 计数时设置自定义背景颜色 支持自定义标题同时计数 例子 要运行示例项目, pod install克隆 repo,然后从 Example 目录运行pod ...
Swift编程-UI控件-UIButton(一)视频教程
这个源码案例是一个不错的Swift编写的Material Design UIButton效果,源码ZMaterialDesignUIButton,Swift编写的Material Design UIButton,希望大家能够喜欢,iOS源码频道。 测试环境:Xcode 6.0,iOS 8.0以上
FSCustomButtonDemo 一个高度自定义UIButton控件,自定义按钮布局样式
Button-RemoveHightlight 移除UIButton的高亮效果(可以设置是否移除)
JTFadingInfoView概述JTFadingInfoView是Google的材料设计,例如基于UIButton子类的具有平滑淡入/淡出动画功能的通知视图。 您可以根据需要选择淡入/淡出方向。 此外,可自定义的属性可以作为行为进行调整,并增强您...
YLButton:自定义按钮,文字图片位置随意定制
SquishButton是UIButton的子类实现按下时会挤压的按钮
圆形 UIButton 子类,带有触摸动画只需初始化并设置颜色、标签、字体、大小和动画类型: DMCircleButton *pulseButton = [DMCircleButton buttonWithLabel: @" Pulse " backgroundColor: [UIColor redColor ] ...
iphone中的基础控件UIButton扩展,自定义UICheckBox, 适合初学者,主要学习自定义UICheckBox的常用属性设置、点击事件、返回所有选中的box索引等
iOS-Shaped-Button-Swift UIButton 子类,仅当用户点击具有 alpha 的按钮部分时才会拦截和触发操作。 在以下屏幕中,有 3 个“重叠”的 UIButton。 通常(因为视图总是矩形)最大或最前面的按钮会“窃取”其他按钮的...
封装了oc和 swift的 2个UIView的虚线外框的类扩展,可以设置线的长度,间隙长度,虚线宽度,虚线颜色,外框圆角边框,一行代码调用非常方便
一个小而灵活的UIButton子类,动画加载进度,完成动画
一个按钮,添加手势,左右滑,给按钮不同的事件。
Swift UIButton 和 UIAlertController详解,改变UIAlertController的字体颜色和字体大小
UIButton + BackgroundColor UIButton的类别或扩展名,用于将纯背景色设置为背景图像。 安装 可可豆 将此添加到您的Podfile pod 'UIButton+BackgroundColor' 用法 导入<UIButton> ,然后可以使用以下命令为您的...