如何用子视图隐藏UITextField文本?

时间:2016-04-19 12:35:17

标签: ios swift uitextfield uilabel

我需要使用子视图隐藏UITextField的text属性。 这似乎很简单,但不幸的是它不起作用。 我做错了什么?

以下是代码:

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)
}

3 个答案:

答案 0 :(得分:1)

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

或者你只是改变了lable的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)

答案 1 :(得分:0)

addSubview UITextField看起来不可能。相反,使用可以将其添加到view文本字段。

答案 2 :(得分:0)

这对我有用:

//input in form
<input type='hidden' name='new_dateTime' value='' />


// Datepicker / Timepicker jQuery Script
$('#datetimepicker1').datetimepicker({
    format : "YYYY-MM-DD"}
    ).on("dp.change", function (e) {
        datetimepicker1 = $('#datetimepicker1').val();
$('[name = new_dateTime]').val(datetimepicker1);
    });

使用循环检查。

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)


}