ios Swift:按下后更改按钮颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31372684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift: Change a button color once pressed
提问by Ty Burgess
I'm trying to make a basic app my iPhone. I'm completely new to Xcode and I've had a browse on google and could not find exactly what I'm looking for. I'm trying to make a checklist for my plane.
我正在尝试为我的 iPhone 制作一个基本的应用程序。我是 Xcode 的新手,我在谷歌上浏览过,但找不到我要找的东西。我正在尝试为我的飞机制作一份清单。
I've basically got a View controller with several buttons on. And what I want is for the button to change from grey to blue once pressed and stay blue unless pressed again. I have no knowledge of Xcode and Swift language at all so if anyone could help please explain as if you're explaining to a kid.
我基本上有一个带有几个按钮的视图控制器。我想要的是按钮一旦按下就从灰色变为蓝色,除非再次按下,否则保持蓝色。我根本不了解 Xcode 和 Swift 语言,所以如果有人可以帮忙,请像在向孩子解释一样解释。
So far I've managed to get the pages all setup and scrolling working but I just need the buttons to change now.
到目前为止,我已经设法让所有页面设置和滚动工作,但我现在只需要更改按钮。
Thank you in advance, Ty.
提前谢谢你,泰。
回答by MShahmeer
This is pretty simple.
这很简单。
You'll start off by first actually setting up the button's background colour in the ViewDidLoad() method. Before this, you should have already set up an IBOutlet for the button. In my example, I'm just going to name the outlet a generic "button".
您将首先在 ViewDidLoad() 方法中实际设置按钮的背景颜色。在此之前,您应该已经为按钮设置了 IBOutlet。在我的示例中,我只是将插座命名为通用“按钮”。
Your IBOutlet would be:
您的 IBOutlet 将是:
@IBOutlet weak var button:UIButton!
Don't forget to hook up the outlet to the button as well!
不要忘记将插座连接到按钮上!
Next, set the button's background image to gray in ViewDidLoad, like so:
接下来,在 ViewDidLoad 中将按钮的背景图像设置为灰色,如下所示:
override func viewDidLoad() {
super.viewDidLoad()
button.backgroundColor = UIColor.grayColor()
}
This should give your button a generic gray background. If you want more control over the actual colour, you can replace button.backgroundColor = UIColor.grayColor()
with button.backgroundColor = UIColor(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)
, replacing "CGFloat" with the values you choose.
这应该给你的按钮一个通用的灰色背景。如果您想更好地控制实际颜色,您可以替换button.backgroundColor = UIColor.grayColor()
为button.backgroundColor = UIColor(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)
,用您选择的值替换“CGFloat”。
Next, the actual code to change the button colour:
接下来,更改按钮颜色的实际代码:
Make an IBAction and wire it up to the button. Mine is called:
创建一个 IBAction 并将其连接到按钮。我的叫:
@IBAction func buttonTapped(sender: AnyObject) {}
Your condition is that if the button is grey, it should become blue on tap and if it is blue, then it should become gray on tap.
你的条件是,如果按钮是灰色的,它应该在点击时变成蓝色,如果是蓝色,那么它应该在点击时变成灰色。
So, within the IBAction that we just defined, you'll add an If Statement, like so:
因此,在我们刚刚定义的 IBAction 中,您将添加一个 If 语句,如下所示:
@IBAction func buttonTapped(sender: AnyObject) {
if button.backgroundColor == UIColor.grayColor() {
button.backgroundColor = UIColor.blueColor()
}
else if button.backgroundColor == UIColor.blueColor() {
button.backgroundColor = UIColor.grayColor()
}
}
What this if statement does is that if the button's background colour is gray, it sets it to blue, or if the background colour is blue, it sets it to gray. You can replace the UIColor.BlueColor()
or UIColor.GrayColor()
, with a more personalised UIColor(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)
if you want.
if 语句的作用是,如果按钮的背景颜色为灰色,则将其设置为蓝色,或者如果背景颜色为蓝色,则将其设置为灰色。如果需要,您可以将UIColor.BlueColor()
或替换UIColor.GrayColor()
为更个性化的UIColor(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)
。
Edit: If you just want to change text colour of the button, just add this statement or its variations in the required place:
编辑:如果您只想更改按钮的文本颜色,只需在所需位置添加此语句或其变体:
button.setTitleColor(UIColor.grayColor(), forState: UIControlState.Selected)
回答by s1ddok
Use this method of UIButton class
使用 UIButton 类的这个方法
func setTitleColor(_ color: UIColor?,
forState state: UIControlState)
The state you are looking for is .Selected
您正在寻找的状态是 .Selected
And of course you have to handle selected state by yourself as a toggle button is not a native behavior.
当然,您必须自己处理选定状态,因为切换按钮不是本机行为。
回答by rabeeh
For Swift 3:
对于 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) {
let button = sender
if button.isSelected == true {
button.backgroundColor = UIColor.uicolorFromHex(0x262D35, alpha: 100)
button.setTitleColor(UIColor.white, for: .normal)
button.isSelected = false
} else {
button.backgroundColor = UIColor.uicolorFromHex(0xF3CD70, alpha: 100)//Choose your colour here
button.setTitleColor(UIColor.white, for: .normal) //To change button Title colour .. check your button Tint color is clear_color..
button.isSelected = true
}
}
without 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)
}
}