我有一个自定义的UITextField子类,它有一个@IBInspectable属性来环绕文本字段的上下角。在Interface Builder中一切都正常工作,绕过正确的角等等,但是当我在device上(或在模拟器中)运行应用程序时,它忽略了我的自定义类,只显示默认的UITextField实现(我在IB中将borderStyle设置为none)。
这是我的定制课程:

@IBDesignable
class RoundedCornersTextField: UITextField {

    @IBInspectable var roundBottom: Bool = false
    @IBInspectable var cornerRadius: CGFloat = 22

    override func textRectForBounds(bounds: CGRect) -> CGRect {
        return CGRectInset(bounds, 22, 0)
    }

    override func editingRectForBounds(bounds: CGRect) -> CGRect {
        return CGRectInset(bounds, 22, 0)
    }

    override func drawRect(rect: CGRect) {
        super.drawRect(rect)

        let cornerRadii: CGSize = CGSize(width: cornerRadius, height: cornerRadius)

        let topBounds = CGRect(x: 0, y: 0, width: rect.width, height: rect.height / 2)
        let bottomBounds = CGRect(x: 0, y: rect.height / 2, width: rect.width, height: rect.height / 2)

        let topPathRoundedCorners: UIRectCorner = roundBottom ? [] : [.TopLeft, .TopRight]
        let bottomPathRoundedCorners: UIRectCorner = roundBottom ? [.BottomLeft, .BottomRight] : []

        let topPath = UIBezierPath(roundedRect: topBounds, byRoundingCorners: topPathRoundedCorners, cornerRadii: cornerRadii)
        let bottomPath = UIBezierPath(roundedRect: bottomBounds, byRoundingCorners: bottomPathRoundedCorners, cornerRadii: cornerRadii)

        topPath.appendPath(bottomPath)

        let layer = CAShapeLayer()

        layer.path = topPath.CGPath
        layer.bounds = rect
        layer.position = self.center
        layer.fillColor = UIColor.whiteColor().CGColor
        layer.lineWidth = 0
        layer.strokeColor = UIColor.clearColor().CGColor

        self.layer.insertSublayer(layer, atIndex: 0)

    }
}

最佳答案

不知道你是否还在搜索答案当我试图用leftview图标创建自定义文本字段时我遇到了同样的问题这段代码完成了我所有的工作。几乎可以用代码替换两个init中的所有代码

import UIKit
@IBDesignable
class IconTextField: UITextField {
var imageView:UIImageView?
@IBInspectable var image: UIImage? {
    didSet { imageView?.image = image }
}
override init(frame: CGRect) {
    super.init(frame: frame)
    let rootView = UIView(frame:CGRect(x:0,y:0,width:45,height:25))
    self.imageView = UIImageView(frame:CGRect(x:10,y:0,width:20,height:20))
    self.imageView?.tintColor = UIColor.red
    self.imageView?.image = image
    rootView.addSubview(self.imageView!)
    self.leftView = rootView
    self.leftViewMode = UITextFieldViewMode.always
}

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)!
    let rootView = UIView(frame:CGRect(x:0,y:0,width:45,height:25))
    self.imageView = UIImageView(frame:CGRect(x:10,y:0,width:20,height:20))
    self.imageView?.tintColor = UIColor.red
    self.imageView?.image = image
    rootView.addSubview(self.imageView!)
    self.leftView = rootView
    self.leftViewMode = UITextFieldViewMode.always
}



}

关于ios - iOS-@IBDesignable UITextField子类在IB中正确显示,但在设备上未正确显示,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36128896/

10-14 23:18
查看更多