【问题标题】:Why does my custom UITextField look like different at Interface Builder vs in running on device?为什么我的自定义 UITextField 在 Interface Builder 和在设备上运行时看起来不同?
【发布时间】:2019-07-12 14:58:59
【问题描述】:

大家好,我对 swift 有点陌生。

我想继承 UITextField 并添加额外的功能以创建我自己的新 TextField ,我通过创建一个新的 CALayer 实例来修改 TextField 的边框以创建一个新的下划线边框所以

我的文本字段出现问题,即我的 TextField 在设备上运行时在 InterfaceBuild VS 中看起来不同,如下图所示?

我尝试在 prepareForInterfaceBuilder 中调用 setborder 函数,但这对我不起作用 所以我的第一个问题是我应该怎么做才能解决这个问题?

第二个在 init func 和 draw func 中调用我的 func setborder 有什么区别?

TextFieldPhoto attributes inspector 在此先感谢

import FontAwesome_swift

@IBDesignable
class NewUITextField: UITextField {


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

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

    }


    override func leftViewRect(forBounds bounds: CGRect) -> CGRect {

        var txtRec = super.leftViewRect(forBounds: bounds)

        txtRec.origin.x += leftPadding

        return txtRec
    }




    override func textRect(forBounds bounds: CGRect) -> CGRect {

        return bounds.inset(by: UIEdgeInsets(top: 0, left: 35, bottom: 0, right: 0))
    }

    override func placeholderRect(forBounds bounds: CGRect) -> CGRect {

        return bounds.inset(by: UIEdgeInsets(top: 0, left: 35, bottom: 0, right: 0))
    }

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


    @IBInspectable  var leftPadding : CGFloat  = 0


    @IBInspectable  var borderColor :UIColor = UIColor.lightGray {

        didSet{

            setBorder()

        }
    }

    @IBInspectable  var borderWidth : CGFloat = 0.4 {

        didSet{
            setBorder()
        }
    }


    @IBInspectable  var leftImage : UIImage? {

        didSet{
            setleftImage()
        }
    }



    func setBorder()  {


        let border = CALayer()

        let width = CGFloat(self.borderWidth)


        border.borderColor =  self.borderColor.cgColor    //UIColor.lightGray.cgColor

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

        border.borderWidth = width

        self.borderStyle = .none

        self.layer.addSublayer(border)

        self.layer.masksToBounds = true




    }


    func setleftImage()  {

        if let img = leftImage{

            self.leftViewMode = .always

            let imgView = UIImageView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))

            imgView.image = img


            imgView.contentMode = .scaleAspectFit

            imgView.tintColor = UIColor.lightGray

            self.leftView = imgView

        }
        else {

            self.leftViewMode = .never

            self.leftView = nil
        }
    }


    override func prepareForInterfaceBuilder() {
        setBorder()
    }

}



【问题讨论】:

  • 你是说边框吗?请在Interface Builder 中分享您的 textField 属性检查器。
  • @EmreCiftci 是的,我的意思是边框,我已经编辑了帖子以在 Interface Builder 中添加属性检查器
  • @EmreCiftci 感谢第一个问题已经解决了第二个问题呢?

标签: ios swift xcode user-interface


【解决方案1】:

您好,我在界面生成器中得到了预期的输出,无需更改您提供的任何代码。

请查看附件图片

为了得到相同的结果,请检查下面的属性检查器图像是否具有最新状态,如图所示

如果您没有获得最新状态,请从菜单中选择编辑器 -> 刷新所有视图

还要确保您有取消选择几个选项来查看来自编辑器 -> 画布 -> 显示布局矩形和显示绑定矩形的预期 o/p

【讨论】:

  • 我已取消选择(布局矩形和显示绑定矩形)并解决了第一个问题,谢谢您对第二个问题有任何想法吗?
  • 你能告诉我你在哪里覆盖了 prepareForInterfaceBuilder 方法吗?
  • ok 所以对于 prepareForInterfaceBuilder ,Interface Builder 用 IB_DESIGNABLE 属性实例化一个类,它调用这个方法让结果对象知道它是在设计时创建的。您可以在您的可设计类中实现此方法,并使用它来配置它们的设计时外观。因此,如果您对这两个 init 方法都进行了注释,那么它是用于设计时的,那么它将仅在 IB 上工作而不是运行时。更多信息请查看以下链接developer.apple.com/documentation/objectivec/nsobject/…
  • 感谢您的回复,但我的意思是在 init func 和 draw func 中调用我的 func setborder 有什么区别?
猜你喜欢
  • 1970-01-01
  • 2020-11-02
  • 2014-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-02
相关资源
最近更新 更多