我正在尝试为该条按钮选项标签添加绿色背景,然后提供一些拐角半径。
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)
这是代码和图片
有关使绿色背景具有拐角半径的任何帮助。
答案 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)