Swift:在推新控制器时,我的工具栏丢失了

时间:2020-04-21 22:16:54

标签: swift toolbar items

我正在构建一个带有底部工具栏的Swift应用程序。我使用集合视图创建了工具栏,因为我遵循以下指示:让我们在youtube上构建该应用:https://www.youtube.com/channel/UCuP2vJ6kRutQBfRmdcI92mA

但是使用工具栏我遇到了一个问题。当我在viewcontroller上推送新视图时,工具栏也不会被推送。如我所读,这是由于我创建的工具栏链接到我刚刚摆脱的控制器,现在导航控制器正在处理推入视图。我还阅读了以下有关添加工具栏的信息:

navigationController?.isToolbarHidden = false

按下新控制器时,这些项目将丢失。不幸的是,我找不到链接了(在StackOverflow的此处)...

因此,我想知道是否可以选择推新控制器并使工具栏停留。也许像将工具栏和项目添加到导航控制器中。

我编写的代码如下:

工具栏

class ToolbarMenu: UIView, UICollectionViewDataSource, UICollectionViewDelegate, 
UICollectionViewDelegateFlowLayout {

//Nachschauen warum das geht?
lazy var collectionView: UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
    cv.backgroundColor = .standardColor
    cv.dataSource = self
    cv.delegate = self
    return cv
}()

let cellId = VerticalCell.Toolbar.rawValue
let imageNames = ["Home", "Ball", "Table", "Account"]
let cellNames = ["Home", "Games", "Table", "Account"]

var horizontalBarLeftAnchorConstraint: NSLayoutConstraint?

var homeController: HomeController?

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

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

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

    //Preselected State
    let selectedIndexPath = IndexPath(item: 0, section: 0)
    collectionView.selectItem(at: selectedIndexPath, animated: false, scrollPosition: .top)

    setupHorizontalBar()
}

func setupHorizontalBar(){
    let horizontalBarView = UIView()
    horizontalBarView.backgroundColor = UIColor(white: 0.9, alpha: 1)
    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: widthAnchor, multiplier: 1/4).isActive = true
    horizontalBarView.heightAnchor.constraint(equalToConstant: 3).isActive = true
}

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

    homeController?.scrollToMenuIndex(menuIndex: indexPath.item)
}

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

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

    cell.imageView.image = UIImage(named: imageNames[indexPath.item])?.withRenderingMode(.alwaysTemplate)
    cell.cellLabel.text = cellNames[indexPath.item]
    cell.tintColor = .black

    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: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

}

MenuCell

 class MenuCell: BaseCell {

let imageView: UIImageView = {
    let iv = UIImageView()
    iv.image = UIImage(named: "Ball")?.withRenderingMode(.alwaysTemplate)
    iv.tintColor = .black
    return iv
}()

let cellLabel: UILabel = {
    let cellLabel = UILabel()
    cellLabel.translatesAutoresizingMaskIntoConstraints = false
    cellLabel.font = .standardText(size: 14)
    cellLabel.textAlignment = .center
    return cellLabel
}()

override var isHighlighted: Bool {
    didSet {
        imageView.tintColor = isHighlighted ? UIColor.white : .black
    }
}

override var isSelected: Bool {
    didSet {
        imageView.tintColor = isSelected ? UIColor.white : .black
    }
}

override func setupViews() {
    super.setupViews()

    addSubview(imageView)
    addSubview(cellLabel)

    addConstraintsWithFormat(format: "H:[v0(28)]", views: imageView)
    addConstraintsWithFormat(format: "V:[v0(28)]-3-[v1(15)]", views: imageView, cellLabel)

    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: -10))
    addConstraint(NSLayoutConstraint(item: cellLabel, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0))
}

}

在Homecontroller中添加工具栏

  view.addSubview(toolBar)
  view.addConstraintsWithFormat(format: "H:|[v0]|", views: toolBar)
  view.addConstraintsWithFormat(format: "V:[v0(60)]-40-|", views: toolBar)

HomeController:

let layout = UICollectionViewFlowLayout()
let vc = newController(collectionViewLayout: layout)
vc.cellFixture = fixture
navigationController?.pushViewController(vc, animated: true) 

也:如果有人对如何改进我的编码有意见,我很高兴听到:)

0 个答案:

没有答案