2015-02-13 10 views
10

Aşağıdaki resimde (bkz. "Başlarken") şeffaf arka plana benzeyen bir UIButton kenarlık nasıl yapabilirim?Swift Saydam Arka Planda Düğme Sınırı

Bunu, storyboard'u kullanarak veya program aracılığıyla nasıl yapmalıyım? backgroundColorclearColor için ayarlama

enter image description here

+2

Ne denedin? Ne işe yaramadı? –

cevap

27

düğmesi şeffaf hale getirir.
Örneğin aşağıdaki kodu deneyin. BorderAlpha, cornerRadius ve renkleri istediğiniz gibi yapılandırabilir ve değiştirebilirsiniz.

let borderAlpha : CGFloat = 0.7 
let cornerRadius : CGFloat = 5.0 

button.frame = CGRectMake(100, 100, 200, 40) 
button.setTitle("Get Started", forState: UIControlState.Normal) 
button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal) 
button.backgroundColor = UIColor.clearColor() 
button.layer.borderWidth = 1.0 
button.layer.borderColor = UIColor(white: 1.0, alpha: borderAlpha).CGColor 
button.layer.cornerRadius = cornerRadius 
3

kullanarak Uzatma:

extension UIButton 
{ 
func setUpLayer(sampleButton: UIButton?) { 
    sampleButton!.setTitle("GET STARTED", forState: UIControlState.Normal) 
    sampleButton?.tintColor = UIColor.whiteColor() 
    sampleButton!.frame = CGRect(x:50, y:500, width:170, height:40) 
    sampleButton!.layer.borderWidth = 1.0 
    sampleButton!.layer.borderColor = UIColor.whiteColor().colorWithAlphaComponent(0.5).CGColor 
    sampleButton!.layer.cornerRadius = 5.0 

    sampleButton!.layer.shadowRadius = 3.0 
    sampleButton!.layer.shadowColor = UIColor.whiteColor().CGColor 
    sampleButton!.layer.shadowOpacity = 0.3 
} 

} 

Kullanımı: Swift 3, Film Şeridi 'için, sadece kimlik Denetimcisi sonra projenize bu alt sınıf eklemek kullanma

let sampleUIButton = UIButton() 
    sampleUIButton.setUpLayer(sampleUIButton) 
    self.view.addSubview(sampleUIButton) 
1

yapmak Bu, storyboard'unuzdaki UIButton sınıfı. Daha sonra yatılı renk ve genişliğini ayarlayabilmeniz gerekir.

@IBDesignable class CustomButton: UIButton { 

@IBInspectable var borderColor: UIColor = UIColor.white { 
    didSet { 
     layer.borderColor = borderColor.cgColor 
    } 
} 

@IBInspectable var borderWidth: CGFloat = 2.0 { 
    didSet { 
     layer.borderWidth = borderWidth 
    } 
} 

override public func layoutSubviews() { 
    super.layoutSubviews() 
    clipsToBounds = true 
} 
}