2017-10-17 196 views
0

我有相等的間隔簡單StackView在我的界面生成器:圓形拐角不顯示正確

enter image description here

當運行該應用程序的按鈕的位置和大小是正確的(與所示在Interface Builder),但是當我試圖把按鈕的角落,中間用下面的代碼:

override func viewDidLoad() { 
    super.viewDidLoad()   
    let maskPath = UIBezierPath(roundedRect: button.bounds, 
           byRoundingCorners: [.bottomLeft,.bottomRight], 
           cornerRadii: CGSize(width: 10.0, height: 10.0)) 
    let shape = CAShapeLayer() 
    shape.path = maskPath.cgPath 
    button.layer.mask = shape 

} 

我得到不正確的結果(按鈕的文字不再居中,也不覆蓋全寬):

enter image description here

問題在哪裏?謝謝

+2

您是否嘗試瞭解涵蓋此視圖的內容?你可以在運行時檢查視圖,請添加截圖 –

回答

3

您不應該在viewDidLoad()中使用button.bounds,因爲此時視圖的幾何體未設置。嘗試將代碼移動到viewWillAppear()viewDidLayoutSubviews(),其中邊界將是正確的。

0

如果第一個答案仍不能解決您的問題,請在DispatchQueue.main.async {}viewWillAppeardidAppear中包裝您的代碼。