Adding UITextField on UIView Programmatically Swift

前端 未结 9 1256
不知归路
不知归路 2021-01-30 06:42

I\'m new to Swift and iOS programming. I was trying to create a UITextField programmatically using Swift but couldn\'t get it quite right. Also how do you change it

9条回答
  •  囚心锁ツ
    2021-01-30 07:20

    self.addSubview(myTextField)
    

    Could work only, if you're working with the subclass of UIView .

    You have to add it as a subview of the UIView of the ViewController's hierarchy.

    var txtField: UITextField = UITextField(frame: CGRect(x: 0, y: 0, width: 500.00, height: 30.00));
    self.view.addSubview(txtField)
    

    By default background colour is white, it just might not appear. You need to provide the style as well as text colour and background colour.

    txtField.borderStyle = UITextBorderStyle.Line
    txtField.text = "myString" 
    txtField.backgroundColor = UIColor.redColor()
    

    For more info, here

提交回复
热议问题