
mport UIKitclass VIEwController: UIVIEwController { overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() //创建button类型:system,custom,contactAdd,(detaildisclosure,infoDark,infolight效果一样),roundedRect let button = UIbutton(type: .custom) //设置button位置 button.frame = CGRect(x: 150,y: 250,wIDth: 100,height: 100) //设置按钮文字 button.setTitle("按钮",for: .highlighted)//有三种状态:普通状态:normal,触摸状态:hightlighted禁用状态:Disabled //设置按钮文字 button.setTitlecolor(UIcolor.black,for: .normal)//有三种状态 //设置按钮文字阴影 button.setTitleShadowcolor(UIcolor.gray,for: .normal) //设置按钮字体和大小 button.TitleLabel?.Font = UIFont.systemFont(ofSize:11)//系统字体 button.TitleLabel?.Font = UIFont(name: "Farah",size: 11)//自定义字体 //设置按钮背景颜色 button.backgroundcolor = UIcolor.orange //设置按图案(如果自定义图标就不需要设置button类型) button.setimage(UIImage(named:"icon"),for: .normal) button.adjustsImageWhenHighlighted = false//使触摸模式下按钮图标也不会变半透明 button.adjustsImageWhenDisabled = false//是触摸模式下按钮图标也不会变半透明 //保留图案本色(当button类型为custom时无需次代码) let image = UIImage(named: "icon")?.withRenderingMode(.alwaysOriginal) button.setimage(image,for: .normal) //设置按钮背景图片 button.setBackgroundImage(UIImage(named: "Image"),for: .normal) //按钮文字过长处理方法 button.TitleLabel?.lineBreakMode = .byClipPing//其他处理方法详情请看Label章节 //按钮触摸点击事件 button.addTarget(self,action: #selector(VIEwController.Tapbutton),for: .touchUpInsIDe)// touchDown:单点触摸按下事件,点触屏幕// touchDownRepeat:多点触摸按下事件// touchDragInsIDe:触摸在控件内拖动时// touchDragOutsIDe:触摸在控件外拖动时// touchdragenter:触摸从控件之外拖动到内部时// touchDragExit:触摸从控件内部拖动到外部时// touchUpInsIDe:在控件之内触摸并抬起事件// touchUpOutsIDe:在控件之外触摸抬起事件// touchCancel:触摸取消事件,即一次触摸因为放上太多手指而被取消,或者电话打断 //添加button到视图中 self.vIEw.addSubvIEw(button) } @objc func Tapbutton() { print("你点击了此按钮") } } 总结 以上是内存溢出为你收集整理的Swift - UIButton全部内容,希望文章能够帮你解决Swift - UIButton所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)