2016-03-21 20 views
1

Metin alanının üst veya alt köşelerini yuvarlaklaştırmak için @IBInspectable özel bir özel UITextField alt sınıfına sahibim. Her şey düzgün bir şekilde Interface Builder'da çalışır, doğru köşeleri yuvarlar ve böyle çalışır, ancak uygulamayı bir device (veya simülatörde) çalıştırdığımda, özel sınıfımı yok sayar ve sadece varsayılan UITextField uygulamasını gösterir (borderStyle hiçbiri olarak ayarlamamış IB).iOS - @IBDesignable UITextField alt sınıfı, IB'de doğru olarak gösteriliyor, ancak cihazda değil

İşte benim özel sınıf var: Hala ben leftview simgesiyle tüm görevimi yaptığım bu kod parçası özel textfield'ı yaratmaya çalıştığı aynı problem vardı cevabı aramaya eğer

@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) 

    } 
} 
+1

Metin alanı arayüz oluşturucusunu oluşturdunuz mu? Değilse, kodu UITextfield – Eric

+0

paylaştığınız yerde kod yazabilirsiniz ve bu metin alanını nasıl başlatıyorsunuz – techloverr

+0

Metin alanı IB'de normal olarak eklenir, kodda başlatılamıyorum. Sınıfı özel sınıfıma ayarladım ve tüm uygun kısıtlamaları oluşturdum. – smeshko

cevap

0

bilmiyorum. Her iki koddaki kodun yerini hemen hemen her kodun yerine değiştirebilirsiniz

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 
} 



} 
İlgili konular