{
// 声明按钮是一个对象,是全局的
var button1:UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.title = "六六六";
button1 = UIButton.init(type: UIButtonType.custom)
// button1 = UIButton(type:.custom)
// button1 = UIButton.init(frame: CGRect(x: 10, y: 10, width: 100, height: 100))
button1 = UIButton(type:.custom)
// 默认文字颜色为蓝色,带“!”图标按钮
// button1 = UIButton(type: .detailDisclosure)
// button1 = UIButton(type: .infoDark)
// button1 = UIButton(type: .infoLight)
// button1 = UIButton(type: .contactAdd)
button1.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
button1.backgroundColor = UIColor.white
button1.backgroundColor = UIColor.init(white: 0.9, alpha: 1)
button1.setBackgroundImage(UIImage(named:"num_1"), for: UIControlState.normal) // 普通状态
button1.setBackgroundImage(UIImage(named:"num_2"), for: UIControlState.highlighted) // 高亮状态
button1.setBackgroundImage(UIImage(named:"num_3"), for: UIControlState.selected) // 选中状态
// 设置按钮不同状态下的图片
button1.setImage(UIImage(named:"num_2"), for: UIControlState.normal)
button1.setTitle("普通", for: UIControlState.normal)
button1.setImage(UIImage(named:"num_1"), for: UIControlState.highlighted)
button1.setTitle("高亮", for: UIControlState.highlighted)
button1.setImage(UIImage(named:"num_3"), for: UIControlState.selected)
button1.setTitle("选择", for: UIControlState.selected)
// 设置按钮上文字的阴影
button1.setTitleColor(UIColor.purple, for: UIControlState.normal)
button1.setTitleColor(UIColor.red, for: UIControlState.highlighted)
button1.setTitleColor(UIColor.darkGray, for: UIControlState.selected)
button1.setTitleShadowColor(UIColor.cyan, for: UIControlState.normal)
button1.setTitleShadowColor(UIColor.yellow, for: UIControlState.highlighted)
button1.setTitleShadowColor(UIColor.blue, for: UIControlState.selected)
button1.addTarget(self, action: #selector(buttonClick(button:)), for: UIControlEvents.touchUpInside)
// 按钮内容的边距(顶部、左边、底部、右边
button1.contentEdgeInsets = UIEdgeInsetsMake(100, 0, 30, 0)
// 按钮上图片的边距
button1.imageEdgeInsets = UIEdgeInsetsMake(10, 0, 20, 0)
// 按钮上文本框的边距
button1.titleEdgeInsets = UIEdgeInsetsMake(5, 0, 10, 0)
// 按钮长按状态下去掉文字阴影
button1.reversesTitleShadowWhenHighlighted = true
// 高亮状态下调整图片
button1.adjustsImageWhenHighlighted = true
// 高亮状态下变灰
button1.showsTouchWhenHighlighted = true
// 设置按钮上的文字
button1.titleLabel?.text = "逗比欢乐多";
button1.imageView?.image = UIImage.init(named: "img111");
//也可以按照这样来创建按钮
self.view.addSubview(button1)
}
//按钮的点击事件的处理
func buttonClick(button :UIButton){
//反选效果
button.isSelected = !button.isSelected;
//获取按钮当前文字
print(button.currentTitle)
/*
按钮当前的一些属性,注意:只能读取,不能修改
currentTitle//当前文字
currentTitleColor//当前文字颜色
currentTitleShadowColor//当前文字阴影色
currentImage//当前图片
currentBackgroundImage//当前背景图片
*/
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
iOS swift之Button详解学习
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 这些天一直在忙私事,嘿嘿,今天先提交一个,其中18和19调整label高度的明天测试过后再补上,现在的是OC版本的...
- 一问:什么是线程? 线程是程序执行流的最小单元,也是指进程内的可执行单元,可调度的实体。 二问:线程和进程什么关...
- 引言 UIScrollView的是几个UIKit类包括的UITableView和UITextView中的超类。 一...