Brewski
Brewski

Reputation: 684

Customize a button and its highlighted state in Swift3 using a class

I'm new to Swift and I assume this is a fundamental question to programming for iOS.

I have three buttons in my storyboard and I want to customize how those buttons look if pressed once, twice and three times.

I also have three themes (pink, blue and orange). What I thought of doing is to create three new classes called pink,blue and orange.swift

I don't want to create them programmatically, only style them programmatically.

What I lack to understand is how do I call the function (Example: "ButtonIsPressed") from my pink.swift class into my @IBAction and @IBOutlet in the main view controller that is also object oriented (ie. I don't want to create a function for every button)?

I can't really find a decent and up-to-date Swift 3 Tutorial for this, any help or advice on this topic will be greatly appreciated.

Why can it not be as simple as?:

@IBAction func buttonPressed(_ sender: UIButton!) {
    self.backgroundColor = myPinkCGolor
}

Upvotes: 1

Views: 1579

Answers (2)

user7014451
user7014451

Reputation:

I think shallowThought's answer will work for changing backgroundColor based on button state of a specifically named IBOutlet.

I have three buttons in my storyboard and I want to customize how those buttons look if pressed once, twice and three times.

If you want to maintain "state", as in have a "counter" for how many times a button's been clicked or tapped, you can use the "tag" property of the button. Set it to zero, and in your IBAction functions increment it. (Like shallowThought said, use .touchUpInside and .touchDown for the events.)

Also, you have one minor - but important! - thing wrong in your code Brewski:

@IBAction func buttonPressed(_ sender: UIButton!) {
    self.backgroundColor = myPinkCGolor
}

Should be:

@IBAction func buttonPressed(_ sender: UIButton!) {
    sender.backgroundColor = myPinkCGolor
}

So combining everything - up vote to shallowThought (also, changing his AnyObject to UIButton and making it Swift 3.x syntax on the UIColors - and would end up with this. Note that there is no need for an IBOutlet, and you can wire everything up in IB without subclassing:

// .touchUpInside event

// can be adapted to show different color if you want, but is coded to  always show white color

@IBAction func buttonClicked(sender: UIButton) {
    sender.backgroundColor = UIColor.whiteColor()
}

// .touchDown event

// will show a different color based on tap counter

@IBAction func buttonReleased(sender: UIButton) {
    switch sender.tag {
    case 1:
        sender.backgroundColor = UIColor.blue
    case 2:
        sender.backgroundColor = UIColor.red
    case 3:
        sender.backgroundColor = UIColor.green
    default:
        sender.backgroundColor = UIColor.yellow
    }
    sender.tag += 1
}

Upvotes: 1

shallowThought
shallowThought

Reputation: 19602

There is no methode to set the backgroundColor for a certain state, like there is for other UIButton properties, so you have to listen to the buttons actions:

class ViewController: UIViewController {

    @IBOutlet weak var button: UIButton!

    @IBAction func buttonClicked(sender: AnyObject) { //Touch Up Inside action
        button.backgroundColor = UIColor.whiteColor()
    }

    @IBAction func buttonReleased(sender: AnyObject) { //Touch Down action
        button.backgroundColor = UIColor.blueColor()

    }
    ...
}

or set a unicolor image withimage:UIImage, forState:.selected.

Upvotes: 0

Related Questions