Change Button Background Color Using Swift Language

Change button background color when selected SWIFT

You could store the outlets inside an array. To do so, watch the following video: https://www.youtube.com/watch?v=bFfPT37Yh8c

After that you will have to create one action for all of your buttons (here it's called buttonPressToggle).

@IBAction func buttonPressToggle(_ sender: UIButton) {
//buttons -> your outlet collection
for btn in buttons {
if btn == sender {
btn.backgroundColor = .white
} else {
btn.backgroundColor = .gray
}
}
}

Or even shorter:

@IBAction func buttonPressToggle(_ sender: UIButton) {
self.buttons.forEach { $0.backgroundColor = ($0 == sender) ? UIColor.white : UIColor.gray }
}

Swift: Change UIButton backgroundColor when button is pressed?

You can try

// add this code snippet outside of any class 
extension UIButton {
func shortChangeTo(_ color:UIColor) {
let prev = self.backgroundColor
self.backgroundColor = color
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
self.backgroundColor = prev
}
}
}

to use it

if userAnswer == actualAnswer {
sender.shortChangeTo(.green)
} else {
sender.shortChangeTo(.red)
}

And change

updateUI()

to

DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
self.updateUI()
}

Change background color of multiple buttons on SwiftUI

You need to have one variable didTap for each Button. This can be achieved by moving the button to a separate view.

You can create this view:

struct MyButton: View {

@State private var didTap = false // This will change the color

let myRow: String // I'm assuming it's a String, otherwise use the correct type

@Binding var isNight: Bool // This will change the variable in the parent view
@Binding var variableTitle: String // This will change the variable in the parent view (always assuming it's a String)

var body: some View {
Button{
didTap.toggle()
variableTitle = myRow
isNight.toggle()
} label:{
ULD(title: myRow, textColor: .black, backgroundColor: didTap ? .red : .green)
}
}
}

Then, in your parent view just call it as per the following example. Remember that isNight and variableTitle must both be a @State variable in the parent view.

ForEach(allWords, id: \.self) { myRow in
MyButton(myRow: myRow, isNight: $isNight, variableTitle: $variableTitle)
}

How set background color of a button - xcode

Just scroll a little bit in Attribute Inspector, you can find Background property in view section that will allow you set the backgroundColor for the UIButton.

Sample Image

If you want to set backgroundColor of button programatically.

Objective C :

 self.yourButton.backgroundColor = [UIColor redColor];

Swift 3 and Swift 4

 self.yourButton.backgroundColor = UIColor.red

Swift 2.3 or lower

 self.yourButton.backgroundColor = UIColor.redColor()

Change other view's background color by button state

You can use Swift's new framework called Combine to subscribe and listen to changes of this property. You will also need to import Combine.

Here is an example

import Combine
import UIKit

@IBOutlet weak var button: UIButton!
var subscriptions = Set<AnyCancellable>()

override func viewDidLoad() {
super.viewDidLoad()
button
.publisher(for: \.isHighlighted)
.sink { highlighted in
if highlighted {
// do whatever you need here
}
}
.store(in: &subscriptions)
}

Here is the full documentation from Apple if you want to use Combine

swift how to switch background color when button is pressed

Try this:

@IBAction func didTapButton() 
{
self.view.backgroundColor = UIColor.clear // <-- reset initial background outside animation block

// then start the animation
UIView.animate(withDuration: 1/25, delay: 0.0, options:[.autoreverse, .repeat, .allowUserInteraction], animations: { [weak self] in
self?.view.backgroundColor = self?.colors.randomElement()
}, completion: nil)
}

i.e. just set the backgroundColor to a random color once in the animation block.

Use autoreverse to return the color to the original (clear or transparent in this example).

Use the repeat option to repeat the animation, forever.

Use the allowUserInteraction to allow user interaction during the animation.

"I would also like to have the message disappear when the button is pressed" ... not sure what this is referring to.



Related Topics



Leave a reply



Submit