Ios 向下滚动CollectionView时隐藏导航栏

Ios 向下滚动CollectionView时隐藏导航栏,ios,swift,Ios,Swift,我以编程方式创建了NavigationBar和TabBar,但在向下滚动collectionView时隐藏了NavigationBar。这是代码 进口基金会 进口基础 导入UIKit 类HomeMyBazar:UICollectionViewController、UICollectionViewDelegateFlowLayout{ let cellId = "cellId" let productCellId = "productCellId" let dealsCellId = "deal

我以编程方式创建了NavigationBar和TabBar,但在向下滚动collectionView时隐藏了NavigationBar。这是代码 进口基金会

进口基础 导入UIKit

类HomeMyBazar:UICollectionViewController、UICollectionViewDelegateFlowLayout{

let cellId = "cellId"

let productCellId = "productCellId"
let dealsCellId = "dealsCellId"


let titles = ["Hello world", "Product", "Deals", "Store world"]

override func viewDidLoad() {
    super.viewDidLoad()


    navigationController?.navigationBar.isTranslucent = false


    setupNavBarButtons()

    setupMenuBar()

    setupCollectionView()

}



func setupNavBarButtons() {









lazy var settingsLauncher: SettingsLauncher = {
    let launcher = SettingsLauncher()
    launcher.homeController = self
    return launcher
}()

func targetMyBazar() {


    let secondViewController = ViewController1()
    self.navigationController?.pushViewController(secondViewController, animated: true)

}

func targetSearch() {

    let viewController = ViewController4()
    self.navigationController?.pushViewController(viewController, animated: true)
}



func targetUser() {

    let viewController = ViewController3()
    self.navigationController?.pushViewController(viewController, animated: true)
}

func targetCart() {

    let viewController = ViewController5()
    self.navigationController?.pushViewController(viewController, animated: true)
}


func handleMore() {

    settingsLauncher.showSettings()
}


func showControllerForSetting(_ setting: Setting) {
    let dummySettingsViewController = UIViewController()
    dummySettingsViewController.view.backgroundColor = UIColor.white
    dummySettingsViewController.navigationItem.title = setting.name.rawValue
    navigationController?.navigationBar.tintColor = UIColor.white
    navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
    navigationController?.pushViewController(dummySettingsViewController, animated: true)
}




func scrollToMenuIndex(_ menuIndex: Int) {
    let indexPath = IndexPath(item: menuIndex, section: 0)
    collectionView?.scrollToItem(at: indexPath, at: UICollectionViewScrollPosition(), animated: true)


}

fileprivate func setTitleForIndex(_ index: Int) {
    if let titleLabel = navigationItem.titleView as? UILabel {
        titleLabel.text = "  \(titles[index])"
    }

}

lazy var menuBar: MenuBar = {
    let mb = MenuBar()
    mb.homeController = self
    return mb
}()

fileprivate func setupMenuBar() {
    navigationController?.hidesBarsOnSwipe = true

    let blackView = UIView()

    blackView.backgroundColor = .lightGray

    view.addSubview(blackView)
    view.addConstraintsWithFormat("H:|[v0]|", views: blackView)
    view.addConstraintsWithFormat("V:[v0(50)]", views: blackView)

    view.addSubview(menuBar)
    view.addConstraintsWithFormat("H:|[v0]|", views: menuBar)
    view.addConstraintsWithFormat("V:[v0(80)]", views: menuBar)

    menuBar.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor).isActive = true
}




override func scrollViewDidScroll(_ scrollView: UIScrollView) {
    menuBar.horizontalBarLeftAnchorConstraint?.constant = scrollView.contentOffset.x / 4
}

override func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {

    let index = targetContentOffset.pointee.x / view.frame.width

    let indexPath = IndexPath(item: Int(index), section: 0)
    menuBar.collectionView.selectItem(at: indexPath, animated: true, scrollPosition: UICollectionViewScrollPosition())

    setTitleForIndex(Int(index))
}


func setupCollectionView() {


    if let flowLayout = collectionView?.collectionViewLayout as? UICollectionViewFlowLayout {
        flowLayout.scrollDirection = .horizontal
        flowLayout.minimumLineSpacing = 0
    }

    collectionView?.backgroundColor = UIColor.blue

    collectionView?.register(MyBazarCell.self, forCellWithReuseIdentifier: cellId)


    collectionView?.register(ProductCell.self, forCellWithReuseIdentifier: productCellId)
    collectionView?.register(DealsCell.self, forCellWithReuseIdentifier: dealsCellId)

    collectionView?.contentInset = UIEdgeInsetsMake(110, 0, 0, 0)


    collectionView?.isPagingEnabled = true
}

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 4
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let identifier: String
    if indexPath.item == 1 {
        identifier = productCellId
    } else if indexPath.item == 2 {
        identifier = dealsCellId
    } else {
        identifier = cellId
    }

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath)

    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: view.frame.width, height: view.frame.height - 50)
}
lazy var collectionView: UICollectionView = {

    let layout = UICollectionViewFlowLayout()

    let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)

    cv.backgroundColor = UIColor.black
    cv.dataSource = self
    cv.delegate = self
    return cv
}()





let data: [Item] = [
    Item(imageName: "service", text: "Service"),
    Item(imageName: "product", text: "Product "),
    Item(imageName: "deals", text: " Deals"),
    Item(imageName: "store", text: "Store")
]


let cellId = "cellId"
let imageNames = ["service", "product", "deals", "store"]
let imageTitle = ["Servic", "Product", "Deals", "Store"]


var homeController: HomeMyBazar?

override init(frame: CGRect) {
    super.init(frame: frame)

    collectionView.register(MenuCell.self, forCellWithReuseIdentifier: cellId)

    addSubview(collectionView)
    addConstraintsWithFormat("H:|[v0]|", views: collectionView)
    addConstraintsWithFormat("V:|[v0]|", views: collectionView)

    let selectedIndexPath = IndexPath(item: 0, section: 0)
    collectionView.selectItem(at: selectedIndexPath, animated: false, scrollPosition: UICollectionViewScrollPosition())

    setupHorizontalBar()
}

var horizontalBarLeftAnchorConstraint: NSLayoutConstraint?

func setupHorizontalBar() {
    let horizontalBarView = UIView()

    horizontalBarView.backgroundColor = UIColor.red
    horizontalBarView.translatesAutoresizingMaskIntoConstraints = false
    addSubview(horizontalBarView)


    horizontalBarLeftAnchorConstraint = horizontalBarView.leftAnchor.constraint(equalTo: self.leftAnchor)
    horizontalBarLeftAnchorConstraint?.isActive = true

    horizontalBarView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
    horizontalBarView.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 1/4).isActive = true
    horizontalBarView.heightAnchor.constraint(equalToConstant: 4).isActive = true
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {



    homeController?.scrollToMenuIndex(indexPath.item)




}



func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {



    return data.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! MenuCell



    let item = data[indexPath.item]
    cell.imageView.image = UIImage(named: item.imageName)
    cell.imageTitle.text = item.text

    cell.tintColor = UIColor.white


    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: frame.width / 4, height: frame.height)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 0
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
}

类MenuCell:BaseCell{

let imageTitle: UILabel = {
    let label = UILabel()
    label.text = "hellow world label"
    label.textColor = .white
    return label
}()

let imageView: UIImageView = {
    let iv = UIImageView()
    iv.image = UIImage(named: "service")?.withRenderingMode(.alwaysTemplate)

    iv.tintColor = .white
    return iv
}()

override var isHighlighted: Bool {
    didSet {

        imageView.tintColor = isHighlighted ? UIColor.white : UIColor.white
    }
}

override var isSelected: Bool {
    didSet {

        imageView.tintColor = isSelected ? UIColor.white : UIColor.white
    }
}

override func setupViews() {
    super.setupViews()

    addSubview(imageTitle)
    addSubview(imageView)

    addConstraintsWithFormat("H:|-25-[v0(80)]|", views: imageTitle)
    addConstraintsWithFormat("V:|-45-[v0(20)]|", views: imageTitle)

    addConstraintsWithFormat("H:[v0(28)]", views: imageView)
    addConstraintsWithFormat("V:|-15-[v0(28)]|", views: imageView)

    addConstraint(NSLayoutConstraint(item: imageTitle, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0))
    addConstraint(NSLayoutConstraint(item: imageTitle, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0))

    addConstraint(NSLayoutConstraint(item: imageView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0))
    addConstraint(NSLayoutConstraint(item: imageView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0))
}
}

你可以试试这个

self.navigationController.hidesBarsOnSwipe = true

我不创建ViewWillAspect方法是您需要为隐藏导航栏添加ViewWillAspect方法