我正在制作日历(刚开始)。我决定尝试以编程方式完成它,但由于一些奇怪的原因,我能够显示collectionView
backgroundColor但是当我添加单元格时,它们没有显示。在我看来,一切都正确设置。在图片中,白色背景是collectionView,红色背景是包含它的视图。黄色细胞虽然没有显示出来。
我的WeekView
班级
class WeekView: UIView, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
lazy var collectionView:UICollectionView = {
let layout = UICollectionViewLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.delegate = self
cv.dataSource = self
cv.directionalLockEnabled = true
cv.showsVerticalScrollIndicator = false
cv.showsHorizontalScrollIndicator = false
cv.translatesAutoresizingMaskIntoConstraints = false
cv.backgroundColor = UIColor.whiteColor()
return cv
}()
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = UIColor.redColor()
collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "myCell")
setup()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setup(){
addSubview(collectionView)
collectionView.leftAnchor.constraintEqualToAnchor(self.leftAnchor, constant: 50).active = true
collectionView.topAnchor.constraintEqualToAnchor(self.topAnchor, constant: 50).active = true
collectionView.rightAnchor.constraintEqualToAnchor(self.rightAnchor).active = true
collectionView.heightAnchor.constraintEqualToAnchor(self.heightAnchor).active = true
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 150
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("myCell", forIndexPath: indexPath)
cell.backgroundColor = UIColor.yellowColor()
return cell
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSizeMake(50, 50)
}
}
我的ViewController
班级
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
setUpLeftMenuView()
setupCalendar()
}
var menuBar:SideMenuView = {
let menuBar = SideMenuView()
return menuBar
}()
func setUpLeftMenuView(){
self.view.addSubview(menuBar)
menuBar.leftAnchor.constraintEqualToAnchor(view.leftAnchor).active = true
menuBar.topAnchor.constraintEqualToAnchor(topLayoutGuide.bottomAnchor).active = true
menuBar.widthAnchor.constraintEqualToConstant(100).active = true
menuBar.heightAnchor.constraintEqualToAnchor(view.heightAnchor).active = true
}
var calendarView:WeekView = {
let cv = WeekView()
cv.translatesAutoresizingMaskIntoConstraints = false
return cv
}()
func setupCalendar() {
view.addSubview(calendarView)
calendarView.leftAnchor.constraintEqualToAnchor(menuBar.rightAnchor).active = true
calendarView.topAnchor.constraintEqualToAnchor(topLayoutGuide.bottomAnchor).active = true
calendarView.rightAnchor.constraintEqualToAnchor(view.rightAnchor).active = true
calendarView.heightAnchor.constraintEqualToAnchor(view.heightAnchor).active = true
}
}