Swift - 按钮(UIButton)的用法

1,按钮的创建

(1)按钮有下面四种类型:
UIButtonType.ContactAdd:前面带“+”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果
UIButtonType.DetailDisclosure:前面带“!”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果
UIButtonType.System:前面不带图标,默认文字颜色为蓝色,有触摸时的高亮效果
UIButtonType.Custom:定制按钮,前面不带图标,默认文字颜色为白色,无触摸时的高亮效果
UIButtonType.InfoDark:为感叹号“!”圆形按钮
UIButtonType.InfoLight:为感叹号“!”圆形按钮
1
2
3
4
5
6
7
//创建一个ContactAdd类型的按钮
let  button: UIButton  UIButton (type:. ContactAdd )
//设置按钮位置和大小
button.frame= CGRectMake (10, 150, 100, 30)
//设置按钮文字
button.setTitle( "按钮" , forState: UIControlState . Normal )
self .view.addSubview(button);
(2)对于Custom定制类型按钮,代码可简化为:
1
let  button =  UIButton (frame: CGRectMake (10, 150, 100, 30))

2,按钮的文字设置
1
2
3
button.setTitle( "普通状态" , forState: UIControlState . Normal //普通状态下的文字
button.setTitle( "触摸状态" , forState: UIControlState . Highlighted //触摸状态下的文字
button.setTitle( "禁用状态" , forState: UIControlState . Disabled //禁用状态下的文字

3,按钮文字颜色的设置
1
2
3
button.setTitleColor( UIColor .blackColor(),forState: . Normal //普通状态下文字的颜色
button.setTitleColor( UIColor .greenColor(),forState: . Highlighted //触摸状态下文字的颜色
button.setTitleColor( UIColor .grayColor(),forState: . Disabled //禁用状态下文字的颜色

4,按钮文字阴影颜色的设置
1
2
3
button.setTitleShadowColor( UIColor .greenColor(),forState:. Normal //普通状态下文字阴影的颜色
button.setTitleShadowColor( UIColor .yellowColor(),forState:. Highlighted //普通状态下文字阴影的颜色
button.setTitleShadowColor( UIColor .grayColor(),forState:. Disabled //普通状态下文字阴影的颜色

5,按钮背景颜色设置
1
button.backgroundColor= UIColor .blackColor()

6,按钮文字图标的设置
1
2
3
button.setImage( UIImage (named: "icon1" ),forState:. Normal )   //设置图标
button.adjustsImageWhenHighlighted= false  //使触摸模式下按钮也不会变暗
button.adjustsImageWhenDisabled= false  //使禁用模式下按钮也不会变暗

7,设置按钮背景图片
1
button.setBackgroundImage( UIImage (named: "background1" ),forState:. Normal )

8,按钮触摸点击事件响应
1
2
3
4
5
6
7
8
9
10
11
//不传递触摸对象(即点击的按钮)
button.addTarget( self ,action: Selector ( "tapped" ),forControlEvents:. TouchUpInside )
func  tapped(){
    print ( "tapped" )
}
 
//传递触摸对象(即点击的按钮),需要在定义action参数时,方法名称后面带上冒号
button.addTarget( self ,action: Selector ( "tapped:" ),forControlEvents:. TouchUpInside )
func  tapped(button: UIButton ){
    print (button.titleForState(. Normal ))
}
常用的触摸事件类型:
TouchDown:单点触摸按下事件,点触屏幕
TouchDownRepeat:多点触摸按下事件,点触计数大于1,按下第2、3或第4根手指的时候
TouchDragInside:触摸在控件内拖动时
TouchDragOutside:触摸在控件外拖动时
TouchDragEnter:触摸从控件之外拖动到内部时
TouchDragExit:触摸从控件内部拖动到外部时
TouchUpInside:在控件之内触摸并抬起事件
TouchUpOutside:在控件之外触摸抬起事件
TouchCancel:触摸取消事件,即一次触摸因为放上太多手指而被取消,或者电话打断

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 iOS 应用开发中,通常使用 UINavigationController 来管理多个页面的转换。在使用 UINavigationController 的情况下,你可以通过调用它的 pushViewController(_:animated:) 方法来转换到另一个页面。 举个例子,假设你有一个名为 FirstViewController 的视图控制器,并且要通过按钮点击事件来转换到另一个名为 SecondViewController 的视图控制器。 首先,需要在你的故事板中创建两个场景。并将场景关联到对应的视图控制器。 其次,在 FirstViewController 中添加一个按钮,并在它的点击事件中,调用 UINavigationController 的 pushViewController(_:animated:) 方法,将 SecondViewController 压入导航栈中 ``` swift import UIKit class FirstViewController: UIViewController { @IBOutlet weak var transitionButton: UIButton! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } @IBAction func transitionButtonDidTapped(_ sender: Any) { let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController self.navigationController?.pushViewController(secondViewController, animated: true) } } ``` 这样你就可以通过按钮点击来在两个场景间转换了。 需要注意的是,需要在你的 Appdelegate 里面配置 Navigation 并且设置第一个页面是哪一个. ```swift let firstViewController = self.storyboard?.instantiateViewController(withIdentifier: "FirstViewController") as! FirstViewController let navigationController = UINavigationController(rootViewController: firstViewController) self.window?.rootViewController = navigationController

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值