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)
}
}
Metin alanı arayüz oluşturucusunu oluşturdunuz mu? Değilse, kodu UITextfield – Eric
paylaştığınız yerde kod yazabilirsiniz ve bu metin alanını nasıl başlatıyorsunuz – techloverr
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