如何在iOS中展示弹窗时避免按钮渲染颜色的改变?

14

我有一个按钮,可以在全屏上显示一个弹出窗口。主视图有6个自定义视图,每个自定义视图都有5个带有灰色或蓝色的色调按钮,具体取决于某些参数。但是当弹出窗口出现时,所有自定义视图内的按钮都变成灰色。弹出窗口消失后,自定义视图恢复其色调颜色。我想避免在呈现弹出窗口时按钮色调颜色发生变化。 自定义视图是UITableviewCell中的一个视图,该自定义视图定义如下。

 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)
            }
        }
    }

在tableview中,我为单元格设置了这个设置

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

对于所有6行,视图都是相同的。这里是`prepare(for:)`方法。

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

tview的初始配置如下。 initial view state

这是弹出窗口呈现的视图。 the buttons in gray collor when popover presented 那么如何避免弹出窗口呈现时色彩的变化?


调试您的cellForRowAt方法,以查看为什么评分会变成零。 - Mocha
1个回答

30

如果您将每个按钮的tintAdjustmentMode设置为.normal,则它们在呈现弹出窗口时不会调整其色调。另一个选择是使用自定义按钮类型(而不是系统类型),该按钮类型不会对色调更改作出响应。


这个解决方案在呈现UIAlertController时也适用。奇怪的是,按钮的tintAdjustmentMode已经是.normal,但在初始化后将其设置为.normal必须在其didSet属性观察器中执行一些额外的操作。 - danfordham
设置tintAdjustmentMode可以工作,但仅使用自定义按钮类型不起作用。 - rmp

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接