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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38460327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:43:22  来源:igfitidea点击:

How set swift 3 UITextField border color?

iosswiftswift3uitextfield

提问by SwiftDeveloper

Hello i have working no error codes for UITextfield border color change but when using it in Swift 3 dont change textfield border color and dont gives error. I need your help my codes under below.

您好,我没有为 UITextfield 边框颜色更改工作没有错误代码,但是在 Swift 3 中使用它时,不要更改文本字段边框颜色,也不会出错。我需要你的帮助我的代码在下面。

@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


}

Thank you !

谢谢 !

回答by Ketan Parmar

You also need to set border width, because your border color is set already but your default border width is 0.0 so you can't see it.

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

So, set border width something like,

所以,设置边框宽度类似,

  email.layer.borderWidth = 1.0

Update :

更新 :

Your code should be like,

你的代码应该是这样的

 @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

}

回答by Nimisha joshy

Use the below code in swift 3:

在以下代码中使用swift 3

in view did load

鉴于确实加载

 outer_line.layer.borderWidth = 1
 outer_line.layer.borderColor = UIColor.lightGray.cgColor

回答by Ashish Thakkar

Try to use this, It might be helpful to you

尝试使用这个,它可能对你有帮助

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

回答by kamwysoc

I think you should first provide a borderWidth

我认为你应该首先提供一个 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


}

and then set a color :)

然后设置颜色:)

回答by Kiran jadhav

Updated Swift 3 :

更新 Swift 3 :

if you want to set the bottom border to UITextField, used below lines of code :

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

// function defination :

// 函数定义:

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

// In ViewDidLoad() :

// 在 ViewDidLoad() 中:

self.setBottomBorderToTextFields()

回答by Dynamic Methods

It is working.

这是工作。

  1. give border width
  2. and after give bgcolor.
  1. 给边框宽度
  2. 并在给 bgcolor 之后。
txtemail.layer.borderWidth = 1.0
txtemail.layer.borderColor = UIColor.blue.cgColor

It definitely works

它绝对有效

回答by meow2x

You need to set the borderWidthfrom the UITextField's layerproperty.

您需要设置borderWidthfromUITextFieldlayer属性。

Like: email.layer.borderWidth = 1.

喜欢:email.layer.borderWidth = 1

Also, if you frequently need to set borders to your views, you can make an extension like this:

此外,如果您经常需要为视图设置边框,您可以进行如下扩展:

extension UIView {
    func addBorderAndColor(color: UIColor, width: CGFloat, corner_radius: CGFloat = 0, clipsToBounds: Bool = false) {
        self.layer.borderWidth = width
        self.layer.borderColor = color.cgColor
        self.layer.cornerRadius = corner_radius
        self.clipsToBounds = clipsToBounds
    }
}

Call this like: email.addBorderAndColor(color: UIColor.white, width: 0.5, corner_radius: 5, clipsToBounds: true)

像这样调用: email.addBorderAndColor(color: UIColor.white, width: 0.5, corner_radius: 5, clipsToBounds: true)

Since this method sets the borderWidth, it will also solve your problem.

由于此方法设置borderWidth,它也将解决您的问题。