簡體   English   中英

UIButtons 在所有設備上的形狀都不正確

[英]UIButtons are not correctly shaped on all devices

我有一個帶有四個方形按鈕的 UIView 控制器,並將它們設置為帶有邊框的圓形。 它在 iPhone 8 和 iPhone X 上完美運行,但在 iPhone SE 和 iPhone 8 Plus 中,UIButtons 不再是圓形的。 我已將 UIButtons 設置為方形並通過自動布局保持該比例,但它似乎不起作用。

在我的ViewController.Swift 中,我鏈接了四個 UIButtons,然后我應用了如下相同的代碼:

@IBOutlet weak var topLeftButtonImage: UIButton!


// Edit it to round
topLeftButtonImage.layer.cornerRadius = topLeftButtonImage.frame.size.width/2
topLeftButtonImage.clipsToBounds = true

// Add border
topLeftButtonImage.layer.borderColor = UIColor.white.cgColor // Button border color
topLeftButtonImage.layer.borderWidth = 4 // Button border width

在這里您可以看到 iPhone SE 和 iPhone 8 Plus 上的行為。 iPhone 8 和 iPhone X 沒問題

iPhone模擬器截圖

自動布局約束:

自動布局在行動

在viewcontroller的viewDidLayoutSubviews方法中設置按鈕cornerRadius

 override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    // Edit it to round
    topLeftButtonImage.layer.cornerRadius = topLeftButtonImage.bounds.size.height / 2
    topLeftButtonImage.clipsToBounds = true

    // Add border
    topLeftButtonImage.layer.borderColor = UIColor.white.cgColor // Button border color
    topLeftButtonImage.layer.borderWidth = 4 // Button border width
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM