条形按钮项目无角半径

时间:2019-01-05 13:54:27

标签: swift navbar cornerradius

我正在尝试为该条按钮选项标签添加绿色背景,然后提供一些拐角半径。

    utcLbl.frame = CGRect(x: 0, y: 0, width: 100, height: 20)
    utcLbl.text = "\(dateFormatter.string(from: Date())) UTC"
    utcItem.customView = utcLbl
    utcLbl.backgroundColor = UIColor.green
    utcLbl.layer.cornerRadius = 20
    utcLbl.textAlignment = .center
    self.navigationItem.setLeftBarButtonItems([utcItem], animated: true)

这是代码和图片

enter image description here

有关使绿色背景具有拐角半径的任何帮助。

1 个答案:

答案 0 :(得分:1)

您需要clipToBounds或masksToBounds

utcLbl.frame = CGRect(x: 0, y: 0, width: 100, height: 20)
utcLbl.text = "\(dateFormatter.string(from: Date())) UTC"
utcItem.customView = utcLbl
utcLbl.backgroundColor = UIColor.green
utcLbl.layer.cornerRadius = 20
utcLbl.textAlignment = .center
utcLbl.layer.masksToBounds = true // Or utcLbl.clipsToBounds = true
self.navigationItem.setLeftBarButtonItems([utcItem], animated: true)