【发布时间】:2017-02-23 17:49:28
【问题描述】:
我有 ViewController 和里面的 UICollectionView,我正在尝试以编程方式将 UILable 添加到 UICollectionView:
let Label: UILabel = {
let label = UILabel()
label.text = "Sample Text Here"
label.textAlignment = .center
label.textColor = UIColor(white: 0.4, alpha: 0.4)
label.font = UIFont.systemFont(ofSize: 20)
return label
}()
override func viewDidLoad() {
super.viewDidLoad()
Label.translatesAutoresizingMaskIntoConstraints = false
Label.frame = CGRect(x: 0, y: 40, width: view.frame.width, height: 20)
collectionView.addSubview(Label)
}
当我尝试更改 y 值时,它是相同的
当我尝试像这样添加它时:view.addSubView(Label) 根本没有出现。
注意:如果没有数据填充单元格,则应显示标签
【问题讨论】:
-
不要将子视图添加到collectionView。创建 UICollectionViewCell 并向单元格添加标签
-
@Bhanupriya 如果没有要填充的单元格,标签将出现
标签: ios swift3 uicollectionview