所以我只是有点困惑为什么这不能按照我想要的方式在这个VC中工作。 基本上我有一个滚动视图设置,其中包含一些项目。顶部项是imageContainer,其中包含一个图像,然后在下面是collectionView。但是,当我尝试将collectionView绑定到scrollView的底部时,没有任何内容出现。它只在我给它高度后出现。 但是不应该将它固定在它上面的项目的底部,并且滚动视图的底部定义它的高度。当我定义高度并滚动到底部时,我可以看到我不想要的scrollView的背面。
我在创建此屏幕时试图模仿本教程 https://medium.freecodecamp.org/tutorial-creating-stretchy-layouts-on-ios-using-auto-layout-3fa974fa5e28
import Foundation
import UIKit
class NewProfileVC: UIViewController,UIScrollViewDelegate {
private let scrollView = UIScrollView()
private let imageView = UIImageView()
private let imageContainer = UIView()
let cellID = "cellID"
let headerID = "headerID"
lazy var myCollectionView : UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
cv.translatesAutoresizingMaskIntoConstraints = false
cv.delegate = self
cv.dataSource = self
cv.backgroundColor = .yellow
return cv
}()
lazy var currentImage : UIImageView = {
let currentImage = UIImageView()
currentImage.clipsToBounds = true
currentImage.translatesAutoresizingMaskIntoConstraints = false
currentImage.contentMode = .scaleToFill
currentImage.layer.masksToBounds = true
currentImage.image = UIImage(named: "lbj")
return currentImage
}()
override func viewDidLoad() {
super.viewDidLoad()
myCollectionView.register(NewUserHeader.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: headerID)
myCollectionView.register(NewUserEventAttendingCell.self, forCellWithReuseIdentifier: cellID)
setupVC()
}
@objc func setupVC(){
//will be responsible for setting up vc
self.navigationController?.navigationBar.isTranslucent = false
let bounds = self.navigationController!.navigationBar.bounds
self.navigationController?.navigationBar.frame = CGRect(x: 0, y: 0, width: bounds.width, height: bounds.height - 400)
view.addSubview(scrollView)
scrollView.contentInsetAdjustmentBehavior = .never
scrollView.delegate = self
scrollView.showsVerticalScrollIndicator = false
//Pin the edges of the scroll view to
// our view controller’s view
scrollView.backgroundColor = .white
scrollView.snp.makeConstraints { (make) in
make.edges.equalTo(view.safeAreaLayoutGuide)
}
imageContainer.backgroundColor = .darkGray
scrollView.addSubview(imageContainer)
//Pin the top of our image view to the scroll view
// pin the left and right to the view controller’s view
// give it an aspect ratio constraint by constraining
// its height to its width with a multiplier
imageContainer.snp.makeConstraints {(make) in
make.top.equalTo(scrollView)
make.left.right.equalTo(view)
make.height.equalTo(imageContainer.snp.width).multipliedBy(0.95)
}
scrollView.addSubview(currentImage)
currentImage.snp.makeConstraints {(make) in
make.left.right.equalTo(imageContainer)
//** Note the priorities
make.top.equalTo(view).priority(.high)
//** We add a height constraint too
make.height.greaterThanOrEqualTo(imageContainer.snp.height).priority(.required)
//** And keep the bottom constraint
make.bottom.equalTo(imageContainer.snp.bottom)
}
scrollView.addSubview(myCollectionView)
myCollectionView.snp.makeConstraints {(make) in
make.top.equalTo(imageContainer.snp.bottom)
make.left.right.equalTo(view)
make.bottom.equalTo(scrollView)
make.height.equalTo(view.bounds.height/1.5)
}
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
//** We want the scroll indicators to use all safe area insets
scrollView.scrollIndicatorInsets = view.safeAreaInsets
//** But we want the actual content inset to just respect the bottom safe inset
scrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: view.safeAreaInsets.bottom, right: 0)
}
private var previousStatusBarHidden = false
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if previousStatusBarHidden != shouldHideStatusBar {
UIView.animate(withDuration: 0.2, animations: {
self.setNeedsStatusBarAppearanceUpdate()
})
previousStatusBarHidden = shouldHideStatusBar
}
}
private var shouldHideStatusBar: Bool {
let frame = myCollectionView.convert(myCollectionView.bounds, to: nil)
return frame.minY < view.safeAreaInsets.top
}
override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
return .slide
}
override var prefersStatusBarHidden: Bool {
return shouldHideStatusBar
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}
extension NewProfileVC: UICollectionViewDataSource, UICollectionViewDelegate,UICollectionViewDelegateFlowLayout {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) as! NewUserEventAttendingCell
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 10.0, left: 1.0, bottom: 1.0, right: 1.0)
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
let kWhateverHeightYouWant = 149
return CGSize(width: collectionView.bounds.size.width - 60, height: CGFloat(kWhateverHeightYouWant))
}
// func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
// return CGSize(width: view.frame.width/6, height: 100)
// }
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerID, for: indexPath) as! NewUserHeader
return header
}
}