简体   繁体   English

为什么我的自定义UITextField在Interface Builder上与在设备上运行时看起来有所不同?

[英]Why does my custom UITextField look like different at Interface Builder vs in running on device?

Hi everyone I'm a bit new to swift . 大家好,我有点新手。

I wanna subclass UITextField and add extra features in order to create my own new TextField , I've modified TextField's border by creating aa new instance of CALayer to create a new underline border so 我想子类化UITextField并添加额外的功能以创建自己的新TextField,我通过创建CALayer的新实例以创建新的下划线边框来修改TextField的边框,因此

I'm running problem with my textfield that is my TextField looks like different in InterfaceBuild VS when running on device as shown below in photo ? 我的文本字段正在运行问题,这是我的TextField在设备上运行时在InterfaceBuild VS中看起来像不同,如下图所示?

I've tried to called setborder func in prepareForInterfaceBuilder but that didn't work for me so my first question is what should i do to fix this problem ? 我试图在prepareForInterfaceBuilder中调用setborder func,但这对我不起作用,所以我的第一个问题是我应该怎么做才能解决此问题?

the second what is the difference between calling my func setborder in init func vs draw func ? 第二个在init func中调用我的func setborder与draw func有什么区别?

TextFieldPhoto attributes inspector thanks in advance TextFieldPhoto 属性检查器预先感谢

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

}



hi i am getting expected output in interface builder, without changing any code given by you. 嗨,我正在界面构建器中获得预期的输出,而无需更改您给定的任何代码。

please check attached image 请检查所附图片 界面图片

and For getting the same please check below image of attribute inspector has Up to date status as shown in image 为了获得相同的效果,请检查下面的属性检查器图像是否具有最新状态,如图所示

最新属性

if you don't get Up To date status then please select Editor -> Refresh All Views from menu 如果您没有获得最新状态,请从菜单中选择编辑器->刷新所有视图 菜单选择

also make sure you have deselected few option to see expected o/p from Editor -> Canvas -> Show Layout Rectangles and Show Bound Rectangles 还请确保已取消选择一些选项,以从“编辑器”->“画布”->“显示布局矩形”和“显示边界矩形”中查看预期的o / p
在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在iOS 6和iOS 7上运行时,为什么我的应用看起来不同? - Why does my app look different when running against iOS 6 vs iOS 7? 在Interface Builder中呈现自定义UITextField子类的错误 - Errors with rendering custom UITextField subclass in Interface Builder 带有图像和表视图的视图在电话上看起来不一样,就像在“界面”构建器上一样 - View with image and tableview does not look the same on phone like it look on Interface builder iOS在界面构建器中设置自定义字体在设备上不起作用 - iOS setting custom font in interface builder does not work on device 为什么自定义 UIButton 图像不会在 Interface Builder 中调整大小? - Why does a custom UIButton image does not resize in Interface Builder? 为什么我的应用在多任务抽屉中看起来像这样? - Why does my app look like this in the multitasking drawer? 如何使uitextfield看起来像在塌陷? - How can I make my uitextfield look like it is caving in? 与界面生成器和模拟器相比,为什么UILabel换行符在设备上的不同位置? - Why are UILabel line breaks at different places on the device compared to interface builder and the simulator? 为什么我的应用程序在我的设备上与模拟器上如此不同? - Why is my app so different on my device vs. the simulator? Xcode 6界面构建器不显示自定义类 - Xcode 6 interface builder does not show custom class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM