ios-如何设置swift 3 UITextField边框颜色?

您好,我没有为UITextfield边框颜色更改工作任何错误代码,但是在Swift 3中使用它时,请不要更改textfield边框颜色,并且不要给出错误。 我需要您的帮助,下面提供我的代码。

@IBOutlet weak var email: UITextField!
@IBOutlet weak var pass: UITextField!


override func viewDidLoad() {
    super.viewDidLoad()

    let myColor : UIColor = UIColor.white()
    email.layer.borderColor = myColor.cgColor
    pass.layer.borderColor = myColor.cgColor


}

谢谢 !

SwiftDeveloper asked 2020-02-29T09:28:07Z
6个解决方案
95 votes

您还需要设置border width,因为已经设置了边框颜色,但是默认边框宽度为0.0,因此看不到它。

因此,将边框宽度设置为

  email.layer.borderWidth = 1.0

更新:

您的代码应该像

 @IBOutlet weak var email: UITextField!
 @IBOutlet weak var pass: UITextField!

 override func viewDidLoad() {
   super.viewDidLoad()

   let myColor = UIColor.white
   email.layer.borderColor = myColor.cgColor
   pass.layer.borderColor = myColor.cgColor

   email.layer.borderWidth = 1.0
   pass.layer.borderWidth = 1.0

}
Lion answered 2020-02-29T09:28:44Z
11 votes

swift 3中使用以下代码:

鉴于负载

 outer_line.layer.borderWidth = 1
 outer_line.layer.borderColor = UIColor.lightGray.cgColor
Nimisha joshy answered 2020-02-29T09:29:08Z
8 votes

尝试使用这个这可能对您有帮助

let myColor : UIColor = UIColor( red: 0.5, green: 0.5, blue:0, alpha: 1.0 )
myTextField.layer.masksToBounds = true
myTextField.layer.borderColor = myColor.CGColor
myTextField.layer.borderWidth = 2.0
Ashish Thakkar answered 2020-02-29T09:29:28Z
6 votes

我认为您应该首先提供borderWidth

@IBOutlet weak var email: UITextField!
@IBOutlet weak var pass: UITextField!


override func viewDidLoad() {
super.viewDidLoad()

let myColor : UIColor = UIColor.white()
email.layer.borderWidth = 1
email.layer.borderColor = myColor.cgColor
pass.layer.borderColor = myColor.cgColor


}

然后设置颜色:)

kamwysoc answered 2020-02-29T09:29:53Z
5 votes

更新了Swift 3:

如果要将底部边框设置为UITextField,请在以下代码行中使用:

//函数定义:

func setBottomBorderToTextFields()  {

    let bottomLine = CALayer()
    bottomLine.frame = CGRect(x: 0, y: yourTextFieldName.frame.height - 1, width: yourTextFieldName.frame.width, height: 1)
    bottomLine.backgroundColor = UIColor.gray.cgColor // background color
    yourTextFieldName.borderStyle = UITextBorderStyle.none // border style
    yourTextFieldName.layer.addSublayer(bottomLine)
}

//在ViewDidLoad()中:

self.setBottomBorderToTextFields()
Kiran jadhav answered 2020-02-29T09:30:25Z
1 votes

这是工作。

  1. 给出边框宽度
  2. 然后给bgcolor。
txtemail.layer.borderWidth = 1.0
txtemail.layer.borderColor = UIColor.blue.cgColor

绝对有效

Dynamic Methods answered 2020-02-29T09:30:58Z
translate from https://stackoverflow.com:/questions/38460327/how-set-swift-3-uitextfield-border-color