I need to hide text property of the UITextField with subview. It seems quite straightforward, but unfortunately it doesn't work. What do I do wrong?
Here's the code:
override func viewDidLoad() {
super.viewDidLoad()
let txtField = UITextField()
txtField.frame = CGRect(x: 10, y: 50, width: 300, height: 30)
txtField.text = "This text is hidden under UILabel"
txtField.backgroundColor = UIColor.blueColor()
view.addSubview(txtField)
let label = UILabel()
label.frame = CGRect(x: 0, y: 0, width: 300, height: 30)
label.backgroundColor = UIColor.redColor()
label.textColor = UIColor.whiteColor()
label.text = "This text should appear on top"
txtField.addSubview(label)
txtField.bringSubviewToFront(label)
}
let txtField = UITextField()
txtField.frame = CGRect(x: 10, y: 50, width: 300, height: 30)
txtField.text = "This text is hidden under UILabel"
view.addSubview(txtField)
let label = UILabel()
let label = UILabel()
// label.frame = CGRect(x: 0, y: 0, width: 300, height: 30)
label.frame = txtField.frame
label.backgroundColor = UIColor.redColor()
label.textColor = UIColor.whiteColor()
label.text = "This text should appear on top"
label.backgroundColor = UIColor.redColor()
label.textColor = UIColor.whiteColor()
label.text = "This text should appear on top"
view.addSubview(label) // add label on view with same position ...
OR you simply change the z order of lable than also it is working.
let txtField = UITextField()
txtField.frame = CGRect(x: 10, y: 50, width: 300, height: 30)
txtField.backgroundColor = UIColor.yellowColor()
txtField.text = "This text is hidden under UILabel"
view.addSubview(txtField)
let label = UILabel()
label.frame = CGRect(x: 0, y:0, width: 300, height: 30)
label.backgroundColor = UIColor.redColor()
label.textColor = UIColor.whiteColor()
label.text = "This Label should appear on top"
label.layer.zPosition=1
txtField.addSubview(label)
Looks like it's impossible to addSubview
to UITextField
. Instead use can add it to view
over your text field.
This worked for me:
override func viewWillAppear(animated: Bool) {
let sampleTextField = UITextField()
sampleTextField.frame = CGRect(x: 10, y: 50, width: 300, height: 100)
sampleTextField.text = "This text is hidden under UIsampleBackgroundLabel"
sampleTextField.textColor = UIColor.blackColor()
sampleTextField.backgroundColor = UIColor.whiteColor()
view.addSubview(sampleTextField)
let sampleBackgroundLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 300, height: 30))
sampleBackgroundLabel.textColor = UIColor.whiteColor()
sampleBackgroundLabel.text = "This text should appear on top"
sampleTextField.addSubview(sampleBackgroundLabel)
sampleTextField.sendSubviewToBack(sampleBackgroundLabel)
}
Check using a loop.
if(condition == true)
{
sampleBackgroundLabel.textColor = UIColor.whiteColor()
}
else
{
sampleBackgroundLabel.textColor = UIColor.blueColor()
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.