[英]Programmatically Setting Corner Radius for UIButtons in Landscape and Portrait Mode for All iPhone and iPad Devices
我正在使用的應用程序幾乎是蘋果默認iPhone計算器應用程序的復制品。
我很難同時為縱向的基本操作員按鈕和基本的操作員按鈕以及橫向模式下的其他按鈕設置拐角半徑。
在viewWillLayoutSubviews()
指定拐角半徑(通過划分UIButton.bounds.height / 2
viewWillLayoutSubviews()
是否正確?
我需要左側和右側的按鈕在所有設備旋轉時保持圓角。 我不關心按鈕的頂部和底部是圓形的。
謝謝大家在幫助我解決此問題方面的考慮。 我不是在尋找直接的答案,但是如果大家都可以指出一些話題,那將是巨大的幫助。 我也嘗試過在viewDidAppear().
設置拐角半徑viewDidAppear().
尋找使用UIButton.bounds.height.
的編程解決方案UIButton.bounds.height.
我相信我只是在嘗試找到正確的方法來通過
var cornerRadius_forButtons: Double = 0
if UIDevice.current.orientation == UIDeviceOrientation.landscapeLeft{
cornerRadius_forButtons = Double(tag1_Button.bounds.height / 2.0)
}
else if UIDevice.current.orientation == UIDeviceOrientation.landscapeRight{
cornerRadius_forButtons = Double(tag1_Button.bounds.height / 2.0)
} else {
cornerRadius_forButtons = Double(tag1_Button.bounds.height / 2.0)
}
UIButton.layer.cornerRadius = CGFloat(cornerRadius_forButtons)
使用此方法時,唯一困難的一點是設置約束。 在大多數情況下,您不必弄亂代碼。
1.)在圖像編輯器中畫一個圓圈。 鏈接到我使用的圖像編輯器
3.)使背景透明(如果背景為白色則不需要)
4.)將圖像添加到您的應用程序資產文件夾
5.)在Xcode中,將按鈕image
屬性設置為剛創建的圖像。
6.)為所有按鈕創建約束
現在,無論方向如何,它都可以在所有設備上運行。
問題就在我身上。 通過在iOS中計算和實現拐角半徑,成功完成了在所有iOS設備的縱向和橫向模式下在UIButton的左側和行駛側獲得圓形邊緣的解決方案。
var cornerRadius_forButtons: Double = 0
override func viewWillLayoutSubviews() {
if UIDevice.current.orientation == UIDeviceOrientation.landscapeLeft{
cornerRadius_forButtons = Double(tag1_Button.bounds.height / 2.0)
}
else if UIDevice.current.orientation == UIDeviceOrientation.landscapeRight{
cornerRadius_forButtons = Double(tag1_Button.bounds.height / 2.0)
} else {
//PORTRAIT
cornerRadius_forButtons = Double(tag1_Button.bounds.height / 2.0)
}
tag1_Button.layer.cornerRadius = CGFloat(cornerRadius_forButtons)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.