2017-07-19 50 views
0

下面給出的類是工作的罰款如何從故事板定製IBDesignable類更改按鈕的cornerRadius創建

@IBDesignable class iButton : UIButton { 

@IBInspectable var cornerRadius : CGFloat = 0.0{ 
    didSet{ 
     layer.cornerRadius = cornerRadius 
    } 
}} 

但問題是,當我在屬性檢查器與大小的按鈕設置cornerRadius值35 (寬度70,高度70)。我在故事板上獲得了四捨五入的按鈕,但在模擬器上運行時,它不是圓形的,而是其圓角矩形。

xCode上的我的設計視圖是iPhone-SE,並在iPhone-7 plus模擬器上模擬。

我還通過在Size Inspector上設置高度和寬度來啓用自動調整大小。

Check this Image for xcode design and Simulated device Screen

Size Inspector for Auto-Resizing

從我瞭解的圓角半徑應爲寬度的一半。當按鈕的大小是通過自動調整大小來實現的,爲什麼拐角半徑不能調整大小。我怎樣才能解決這個問題?

在此先感謝。

+0

給按鈕的約束,然後用這個代替'button.layer.cornerRadius = button.frame.width/2' – DatForis

+0

對於圓形按鈕,它會有所幫助,但不是所有情況下@DatForis – Hurdler

+0

https://iosdevcenters.blogspot.com/2017/07/how-to-use-ibinspectable-in-swift-30.html –

回答

0

謝謝你,我得到了答案。

我們還需要縮放cornerRadius。

layer.cornerRadius = cornerRadius * (UIScreen.main.bounds.width/320.0) //320.0 be my width of the screen that i designed on storyboard 
0
import UIKit 

    @IBDesignable class RoundedCornerButton: UIButton { 

     override func drawRect(rect: CGRect) { 
     self.clipsToBounds = true 
    } 

    @IBInspectable var cornerRadius: CGFloat = 0 { 
     didSet { 
      self.layer.cornerRadius = cornerRadius 
     } 
     } 
} 

,或者你可以檢查此鏈接此鏈接瞭解更多信息

https://www.youtube.com/watch?v=JQ5i2YKwvJ8