文本字段中的 Border 和 Padding 样式不适用于 swift5

The Border and Padding styles in the text field do not apply in swift5

我目前正在从事 iOS 项目。我的问题是 textfield 中的任何内容都没有改变我。很难找到问题的答案。我该如何更改?

import Foundation
import UIKit

class passwrodCheckAlertController : UIViewController {


    @IBOutlet weak var passModalAlert: UITextView!

    @IBOutlet weak var passTextfield: UITextField!
    @IBOutlet weak var cancelbutton: UIButton!
    @IBOutlet weak var okButton: UIButton!



//    let color = UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.5)
    let color = UIColor.red
    let insets = UIEdgeInsets.init(top: 31.7, left: 0.0, bottom: 0.0, right: 0.0)

    override func viewDidLoad() {
        super.viewDidLoad()
        self.passModalAlert.layer.cornerRadius = 8
        self.passTextfield.layer.cornerRadius = 3
        self.passTextfield.layer.borderColor = color.cgColor
        self.passModalAlert.textContainerInset = insets
        changeViewFont()
    }

}

class PaddingTextField : UITextField {

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func editingRect(forBounds bounds: CGRect) -> CGRect {
        bounds.inset(by: UIEdgeInsets(top: 0, left: 100, bottom: 0, right: 15))
        return bounds
    }

}

MainStoryboard

画面不变

CornerRadius 在文本视图中效果很好。

提前致谢


编辑开始

我听了评论,用了leftview。差距问题就解决了。但是我的Border风格还是没变。

        let paddingView = UIView(frame: CGRect.init(x: 0, y: 0, width: 10, height: self.passTextfield.frame.height))
        self.passTextfield.leftView = paddingView
        self.passTextfield.leftViewMode = .always
        self.passTextfield.layer.cornerRadius = 3
        self.passTextfield.layer.borderColor = color.cgColor
        self.passModalAlert.textContainerInset = insets

Border style doesn't change.


编辑第二个开始

@IBOutlet weak var passTextfield: PaddingTextField!
...
class PaddingTextField : UITextField {

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func awakeFromNib() {
        //set your border style here
        self.layer.cornerRadius = 3
        self.layer.borderColor = UIColor.red.cgColor
    }

    override func editingRect(forBounds bounds: CGRect) -> CGRect {
        bounds.inset(by: UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 15))
        return bounds
    }

}

But this has not changed anything changed.

您的问题似乎是您在创建插座时的设置问题,您应该 select PaddingTextField 而不是 UITextField,因为 UITextField 是一个超类,并且对其子项具有的附加功能一无所知。

删除 IBOutlet

@IBOutlet weak var passTextfield: UITextField!

并重新创建它应该如下所示:

@IBOutlet weak var passTextfield: PaddingTextField!

希望这对您有所帮助。

关于边界:

class PaddingTextField : UITextField {

   override func awakeFromNib() {
       //set your border style here
       self.layer.cornerRadius = 3
       // Add borderWidth as otherwise you are having a 0 point wide border
       self.layer.borderWidth = 5 
       self.layer.borderColor = UIColor.red.cgColor
   }

   override func editingRect(forBounds bounds: CGRect) -> CGRect {
       bounds.inset(by: UIEdgeInsets(top: 0, left: 100, bottom: 0, right: 15))
       return bounds
   }

}

按预期运行: