Как получить все элементы (например, UiLabel, UITextfield) из суперпредставления и установить цвет текста метки и цвет заполнителя текстового поля - PullRequest
0 голосов
/ 30 мая 2018

У меня проблема с настройкой цвета заполнителя поля UIText и цвета текста UIlabel

enter image description here

  1. Мы можем видеть, что янужно в данном экране.

Вот код, который я использую для идентификации UILabel и UITextfield.

func processSubviewsNight(of view: UIView) {

        for subview in view.subviews {

            if subview is UITextField {
                if let textField : UITextField = subview as? UITextField {
                    textField.setValue(UIColor.white, forKeyPath: "_placeholderLabel.textColor")
                      textField.backgroundColor = UIColor.appBlueColor()
                }
            }

            if subview is UILabel {
                if let label : UILabel = subview as? UILabel {
                    label.textColor = UIColor.white
                }
            }

            if subview is UIButton {
                if let button : UIButton = subview as? UIButton {
                    button.backgroundColor = UIColor.red
                }
            }
                  processSubviewsNight(of: subview)
           }
    }
Проблема в том, что заполнитель UITextfield и текст UIButton идут внутри цикла UILabel и изменяют цвет заполнителя UITextfield так же, как и цвет текста UIlabel

Ответы [ 2 ]

0 голосов
/ 30 мая 2018

Вы должны позвонить processSubviewsNight(of: subview) в другом состоянии.В противном случае подпредставления текстового поля будут переданы этому методу.

func processSubviewsNight(of view: UIView) {

        for view in self.view.subviews {
            if let lbl = view as? UILabel {
                label.textColor = UIColor.white
            } else if let textField = view as? UITextField {
                textField.setValue(UIColor.white, forKeyPath: "_placeholderLabel.textColor")
                textField.backgroundColor = UIColor.appBlueColor()
            } else if let button = view as? UIButton {
                button.backgroundColor = UIColor.red
            } else{
                processSubviewsNight(of: view)
            }
        }

    }
0 голосов
/ 30 мая 2018

Вам нужно пройти через все подпредставления и проверить соответствующий тип, чтобы изменить его свойства.

for subview in view.subviews {

        if let textField = subview as? UITextFiled {

            textFiled.setValue(UIColor.white, forKeyPath: "_placeholderLabel.textColor")
            textField.backgroundColor = UIColor.appBlueColor()
            //set properties

        } else if let button = subview as? UIButton {

            button.backgroundColor = UIColor.red
            //set properties

        } else if let label = subview as? UILabel {

            label.textColor = UIColor.white
            //set properties
        }
    }
...