Ios Swift:按下按钮后更改按钮颜色
我正在尝试在iPhone上制作一个基本的应用程序。我对Xcode完全陌生,我在谷歌上浏览了一下,找不到我想要的东西。我正在为我的飞机做一份清单 我基本上有一个视图控制器,上面有几个按钮。我想要的是按钮一旦按下就从灰色变为蓝色,除非再次按下,否则保持蓝色。我根本不懂Xcode和Swift语言,所以如果有人能帮我解释的话,请像向孩子解释一样解释 到目前为止,我已经设法让所有的页面设置和滚动工作,但我只需要现在改变按钮 提前谢谢大家,,Ios Swift:按下按钮后更改按钮颜色,ios,iphone,swift,button,Ios,Iphone,Swift,Button,我正在尝试在iPhone上制作一个基本的应用程序。我对Xcode完全陌生,我在谷歌上浏览了一下,找不到我想要的东西。我正在为我的飞机做一份清单 我基本上有一个视图控制器,上面有几个按钮。我想要的是按钮一旦按下就从灰色变为蓝色,除非再次按下,否则保持蓝色。我根本不懂Xcode和Swift语言,所以如果有人能帮我解释的话,请像向孩子解释一样解释 到目前为止,我已经设法让所有的页面设置和滚动工作,但我只需要现在改变按钮 提前谢谢大家,, Ty.使用UIButton类的此方法 func setTitle
Ty.使用UIButton类的此方法
func setTitleColor(_ color: UIColor?,
forState state: UIControlState)
您正在查找的状态为。已选定
当然,您必须自己处理所选状态,因为切换按钮不是一种原生行为。这非常简单
首先,在ViewDidLoad()方法中实际设置按钮的背景色。在此之前,您应该已经为按钮设置了IBOutlet。在我的例子中,我只是将插座命名为一个通用的“按钮”
您的IBOutlet将是:
@IBOutlet weak var button:UIButton!
别忘了把插座连到按钮上
接下来,在ViewDidLoad中将按钮的背景图像设置为灰色,如下所示:
override func viewDidLoad() {
super.viewDidLoad()
button.backgroundColor = UIColor.grayColor()
}
@IBAction func buttonTapped(sender: AnyObject) {
if button.backgroundColor == UIColor.grayColor() {
button.backgroundColor = UIColor.blueColor()
}
else if button.backgroundColor == UIColor.blueColor() {
button.backgroundColor = UIColor.grayColor()
}
}
这应该给你的按钮一个通用的灰色背景。如果您想对实际颜色进行更多控制,可以将按钮.backgroundColor=UIColor.grayColor()
替换为按钮.backgroundColor=UIColor(红色:CGFloat,绿色:CGFloat,蓝色:CGFloat,alpha:CGFloat)
,将“CGFloat”替换为您选择的值
接下来,更改按钮颜色的实际代码:
制作一个iAction并将其连接到按钮。我的名字叫:
@IBAction func buttonTapped(sender: AnyObject) {}
您的条件是,如果按钮为灰色,则点击时按钮应变为蓝色,如果按钮为蓝色,则点击时按钮应变为灰色
因此,在我们刚刚定义的iAction中,您将添加一个If语句,如下所示:
override func viewDidLoad() {
super.viewDidLoad()
button.backgroundColor = UIColor.grayColor()
}
@IBAction func buttonTapped(sender: AnyObject) {
if button.backgroundColor == UIColor.grayColor() {
button.backgroundColor = UIColor.blueColor()
}
else if button.backgroundColor == UIColor.blueColor() {
button.backgroundColor = UIColor.grayColor()
}
}
这个if语句所做的是,如果按钮的背景颜色是灰色的,则将其设置为蓝色;如果背景颜色是蓝色的,则将其设置为灰色。如果需要,您可以将UIColor.BlueColor()
或UIColor.GrayColor()
替换为更个性化的UIColor(红色:CGFloat、绿色:CGFloat、蓝色:CGFloat、alpha:CGFloat)
编辑:如果您只想更改按钮的文本颜色,只需在所需位置添加此语句或其变体:
button.setTitleColor(UIColor.grayColor(), forState: UIControlState.Selected)
对于Swift 3:
// for change background color of a selected button in a collection view
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let collectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
collectionCell.button.addTarget(self, action: #selector(button_Select(_:)), for: .touchUpInside) // add target for action
return collectionCell
}
@objc func button_Select(_ sender: UIButton) {
if sender.isSelected == true {
sender.backgroundColor = UIColor.uicolorFromHex(0x262D35, alpha: 100)
sender.setTitleColor(UIColor.white, for: .normal)
sender.isSelected = false
} else {
sender.backgroundColor = UIColor.uicolorFromHex(0xF3CD70, alpha: 100)//Choose your colour here
sender.setTitleColor(UIColor.white, for: .normal) //To change button Title colour .. check your button Tint color is clear_color..
sender.isSelected = true
}
}
没有CollectionView:
//MARK: - UIButton Action
@IBAction func colorChangingBtn(_ sender: UIButton){
sender.isSelected = !sender.isSelected
if sender.isSelected{
sender.backgroundColor = UIColor.gray
sender.setTitleColor(UIColor.blue, for: .normal)
}
else{
sender.backgroundColor = UIColor.blue
sender.setTitleColor(UIColor.white, for: .normal)
}
}
更新SWIFT 5 记住在选择状态时也调用颜色
@IBAction func yourBtnAction(_ sender: UIButton) {
sender.isSelected = !sender.isSelected
sender.tintColor = .clear
if sender.isSelected{
sender.setTitleColor(.green, for: .selected)
}
else{
sender.setTitleColor(.red, for: .selected)
}
}
当用户将手指放在按钮上时,状态将被突出显示,例如:
button.setTitleColor(UIColor.white.withAlphaComponent(0.5),例如:。突出显示)