Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 具有不连续底边框的自定义UITextfield_Swift_Uitextfield_Uitextinput - Fatal编程技术网

Swift 具有不连续底边框的自定义UITextfield

Swift 具有不连续底边框的自定义UITextfield,swift,uitextfield,uitextinput,Swift,Uitextfield,Uitextinput,我现在正在一个UITextfield上工作。我希望知道如何在UITextfield中添加四个不连续的底部边框,以及如何使输入数字之间的间距变大,使它们分别精确地匹配四行。此外,如果可能,当用户在该行输入数字时,如何使该行变为黑色(而其他行保持灰色)?非常感谢你 检查此.. ViewController.swift import UIKit class ViewController: UIViewController, UITextFieldDelegate { @IBOutlet


我现在正在一个UITextfield上工作。我希望知道如何在UITextfield中添加四个不连续的底部边框,以及如何使输入数字之间的间距变大,使它们分别精确地匹配四行。此外,如果可能,当用户在该行输入数字时,如何使该行变为黑色(而其他行保持灰色)?非常感谢你

检查此..

ViewController.swift

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var txtOne: UITextField!
    @IBOutlet weak var txtTwo: UITextField!
    @IBOutlet weak var txtThree: UITextField!
    @IBOutlet weak var txtFour: UITextField!

    @IBOutlet weak var vwFour: UIView!
    @IBOutlet weak var vwThree: UIView!
    @IBOutlet weak var vwTwo: UIView!
    @IBOutlet weak var vwOne: UIView!


    func textFieldDidBeginEditing(_ textField: UITextField) {
        if textField == txtOne {
            vwOne.backgroundColor = .black
            vwTwo.backgroundColor = .lightGray
            vwThree.backgroundColor = .lightGray
            vwFour.backgroundColor = .lightGray
        } else if textField == txtTwo {
            vwTwo.backgroundColor = .black
            vwOne.backgroundColor = .lightGray
            vwThree.backgroundColor = .lightGray
            vwFour.backgroundColor = .lightGray
        } else if textField == txtThree {
            vwThree.backgroundColor = .black
            vwTwo.backgroundColor = .lightGray
            vwOne.backgroundColor = .lightGray
            vwFour.backgroundColor = .lightGray
        } else {
            vwFour.backgroundColor = .black
            vwTwo.backgroundColor = .lightGray
            vwThree.backgroundColor = .lightGray
            vwOne.backgroundColor = .lightGray
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

}

检查此..

ViewController.swift

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var txtOne: UITextField!
    @IBOutlet weak var txtTwo: UITextField!
    @IBOutlet weak var txtThree: UITextField!
    @IBOutlet weak var txtFour: UITextField!

    @IBOutlet weak var vwFour: UIView!
    @IBOutlet weak var vwThree: UIView!
    @IBOutlet weak var vwTwo: UIView!
    @IBOutlet weak var vwOne: UIView!


    func textFieldDidBeginEditing(_ textField: UITextField) {
        if textField == txtOne {
            vwOne.backgroundColor = .black
            vwTwo.backgroundColor = .lightGray
            vwThree.backgroundColor = .lightGray
            vwFour.backgroundColor = .lightGray
        } else if textField == txtTwo {
            vwTwo.backgroundColor = .black
            vwOne.backgroundColor = .lightGray
            vwThree.backgroundColor = .lightGray
            vwFour.backgroundColor = .lightGray
        } else if textField == txtThree {
            vwThree.backgroundColor = .black
            vwTwo.backgroundColor = .lightGray
            vwOne.backgroundColor = .lightGray
            vwFour.backgroundColor = .lightGray
        } else {
            vwFour.backgroundColor = .black
            vwTwo.backgroundColor = .lightGray
            vwThree.backgroundColor = .lightGray
            vwOne.backgroundColor = .lightGray
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

}

使用UITextField的以下子类,并在故事板中或以编程方式为每个数字创建textfield

请注意,每个文本字段都必须设置一个标记,例如

第一位:
textField1.tag=1

第二位:
textField1.tag=2

第三位:
textField1.tag=3

第四位:
textField1.tag=4

class CustomTextField: UITextField {
    private let normalStateColor = UIColor.lightGray.cgColor
    private let focusStateColor = UIColor.black.cgColor

    private let border = CALayer()
    private let borderHeight: CGFloat = 4.0

    // MARK:- Init
    required init?(coder aDecoder: NSCoder) {
        super.init(coder:aDecoder)
        setup()
    }

    override init(frame:CGRect) {
        super.init(frame:frame)
        setup()
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        setup()
    }
    // MARK:- Overrides
    override func layoutSubviews() {
        super.layoutSubviews()

        let size = self.frame.size
        self.border.frame = CGRect(x: 0, y: size.height - borderHeight, width: size.width, height: borderHeight)
    }

    override func willMove(toSuperview newSuperview: UIView!) {
        guard newSuperview != nil else {
            NotificationCenter.default.removeObserver(self)
            return
        }

        NotificationCenter.default.addObserver(self, selector: #selector(beginEdit),
                                               name: UITextField.textDidBeginEditingNotification, object: self)
        NotificationCenter.default.addObserver(self, selector: #selector(endEdit),
                                               name: UITextField.textDidEndEditingNotification, object: self)
    }

    @objc func beginEdit() {
        border.backgroundColor = self.focusStateColor
    }

    @objc func endEdit() {
        border.backgroundColor = self.normalStateColor
    }

    private func setup() {
        border.backgroundColor = self.normalStateColor
        textAlignment = .center
        borderStyle = .none
        layer.addSublayer(border)
        delegate = self
    }
}

extension CustomTextField: UITextFieldDelegate {

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if textField.text!.count < 1  && string.count > 0 {
            textField.text = string
            textField.superview?.viewWithTag(textField.tag + 1)?.becomeFirstResponder()
            return false
        } else if textField.text!.count >= 1  && string.count == 0 {
            textField.text = ""
            textField.superview?.viewWithTag(textField.tag - 1)?.becomeFirstResponder()
            return false
        }

        return true
    }
}
class CustomTextField:UITextField{
private let normalStateColor=UIColor.lightGray.cgColor
private let focusStateColor=UIColor.black.cgColor
私有let border=CALayer()
私人出租边界高度:CGFloat=4.0
//MARK:-Init
必需的初始化?(编码器aDecoder:NSCoder){
super.init(编码者:aDecoder)
设置()
}
重写初始化(帧:CGRect){
super.init(frame:frame)
设置()
}
重写func awakeFromNib(){
super.awakeFromNib()
设置()
}
//标记:-覆盖
覆盖func布局子视图(){
super.layoutSubviews()
让大小=self.frame.size
self.border.frame=CGRect(x:0,y:size.height-borderHeight,width:size.width,height:borderHeight)
}
重写func willMove(toSuperview newSuperview:UIView!){
guard newSuperview!=无其他{
NotificationCenter.default.removeObserver(自)
返回
}
NotificationCenter.default.addObserver(self,选择器:#选择器(beginEdit),
名称:UITextField.textDidBeginEditingNotification,对象:self)
NotificationCenter.default.addObserver(self,selector:#selector(endEdit),
名称:UITextField.textdideditingnotification,对象:self)
}
@objc func beginEdit(){
border.backgroundColor=self.focusStateColor
}
@objc func endEdit(){
border.backgroundColor=self.normalStateColor
}
专用函数设置(){
border.backgroundColor=self.normalStateColor
textAlignment=.center
borderStyle=.none
layer.addSublayer(边框)
代表=自我
}
}
扩展CustomTextField:UITextFieldDelegate{
func textField(textField:UITextField,shouldChangeCharacters范围:NSRange,replacementString:string)->Bool{
如果textField.text!.count<1&&string.count>0{
textField.text=字符串
textField.superview?.viewWithTag(textField.tag+1)?.becomeFirstResponder()
返回错误
}否则,如果textField.text!.count>=1&&string.count==0{
textField.text=“”
textField.superview?.viewWithTag(textField.tag-1)?.becomeFirstResponder()
返回错误
}
返回真值
}
}
这就产生了


使用UITextField的以下子类,并在故事板中或以编程方式为每个数字创建textfield

请注意,每个文本字段都必须设置一个标记,例如

第一位:
textField1.tag=1

第二位:
textField1.tag=2

第三位:
textField1.tag=3

第四位:
textField1.tag=4

class CustomTextField: UITextField {
    private let normalStateColor = UIColor.lightGray.cgColor
    private let focusStateColor = UIColor.black.cgColor

    private let border = CALayer()
    private let borderHeight: CGFloat = 4.0

    // MARK:- Init
    required init?(coder aDecoder: NSCoder) {
        super.init(coder:aDecoder)
        setup()
    }

    override init(frame:CGRect) {
        super.init(frame:frame)
        setup()
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        setup()
    }
    // MARK:- Overrides
    override func layoutSubviews() {
        super.layoutSubviews()

        let size = self.frame.size
        self.border.frame = CGRect(x: 0, y: size.height - borderHeight, width: size.width, height: borderHeight)
    }

    override func willMove(toSuperview newSuperview: UIView!) {
        guard newSuperview != nil else {
            NotificationCenter.default.removeObserver(self)
            return
        }

        NotificationCenter.default.addObserver(self, selector: #selector(beginEdit),
                                               name: UITextField.textDidBeginEditingNotification, object: self)
        NotificationCenter.default.addObserver(self, selector: #selector(endEdit),
                                               name: UITextField.textDidEndEditingNotification, object: self)
    }

    @objc func beginEdit() {
        border.backgroundColor = self.focusStateColor
    }

    @objc func endEdit() {
        border.backgroundColor = self.normalStateColor
    }

    private func setup() {
        border.backgroundColor = self.normalStateColor
        textAlignment = .center
        borderStyle = .none
        layer.addSublayer(border)
        delegate = self
    }
}

extension CustomTextField: UITextFieldDelegate {

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if textField.text!.count < 1  && string.count > 0 {
            textField.text = string
            textField.superview?.viewWithTag(textField.tag + 1)?.becomeFirstResponder()
            return false
        } else if textField.text!.count >= 1  && string.count == 0 {
            textField.text = ""
            textField.superview?.viewWithTag(textField.tag - 1)?.becomeFirstResponder()
            return false
        }

        return true
    }
}
class CustomTextField:UITextField{
private let normalStateColor=UIColor.lightGray.cgColor
private let focusStateColor=UIColor.black.cgColor
私有let border=CALayer()
私人出租边界高度:CGFloat=4.0
//MARK:-Init
必需的初始化?(编码器aDecoder:NSCoder){
super.init(编码者:aDecoder)
设置()
}
重写初始化(帧:CGRect){
super.init(frame:frame)
设置()
}
重写func awakeFromNib(){
super.awakeFromNib()
设置()
}
//标记:-覆盖
覆盖func布局子视图(){
super.layoutSubviews()
让大小=self.frame.size
self.border.frame=CGRect(x:0,y:size.height-borderHeight,width:size.width,height:borderHeight)
}
重写func willMove(toSuperview newSuperview:UIView!){
guard newSuperview!=无其他{
NotificationCenter.default.removeObserver(自)
返回
}
NotificationCenter.default.addObserver(self,选择器:#选择器(beginEdit),
名称:UITextField.textDidBeginEditingNotification,对象:self)
NotificationCenter.default.addObserver(self,selector:#selector(endEdit),
名称:UITextField.textdideditingnotification,对象:self)
}
@objc func beginEdit(){
border.backgroundColor=self.focusStateColor
}
@objc func endEdit(){
border.backgroundColor=self.normalStateColor
}
专用函数设置(){
border.backgroundColor=self.normalStateColor
textAlignment=.center
borderStyle=.none
layer.addSublayer(边框)
代表=自我
}
}
扩展CustomTextField:UITextFieldDelegate{
func textField(textField:UITextField,shouldChangeCharacters范围:NSRange,replacementString:string)->Bool{
如果textField.text!.count<1&&string.count>0{
textField.text=字符串
textField.superview?.viewWithTag(textField.tag+1)?.becomeFirstResponder()
返回错误
}否则,如果textField.text!.count>=1&&string.count==0{