gpt4 book ai didi

ios - 如何用 subview 隐藏 UITextField 文本?

转载 作者:行者123 更新时间:2023-11-29 01:03:28 25 4
gpt4 key购买 nike

我需要用 subview 隐藏 UITextField 的文本属性。这看起来很简单,但不幸的是它不起作用。我做错了什么?

代码如下:

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 ...

或者您只需更改标签的 z 顺序,它也能正常工作。

    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)

关于ios - 如何用 subview 隐藏 UITextField 文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36718585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com