How to change all TextField border colour in Swift 3

白昼怎懂夜的黑 提交于 2019-12-12 09:27:33

问题


How to change all TextField border colour in Swift 3 i build one iPad application. i have many TextField in my .xib file and now i want to change border colour but it so many line to write particular textfield so any sort way for this ?


回答1:


Add this extension to create border for all textfields in your project.

extension UITextField
{
    open override func draw(_ rect: CGRect) {
        self.layer.cornerRadius = 3.0
        self.layer.borderWidth = 1.0
        self.layer.borderColor = UIColor.lightGray.cgColor
        self.layer.masksToBounds = true
    }
}



回答2:


extension UITextField {
func cornerRadius(value: CGFloat) {
    self.layer.cornerRadius = value
    self.layer.borderWidth = 1.0
    self.layer.borderColor = UIColor.lightGray.cgColor
    self.layer.masksToBounds = true
}}



回答3:


You should create a new class which is subclass of UITextField as this :

import UIKit

class YourTextField: UITextField {
    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
        self.setBorderColor()
    }
    required override init(frame: CGRect) {
        super.init(frame: frame)
        self.setBorderColor()
    }
    func setBorderColor(){
        self.layer.borderColor = .red // color you want
        self.layer.borderWidth = 3
        // code which is common for all text fields
    }
}

Now open xib select all text fields. In identity inspector, change the custom class to YourTextField This way even you have 1000 text fields in you project, no need to write even one more line for this purpose.



来源:https://stackoverflow.com/questions/43044518/how-to-change-all-textfield-border-colour-in-swift-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!