Change the backgroundcolor of UIButton in IBOutletCollection - ios

I have 16 button. And I want to change background color of button, when button is clicked.
This is my code:
#IBOutlet var buttons: [UIButton]!
#IBAction func button_Taped(_ sender: UIButton) {
}
}
How can I change when clicked the buutton change background color of button

#IBOutlet var buttons: [UIButton]!
#IBAction func button_Taped(_ sender: UIButton) {
sender.backgroundColor = // your desired color
}
}

Related

Press search button to show the searchBar from isHidden to isSelected

How can I make an if statement in swift that when I click the search button as shown in the picture below to show the search bar from isHidden to isSelected
Basically when I press the searchImage the searchBar will show from its initial isHidden to isSelected
pic
#IBAction func searchButton(_ sender: Any) {
}
#IBOutlet weak var searchField: UISearchBar!
I don't know what you need exactly but if you need to switch isHidden or isSelected you can add var to keep the search bar state like:
var isSearchFieldHidden: Bool = false{
didSet{
self.searchField.isHidden = isSearchFieldHidden
}
}
#IBAction func searchButton(_ sender: Any) {
isSearchFieldHidden.toggle()
}
#IBOutlet weak var searchField: UISearchBar!
Try to use
Button(action: {
isHidden.toggle()
}) {
Image("search_icon")
}
!isHidden{
// any view: Button, TextField etc.
your_searchBarView()
}
and read: https://developer.apple.com/documentation/swiftui/text/hidden()#declaration

Using UIButton as a Switch

I am trying to use a UIButton as a switch. When the button is tapped, the button image should change to (atcs.png) and state should be (.selected).
When image is tapped again, the button image should again to (actns.png) and state should be (.normal).
Below is an example of my current code:
#IBOutlet weak var atcBtn: UIButton!
#IBAction func atcTapped(_ sender: UIButton) {
if atcBtn.isSelected {
atcBtn.setImage(UIImage(named: "atcs.png"), for:.selected)
} else {
atcBtn.setImage(UIImage(named: "atcns.png"), for:.normal)
}
}
This somehow isn't working. Is there anything missing? Tried almost every solution listed on here, but nothing.
First, move your setImage code to viewDidLoad. There's no reason to repeatedly set these images:
atcBtn.setImage(UIImage(named: "atcs.png"), for:.selected)
atcBtn.setImage(UIImage(named: "atcns.png"), for:.normal)
Next, toggle the isSelected property when the button is tapped:
#IBAction func atcTapped(_ sender: UIButton) {
sender.isSelected = !sender.isSelected
}
Also of note - use sender instead of hardcoding atcBtn.

How to unhide the button after all switches in ON condition?

I have 2 UISwitch and a Button, in viewDidLoad, I set the button to be hidden and disabled, I want only my button to be not hidden if those 2 switch is in ON state, otherwise, I want my button to hide again. is there any method from UI Switch delegate that can be used ? how do I do that in Swift ?
here is the code I use
import UIKit
class AskingAuthorizationVC: UIViewController {
#IBOutlet weak var locationSwitch: DesignableSwitch!
#IBOutlet weak var notificationSwitch: DesignableSwitch!
#IBOutlet weak var nextButton: DesignableButton!
override func viewDidLoad() {
super.viewDidLoad()
// initial state
nextButton.isHidden = true
nextButton.isEnabled = false
notificationSwitch.isOn = false
locationSwitch.isOn = false
}
#IBAction func signUpButtonDidPressed(_ sender: Any) {
performSegue(withIdentifier: "toAuthenticationVC", sender: nil)
}
}
Hook both of the UISwitch-s as IBActions & IBOutlets
#IBAction func oweSwitch(_ sender: UISwitch) {
self.mybutton.isHidden = !(switch1.isOn && switch2.isOn)
}

How to use one button to add scores for different integers (swift3)

Right now i have a purple and red score. I would like to use the 1 and 2 buttons to add the score for the colors. I know how to do this by creating just a sets of buttons to the specific color but doing this again and again will create problems. So what I am trying to do is if purple is selected and if and only if 1 is pressed then the total score of purple will = 1. I guess what I am trying is perform a switch statement for the buttons.
Before purple/red button is pressed.
After purple/red button is pressed.
import UIKit
class ViewController: UIViewController {
var purpleScore = 0
var redScore = 0
#IBOutlet var plus1: UIButton!
#IBOutlet var plus2: UIButton!
#IBOutlet var addRed: UIButton!
#IBOutlet var addPurlple: UIButton!
#IBOutlet var totalScorePurple: UILabel!
#IBOutlet var totalScoreRed: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
plus1.isHidden = true
plus2.isHidden = true
}
#IBAction func enteringRed(_ sender: Any) {
plus1.isHidden = false
plus2.isHidden = false
}
#IBAction func enteringPurple(_ sender: Any) {
plus1.isHidden = false
plus2.isHidden = false
}
#IBAction func plus1(_ sender: Any) {
}
#IBAction func plus2(_ sender: Any) {
}
}
If I understand correctly as the comment above, this should work
Create 2 Bool for red and purple to see weather they are checked or not, this is easiest way, else you can just check the redButton color got changed (if it does) or .selected = true
var redChecked = false
var purpleChecked = false
#IBAction func enteringRed(_ sender: Any) {
redChecked = !redChecked
//change color etc..
}
#IBAction func enteringPurple(_ sender: Any) {
purpleChecked = !purpleChecked
//change color etc..
}
#IBAction func plus1(_ sender: Any) {
if redChecked {
redScore+=1
}
if purpleChecked {
purpleScore+=1
}
}
#IBAction func plus2(_ sender: Any) {
if redChecked {
redScore+=2
}
if purpleChecked {
purpleScore+=2
}
}

Swift - UIToolbar item selected state

I have this toolbar in my navigation controller. Now what I am trying to do is when the user selects an item (UIBarButtonItem) in my toolbar, have that item highlighted with a background colour until either the user deselects the item or selects another item. How would I do this?
Here are my selector methods for each item of the toolbar, I connected them via storyboard:
#IBAction func addText(sender: AnyObject) {
annotationSelected = 3
}
#IBAction func drawCircle(sender: AnyObject) {
annotationSelected = 1
}
#IBAction func drawRectangle(sender: AnyObject) {
annotationSelected = 2
}
#IBAction func drawStamp(sender: AnyObject) {
annotationSelected = 4
}
This is all I have done. Here is a screenshot of my toolbar:
Here is what I got:
#IBOutlet var textToolButton: UIBarButtonItem!
#IBOutlet var circleToolButton: UIBarButtonItem!
#IBOutlet var rectangleToolButton: UIBarButtonItem!
#IBOutlet var stampToolButton: UIBarButtonItem!
then
textToolButton.target = self
textToolButton.style = .Done
textToolButton.action = #selector(ViewController.barButtonPressed)
let selectedBackgroundColor = UIImage(color: .redColor())
textToolButton.setBackgroundImage(selectedBackgroundColor, forState: UIControlState.Highlighted, style: .Done, barMetrics: UIBarMetrics.Default)
and then the method
func barButtonPressed(sender: UIBarButtonItem) {
print(sender)
annotationSelected = sender.tag
}
background is still not changing color
I find a same question. May be can help you.
custom-pressed-uibarbuttonitem-backgrounds
I find a easy method to do. You can dray a button to the toolBar,and you will see like this.
And you should change the button's type and Image.
storyboard screenshot
then you should link the button to your viewController.
#IBOutlet weak var textToolButton: UIButton!
and you can do.
let selectedBackgroundColor = UIImage(color: .redColor())
textToolButton.setBackgroundImage(selectedBackgroundColor, forState: .Highlighted)
May be I can help you.
The cleanest way you could do it, is create an overall function where you pass in the button that's been selected. Something like this:
var allButtons = [button1, button2, button3, button4]
func resetTabBar (buttonSelected:UIButton) {
for button in allButtons {
if button == buttonSelected {
button.backgroundColor = "Black"
}
else {
button.backgroundColor = "Blue"
}
}
}
And then in your functions you've created, just pass in the sender like so:
#IBAction func addText(sender: AnyObject) {
resetTabBar(sender)
}
Note: This is assuming you have outlets for all of your buttons. If you don't, add them.

Resources