Ios 如何从superview获取所有元素(如:UiLabel、UITextfield),并设置标签文本颜色和文本字段占位符颜色
我无法设置UITextfield占位符颜色和UIlabel文本颜色Ios 如何从superview获取所有元素(如:UiLabel、UITextfield),并设置标签文本颜色和文本字段占位符颜色,ios,iphone,swift,uiview,uilabel,Ios,Iphone,Swift,Uiview,Uilabel,我无法设置UITextfield占位符颜色和UIlabel文本颜色 我们可以在给定的屏幕中看到我需要什么 下面是我用来标识UILabel和UITextfield的代码 问题是UIExtField占位符和UIButton文本进入UILabel循环,并将UIExtField占位符颜色更改为与UILabel文本颜色相同的颜色 您需要遍历所有子视图并检查相应的类型以更改其属性 for subview in view.subviews { if let textField = subv
您需要遍历所有子视图并检查相应的类型以更改其属性
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
}
}
您需要遍历所有子视图并检查相应的类型以更改其属性
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
}
}
您应该在else条件下调用
processSubviewsNight(of:subview)
。否则,textfield的子视图将传递到此方法
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)
}
}
}
您应该在else条件下调用
processSubviewsNight(of:subview)
。否则,textfield的子视图将传递到此方法
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)
}
}
}
请尝试使用else if{}循环而不是if{}打印子视图的描述:。。。。。。文本“输入您的任务”是UITextfield的占位符,但此文本进入UILabel loopTry,否则如果{}循环而不是{}打印子视图的描述:。。。。。。文本“输入您的任务”是UITextfield的占位符,但该文本进入UILabel循环。。。。。。文本“输入您的任务”是UITextfield的占位符,但此文本进入UILabel循环。实际上占位符标签包含在UITextfield中,因此您可以使用
\u placeholderLabel.textColor
…获取并更改颜色。。。。。。文本“输入您的任务”是UITextfield的占位符,但此文本进入UILabel循环。实际上占位符标签包含在UITextfield中,因此您可以使用\u placeholderLabel.textColor
…获取并更改颜色。