Swift'de programlı olarak UIView'ın özel kenar rengini ayarlamaya çalışıyorum.
Swift'de programlı olarak UIView'ın özel kenar rengini ayarlamaya çalışıyorum.
Yanıtlar:
Swift 2.0+ kullanıyorsanız
self.yourView.layer.borderWidth = 1
self.yourView.layer.borderColor = UIColor(red:222/255, green:225/255, blue:227/255, alpha: 1).cgColor
In Swift 4 Eğer sınır rengini ayarlayabilir ve kod aşağıda kullanarak UIControls için genişliği.
let yourColor : UIColor = UIColor( red: 0.7, green: 0.3, blue:0.1, alpha: 1.0 )
yourControl.layer.masksToBounds = true
yourControl.layer.borderColor = yourColor.CGColor
yourControl.layer.borderWidth = 1.0
<Swift 4 , Aşağıdaki kodu kullanarak UIView'un kenarlık genişliğini ve kenarlık rengini ayarlayabilirsiniz.
yourView.layer.borderWidth = 1
yourView.layer.borderColor = UIColor.red.cgColor
Aynısını yapmak için @IBDesignable ve @IBInspectable kullanın .
Yeniden kullanılabilirler, Arayüz Oluşturucu'dan kolayca değiştirilebilirler ve değişiklikler anında Öykü Paneline yansıtılır
Film şeridindeki nesneleri belirli bir sınıfa uygun hale getirin
Kod Parçacığı:
@IBDesignable
class CustomView: UIView{
@IBInspectable var borderWidth: CGFloat = 0.0{
didSet{
self.layer.borderWidth = borderWidth
}
}
@IBInspectable var borderColor: UIColor = UIColor.clear {
didSet {
self.layer.borderColor = borderColor.cgColor
}
}
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
}
}
Arayüz Oluşturucu'dan kolay değişiklik yapılmasına izin verir:
@IBDesignableve @IBInspectableher şey iyi çalışıyor, bu nedenle gümrükler için Renk Varlıklarını hem karanlık hem de açık modda kullanmanın bir yolu olmalı ~ bunu nasıl başarabileceğini kimse biliyor mu?
Tüm UIViews ile kullanmak için bir uzantı yazabilirsiniz, örn. UIButton, UILabel, UIImageView vb. Aşağıdaki yöntemimi ihtiyacınıza göre özelleştirebilirsiniz, ancak sizin için iyi çalışacağını düşünüyorum.
extension UIView{
func setBorder(radius:CGFloat, color:UIColor = UIColor.clearColor()) -> UIView{
var roundView:UIView = self
roundView.layer.cornerRadius = CGFloat(radius)
roundView.layer.borderWidth = 1
roundView.layer.borderColor = color.CGColor
roundView.clipsToBounds = true
return roundView
}
}
Kullanım:
btnLogin.setBorder(7, color: UIColor.lightGrayColor())
imgViewUserPick.setBorder(10)
hızlı 3
func borderColor(){
self.viewMenuItems.layer.cornerRadius = 13
self.viewMenuItems.layer.borderWidth = 1
self.viewMenuItems.layer.borderColor = UIColor.white.cgColor
}
Swift 5.2 , UIView + Uzantı
extension UIView {
public func addViewBorder(borderColor:CGColor,borderWith:CGFloat,borderCornerRadius:CGFloat){
self.layer.borderWidth = borderWith
self.layer.borderColor = borderColor
self.layer.cornerRadius = borderCornerRadius
}
}
Bu uzantıyı kullandınız;
yourView.addViewBorder(borderColor: #colorLiteral(red: 0.6, green: 0.6, blue: 0.6, alpha: 1), borderWith: 1.0, borderCornerRadius: 20)
Kodu kendi viewDidLoad()
self.view.layer.borderColor = anyColor().CGColor
Ve Colorile ayarlayabilirsinizRGB
func anyColor() -> UIColor {
return UIColor(red: 0.0/255.0, green: 0.0/255.0, blue: 0.0/255.0, alpha: 1.0)
}
Hakkında bir şeyler öğrenmek CALayerdeUIKit
hızlı 3.0
self.uiTextView.layer.borderWidth = 0.5
self.txtItemShortDes.layer.borderColor = UIColor(red:205.0/255.0, green:205.0/255.0, blue:205.0/255.0, alpha: 1.0).cgColor
Bunun için yöntem oluşturabiliriz. Basitçe kullanın.
public func createBorderForView(color: UIColor, radius: CGFloat, width: CGFloat = 0.7) {
self.layer.borderWidth = width
self.layer.cornerRadius = radius
self.layer.shouldRasterize = false
self.layer.rasterizationScale = 2
self.clipsToBounds = true
self.layer.masksToBounds = true
let cgColor: CGColor = color.cgColor
self.layer.borderColor = cgColor
}
Swift 3.0
groundTrump.layer.borderColor = UIColor.red.cgColor