2017-03-14 38 views

回答

0

你需要使用Button的圖層來設置border屬性。 e.g:

button.layer.cornerRadius = button.frame.height/2.0 
button.layer.masksToBounds = true 
button.layer.borderColor = UIColor.lightGray.cgColor 
button.layer.borderWidth = 1.0 

OR

添加此擴展獲得故事板這些選項。

extension UIView { 
    @IBInspectable var cornerRadius: CGFloat { 
     get { 
      return layer.cornerRadius 
     } 
     set { 
      layer.cornerRadius = newValue 
      layer.masksToBounds = newValue > 0 
     } 
    } 
    @IBInspectable var borderWidth: CGFloat { 
     get { 
      return layer.borderWidth 
     } 
     set { 
      layer.borderWidth = newValue 
     } 
    } 
    @IBInspectable var borderColor: UIColor? { 
     get { 
      let color = UIColor.init(cgColor: layer.borderColor!) 
      return color 
     } 
     set { 
      layer.borderColor = newValue?.cgColor 
     } 
    } 
    @IBInspectable var shadowRadius: CGFloat { 
     get { 
      return layer.shadowRadius 
     } 
     set { 
      layer.shadowColor = UIColor.black.cgColor 
      layer.shadowOffset = CGSize(width: 0, height: 2) 
      layer.shadowOpacity = 0.4 
      layer.shadowRadius = shadowRadius 
     } 
    } 
} 
+0

對,沒錯,這是錯的。但爲什麼我在Storyboard中沒有這個選項? –

+0

默認情況下,這些選項在故事板中不可編輯。如果你想要的話,你可以添加一個'UIView擴展'來獲取故事板中的這些選項。檢查更新的答案。 –

+1

謝謝!我用庫「材料」,它有默認的這一個,昨天我刪除了這個庫,它發生了)我認爲這是默認設置在IOS(我是新的在iOS編程) –

0

它應該是這樣的

#import <QuartzCore/QuartzCore.h> 

[[myButton layer] setBorderWidth:2.0f]; 

[[myButton layer] setBorderColor:[UIColor greenColor].CGColor]; 

myButton.layer.cornerRadius = myButton.frame.height/2.0; 

對於裁判:

https://stackoverflow.com/a/32468101/5184217

0

不能直接設置的CALayer屬性的UIElement。相反,需要使用.layer屬性。

例子:

button.layer.cornerRadius = button.frame.height/ 2.0 

button.layer.maskToBounds = true 
相關問題