VsMaX
VsMaX

Reputation: 1795

UIButton doesn't change text color when pressed

UIButton created programatically doesn't change color when pressed, as all other buttons created using Interface Builder.

My button is created as follows:

        let cableDetailsButton = UIButton(type: UIButtonType.System)
        cableDetailsButton.frame = CGRectMake(8, 8, 42, 21)
        cableDetailsButton.setTitle("Title", forState: UIControlState.Normal)
        cableDetailsButton.setTitleColor(self.view.tintColor, forState: .Normal)
        cableDetailsButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Highlighted)
        cableDetailsButton.addTarget(self, action: #selector(FuseDetailsViewController.cableButtonPressed), forControlEvents: .TouchUpInside)

I would like to add that tintColor is default iOS color (the blue one)

My goal is to create programatically the same (default!) button as the one drag&dropped from the list in Interaface Builder. I want my button to change color/alpha upon pressing.

I am using Swift 2.3

Unfortunately such simple task overwhelmed me to the extent to which I have to ask for help. Thank you in advance for help.

UPDATE: Button is part of UISCrollView

Upvotes: 4

Views: 6259

Answers (4)

Developer Sheldon
Developer Sheldon

Reputation: 2160

  1. implement a flag in your view controller

    var buttonSelected = false
    
  2. keep your code for your button but remove one line

    let cableDetailsButton = UIButton(type: UIButtonType.System)
    cableDetailsButton.frame = CGRectMake(8, 8, 42, 21)
    cableDetailsButton.setTitle("Title", forState: UIControlState.Normal)
    cableDetailsButton.setTitleColor(self.view.tintColor, forState: .Normal)
    cableDetailsButton.addTarget(self, action: #selector(FuseDetailsViewController.cableButtonPressed), forControlEvents: .TouchUpInside)
    
  3. in your targeted function add the following

    func cableButtonPressed() {
        if buttonSelected {
           cableDetailsButton.setTitleColor(self.view.tintColor, forState: .Normal)
           buttonSelected = !buttonSelected
           //do whatever else you need to do
        } else {
           cableDetailsButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
           buttonSelected = !buttonSelected
           //do whatever else you need to do
        }
        //do whatever else you need to do
    }
    

Upvotes: 0

Vinodh
Vinodh

Reputation: 5268

There are 2 approaches to highlight button

Approach 1

   let cableDetailsButton = UIButton(type: UIButtonType.System)
    cableDetailsButton.frame = CGRectMake(20, 50, self.view.frame.size.width - 32, 50)
    cableDetailsButton.setTitle("Title", forState: UIControlState.Normal)
    cableDetailsButton.addTarget(self, action: #selector(cableButtonPressed(_:)), forControlEvents: UIControlEvents.TouchUpInside)
    cableDetailsButton.backgroundColor = UIColor.whiteColor()
    self.view.addSubview(cableDetailsButton)

    func cableButtonPressed(sender: UIButton){
        dispatch_async(dispatch_get_main_queue(), {

            if self.isHighLighted == false{
                sender.highlighted = true;
                self.isHighLighted = true
            }else{
                sender.highlighted = false;
                self.isHighLighted = false
            }
        });
    }

Approach 2

    let cableDetailsButton = UIButton(type: UIButtonType.System)
    cableDetailsButton.frame = CGRectMake(20, 50, self.view.frame.size.width - 32, 50)
    cableDetailsButton.setTitle("Title", forState: UIControlState.Normal)
    cableDetailsButton.setTitleColor(UIColor.greenColor(), forState: UIControlState.Normal)
    cableDetailsButton.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Highlighted)
    cableDetailsButton.addTarget(self, action: #selector(cableButtonPressed(_:)), forControlEvents: UIControlEvents.TouchUpInside)
    self.view.addSubview(cableDetailsButton)


  func cableButtonPressed(sender: UIButton){
        if sender.selected {
            // set selected
            sender.selected = true
        } else {
            // set deselected
            sender.selected = false
        }

    }

Upvotes: 0

Ajil O.
Ajil O.

Reputation: 6892

This is working for me.

    let cableDetailsButton = UIButton(type: .Custom)
    cableDetailsButton.frame = CGRect(x: 68, y: 68, width: 100, height: 70)
    cableDetailsButton.setTitle("Some button", forState: .Normal)
    cableDetailsButton.setTitleColor(self.view.tintColor, forState: .Normal)
    cableDetailsButton.setTitleColor(UIColor.blackColor(), forState: .Highlighted)
    view.addSubview(cableDetailsButton)

Or I did not completely understand what your question was.

Upvotes: 0

Annie Gupta
Annie Gupta

Reputation: 2822

Instead of

let cableDetailsButton = UIButton(type: UIButtonType.system)

you need to change the UIButtonType as .custom

let cableDetailsButton = UIButton(type: UIButtonType.custom)

Also the syntax is incorrect if you are working on Swift3. Find the corrected syntax below

let cableDetailsButton = UIButton(type: UIButtonType.custom)
cableDetailsButton.frame = CGRect(x: 8, y: 8, width: 42, height: 21)
cableDetailsButton.setTitle("Dane o przewodach", for: UIControlState.normal)
cableDetailsButton.setTitleColor(self.view.tintColor, for: .normal)
cableDetailsButton.setTitleColor(UIColor.black, for: UIControlState.highlighted)
cableDetailsButton.addTarget(self, action: #selector(self.cableButtonPressed), for: .touchUpInside)

Upvotes: 5

Related Questions