嗨,我试图用图层来画一个圆圈,然而,我的标签只显示数字,而不是我为标签创建的背景色或边框。这是为什么?这是我的密码:
let countLabel = UILabel()
countLabel.text = "5"
let size:CGFloat = 55.0
countLabel.textColor = UIColor.white
countLabel.textAlignment = .center
countLabel.font = UIFont.systemFont(ofSize: 14.0)
countLabel.bounds = CGRect(x : 0.0,y : 0.0,width : size, height : size)
countLabel.layer.cornerRadius = size / 2
countLabel.layer.borderWidth = 3.0
//countLabel.layer.masksToBounds = true
countLabel.layer.backgroundColor = UIColor.orange.cgColor
countLabel.layer.borderColor = UIColor.orange.cgColor
countLabel.center = CGPoint(x:200.0,y: 200.0)
countLabel.translatesAutoresizingMaskIntoConstraints = false
self.topContainer.addSubview(countLabel)
countLabel.topAnchor.constraint(equalTo: profileImage.topAnchor).isActive = true
countLabel.trailingAnchor.constraint(equalTo: profileImage.trailingAnchor, constant: 10).isActive = true 我想得到这样的东西。

。
然而,上面没有输出橙色的颜色。为什么?
发布于 2016-11-16 19:44:53
创建一个视图并使其成为一个圆圈,然后在其中添加标签。以下是代码:
let size:CGFloat = 36
let someView = UIView()
someView.translatesAutoresizingMaskIntoConstraints = false
someView.layer.cornerRadius = size/2
someView.backgroundColor = .orange
someView.layer.borderColor = UIColor.white.cgColor
someView.layer.borderWidth = 2
self.topContainer.addSubview(someView)
someView.topAnchor.constraint(equalTo: profileImage.topAnchor).isActive = true
someView.trailingAnchor.constraint(equalTo: profileImage.trailingAnchor, constant: 10).isActive = true
someView.heightAnchor.constraint(equalToConstant: size).isActive = true
someView.widthAnchor.constraint(equalToConstant: size).isActive = true
let countLabel = UILabel()
countLabel.text = "5"
countLabel.textColor = UIColor.white
countLabel.textAlignment = .center
countLabel.font = UIFont.systemFont(ofSize: 14.0)
countLabel.translatesAutoresizingMaskIntoConstraints = false
someView.addSubview(countLabel)
countLabel.centerXAnchor.constraint(equalTo: someView.centerXAnchor).isActive = true
countLabel.centerYAnchor.constraint(equalTo: someView.centerYAnchor).isActive = true发布于 2016-11-16 12:45:12
尝试添加以下代码:
countLabel.layer.cornerRadius = 0.5 * countLabel.bounds.size.width而不是:
// remove the below code of line from your code
countLabel.layer.cornerRadius = size / 2发布于 2016-11-16 12:42:13
我猜你在这里偶然发现了一个窃听器。
如果我将基于代码的代码添加到游乐场中,它将输出“空图像”,而不是显示视图。
但是如果我用一个框架创建标签,它就能工作。
不起作用:
let countLabel = UILabel()
countLabel.frame = CGRect(x : 0.0,y : 0.0,width : size, height : size)不起作用:
let countLabel = UILabel()
countLabel.bounds = CGRect(x : 0.0,y : 0.0,width : size, height : size)不起作用:
let countLabel = UILabel()
countLabel.sizeToFit()工作:
let size:CGFloat = 55.0
let countLabel = UILabel(frame: CGRect(x : 0.0,y : 0.0,width : size, height : size))我操场上的完整代码:
import UIKit
let size:CGFloat = 55.0
let countLabel = UILabel(frame: CGRect(x : 0.0,y : 0.0,width : size, height : size))
countLabel.text = "5"
countLabel.textColor = UIColor.white
countLabel.textAlignment = .center
countLabel.font = UIFont.systemFont(ofSize: 24.0)
countLabel.layer.cornerRadius = size / 2
countLabel.layer.borderWidth = 3.0
countLabel.layer.masksToBounds = true
countLabel.layer.backgroundColor = UIColor.orange.cgColor
countLabel.layer.borderColor = UIColor.orange.cgColor
countLabel.translatesAutoresizingMaskIntoConstraints = false结果:

https://stackoverflow.com/questions/40632207
复制相似问题