我的 View 中有一个标签,根据某些条件,该标签对于某些行将可见。我在堆栈 View 中添加了标签。标签的 UI 是背景色和圆角半径。
我有子类 UItableview 单元格并使用了以下代码:
class SampleCell: UITableViewCell {
@IBOutlet weak var status: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
override func prepareForReuse() {
super.prepareForReuse()
self.status.text = nil
}
override func didMoveToSuperview() {
super.didMoveToSuperview()
layoutIfNeeded()
}
func setUpStatusLabel(isShow: Bool) {
self.status.text = "Status"
self.status.textColor = UIColor.red
self.status.font = UIFont.systemFont(ofSize: 11)
self.status.layer.cornerRadius =
self.status.frame.size.height/2
self.status.layer.masksToBounds = false
self.status.layer.backgroundColor = UIColor.lightGray.cgColor
self.status.isHidden = !isShow
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = (tableView.dequeueReusableCell(withIdentifier: "sample", for: indexPath) as? SampleCell)!
if indexPath.row % 2 == 0 {
cell.setUpStatusLabel(isShow: true)
} else {
cell.setUpStatusLabel(isShow: false)
}
}
当我启动此代码并滚动表格 View 时,标签的角半径有时会以这样的方式显示矩形 View ,这意味着没有角半径,有时背景颜色也会丢失。当用户滚动表格 View 时,我需要保留所有这些。
最佳答案
您需要为 isShow
的两种情况定义您的 UI:
func setUpStatusLabel(isShow: Bool) {
self.status.isHidden = !isShow
self.status.layer.masksToBounds = false
self.status.layer.backgroundColor = UIColor.lightGray.cgColor
self.status.font = UIFont.systemFont(ofSize: 11)
if isShow {
self.status.text = "Status"
self.status.textColor = UIColor.red
self.status.layer.cornerRadius = self.status.frame.size.height/2
} else {
//change the following according to your need.
self.status.text = ""
self.status.textColor = UIColor.darkText
self.status.layer.cornerRadius = 0
}
}
关于ios - 滚动表格 View 时,标签的角半径变得扭曲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58165051/