2017-07-14 73 views
1

我有一個包含文本和圖像的按鈕,但我希​​望圖像上有一個圓角半徑。當我嘗試將角半徑應用到按鈕時,它適用於我認爲正確的整個按鈕。我怎樣才能將角落半徑設置爲圖像?Swift - 設置嵌套在UIButton中的圖像的圓角半徑

這裏是我的代碼:

let titleButton = UIButton() 
titleButton.frame = CGRect(x: 0, y: 0, width: 100, height: 40) 
titleButton.setTitle("display name", for: .normal) 
titleButton.setImage(#imageLiteral(resourceName: "imgName"), for: .normal) 
titleButton.layer.masksToBounds = true 
titleButton.layer.cornerRadius = titleButton.frame.width/2 

self.navigationItem.titleView = titleButton 

回答

2

UIButton上有一個UIImageView。所以到角落,只需將半徑設置爲圖像視圖

titleButton.imageView.layer.cornerRadius = 5 
0

層,如果你想四捨五入然後設置高度和寬度等於

titleButton.frame = CGRect(x: 0, y: 0, width: 50, height: 50) 

titleButton.layer.cornerRadius = titleButton.frame.size.width/2.0 
titleButton.layer.masksToBounds = true 

,如果你想角落只四捨五入然後

titleButton.frame = CGRect(x: 0, y: 0, width: 50, height: 50) 

titleButton.layer.cornerRadius = 10 
titleButton.layer.masksToBounds = true