iosswiftuibuttonuipopovertintcolor

How to avoid button tint color change when presenting a popover in iOS?


I have a button that show a popover over fullscreen, the main view has a 6 custom views that have 5 button with a tint color in grey or blue, depending of some parameter. but when the popover appears al the button inside the customs view become gray, as soon that popover disappear the custom view get is tint color, I want to avoid that when the popover is presented the button tint conlor does not change. the custom view is a view inside a UITableviewCell. the custom view is define as this.

 class ratingDoors: UIView {
    var rating: Int = 0{
        didSet{
            makeRating()
        }
    }

    @IBOutlet var contentView: UIView!
    @IBOutlet weak var button1: UIButton!
    @IBOutlet weak var button2: UIButton!
    @IBOutlet weak var button3: UIButton!
    @IBOutlet weak var button4: UIButton!
    @IBOutlet weak var button5: UIButton!

    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commonInit()
        //fatalError("init(coder:) has not been implemented")
    }
    private func commonInit() {
        //TODO: Do some stuff
        // 1. Load the nib
        Bundle.main.loadNibNamed("ratingDoors", owner: self, options: nil)

        // 2. Add the 'contentView' to self
        addSubview(contentView)
        contentView.frame = self.bounds
        contentView.autoresizingMask = [.flexibleHeight,.flexibleWidth]
    }

    @IBAction func setRating(sender: UIButton){
        rating = sender.tag
        //makeRating()
    }
    func makeRating() {
        button1.configureRatingButton(i: rating)
        button2.configureRatingButton(i: rating)
        button3.configureRatingButton(i: rating)
        button4.configureRatingButton(i: rating)
        button5.configureRatingButton(i: rating)
    }
    }

    extension UIButton{
        func configureRatingButton(i: Int){
            if self.tag <= i {
                self.tintColor = UIColor(red: 90/255, green: 212/255, blue: 227/255, alpha: 1.0)
            }else{
                self.tintColor = UIColor(red: 204/255, green: 204/255, blue: 204/255, alpha: 1)
            }
        }
    }

in the tableview i have this setup for the cell

let cell = tableView.dequeueReusableCell(withIdentifier: "ratingCell") as! ratingTableViewCell
        cell.ratingLabel.text = "Ordenado"
            if let ordered = requestManager.instance.user.ordered{
                cell.ratingView.rating = ordered
            }

        return cell

and so for the all 6 rows with the same view. here is the prepare(for:)

let popoverVC = segue.destination as! popoverViewController
        popoverVC.delegate = self
        popoverVC.modalPresentationStyle = UIModalPresentationStyle.popover
        popoverVC.popoverPresentationController!.delegate = self

the initial configuration for tview is like this. initial view state

Here is the view with the popover presented. the buttons in gray collor when popover presented so how avoid the tint color change in the popover presntation?


Solution

  • If you set each button’s tintAdjustmentMode to .normal, they will not adjust their tint upon presenting the popover. Another option could be to use a Custom button type (instead of System) that doesn’t respond to tint changes.