ios - 如何从超级视图中获取所有元素(如:UiLabel,UITextfield)并设置标签文本颜色和文本字段占位符颜色
问题描述
我在设置 UITextfield 占位符颜色和 UIlabel 文本颜色时遇到问题
- 我们可以在给定的屏幕中看到我需要的东西。
这是我用来识别 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 文本颜色相同
解决方案
您需要浏览所有子视图并检查相应的类型以更改其属性。
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
}
}
推荐阅读
- android - RecyclerView:电影图像不显示
- devops - 我们如何在 Azure DevOps 中禁用测试用例工作项类型
- vba - 出错后循环回输入框
- swift - 从核心数据实体中获取对象检索到错误的对象
- javascript - 等待比那时更快吗?
- android - 为什么 putExtra 没有将正确的值从服务传递到活动?
- mxnet - 在 Android 上运行 GluonCV 对象检测模型
- github - 切换分支并仅与您一起进行特定更改
- logging - 在 Grails 中配置日志级别服务器
- c# - Angular Material中asp Listbox类型功能的控件是什么?