【问题标题】:Not getting proper Circular shape of UI Button for iOS 11 (Xcode 9)没有为 iOS 11 (Xcode 9) 获得正确的圆形 UI 按钮形状
【发布时间】:2017-10-16 05:42:53
【问题描述】:

我在导航栏上添加了一个按钮作为自定义视图。它需要是圆形的。 使用以下代码:

    let infoButton = UIButton(frame: CGRect(x: 0, y: 0, width: 24, height: 24))
    infoButton.setTitle("₹", for: .normal)
    infoButton.layer.cornerRadius = 12
    infoButton.clipsToBounds = true
    infoButton.layer.borderWidth = 1.5

    let infoItem = UIBarButtonItem(customView: infoButton)
    navigationItem.rightBarButtonItems = [infoItem]

到目前为止,这一直运行良好但在 iOS 11 设备中没有得到正确的圆形。

运行在 iOS 10 上的设备的快照:

运行在 iOS 11 上的设备的快照:

【问题讨论】:

    标签: xcode uibarbuttonitem ios11 swift4


    【解决方案1】:

    如果要创建圆形视图,则无需设置 clipsToBounds,而是将 maskToBounds 设置为 true。

    【讨论】:

    • 没有帮助。这里也不需要masksToBounds。
    【解决方案2】:

    这样使用:

    这里我改了cornerRadius和width:

    let infoButton = UIButton(frame: CGRect(x: 0, y: 0, width: 34, height: 24))
    infoButton.setTitle("₹", for: .normal)
    infoButton.layer.cornerRadius = infoButton.bounds.size.width / 2
    infoButton.clipsToBounds = true
    infoButton.layer.borderWidth = 1.5
    

    【讨论】:

    • 试过了,还是没有得到完美的圆形。
    • 尝试增加width并尝试。我签入了iPad 和iPhone 它为我工作。
    • 您检查的是否是 iOS 11 设备?它在运行 iOS 10 的所有设备上都可以正常工作。问题出在 iOS 11 设备上。
    • 是的,我正在检查 iOS11
    • 请检查我从 iOS 11 设备上传的结果。
    猜你喜欢
    • 2013-08-25
    • 2013-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多