我正在设计一个带有 swift3 表格 View 的 iOS 汉堡菜单
我想在菜单选项卡中显示通知数量。
let pieNotifi = UIImageView(frame: CGRectMake(cell.contentView.frame.size.width-40, cell.contentView.frame.size.height/2-7, 30, 20))
let pieLabNotifi = UILabel(frame: CGRectMake(cell.contentView.frame.size.width-40, cell.contentView.frame.size.height/2-7, 30, 20))
let subviews = self.view.viewWithTag(100001)
if myGlobal.unreaddot != 0 { //just a count for notifcaiton
if subviews == nil {
pieNotifi.tag = 100001;
cell.addSubview(pieNotifi)
let pieShape = pieNotifi.layer;
pieShape.backgroundColor = UIColor.red.cgColor
pieShape.cornerRadius = pieShape.bounds.width / 4
pieLabNotifi.textColor = UIColor.white
pieLabNotifi.adjustsFontSizeToFitWidth = true;
pieLabNotifi.minimumScaleFactor = 0.5
pieLabNotifi.textAlignment = NSTextAlignment.center
pieNotifi.addSubview(pieLabNotifi)
pieLabNotifi.text = String(myGlobal.unreaddot)
}
else{
pieLabNotifi.text = String(myGlobal.unreaddot)
}
}else{
let subviews = cell.viewWithTag(100001)
subviews?.removeFromSuperview()
print("remove red dot")
}
但是,它只显示红色图层,但没有标签。引用下图 enter image description here
我曾尝试设置
.sendSubview(to back:)
.bringSubview(toFront: )
.layer.zPosition = 1
所有这些关键词都不适合我。
有人可以建议我解决这个问题吗?
最佳答案
您没有向 UITableView 添加 subview 。根据您的代码,您将向 UITableView 的单元格添加一个 subview 。 尝试将 subview 添加到单元格的内容 View 中,例如:
cell.contentView.addSubView(pieNotifi)
关于ios - 标签不显示在 UITableView 的 subview 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43179203/