Swiped时无法隐藏NavBar

时间:2017-10-29 07:29:22

标签: ios swift uinavigationbar

我试图在刷卡时隐藏我的navBar并在navigationController?.hidesBarsOnSwipe = trueviewWillAppear()同时实施了viewDidLoad(),但导航栏仍未被隐藏。在我的例子中,我在navBar下面实现了一个自定义的segmentedController,它可以在两个不同的tableViewControllers之间切换。

我不确定这是否是navBar无法隐藏的原因。我的应用程序看起来像这样,我要隐藏的部分是“门票”。部。enter image description here

我的代码如下:

class TicketsViewController: UIViewController {

    var upcomingTableViewController: UpcomingTableViewController!
    var pastTransactionTableViewController: PastTransactionsTableViewController!

    let segmentedControllerView: SegmentedController = {
        let sc = SegmentedController()
        sc.translatesAutoresizingMaskIntoConstraints = false
        sc.segmentedController.addTarget(self, action: #selector(segmentedControlValueChanged), for: .valueChanged)
        sc.segmentedController.selectedSegmentIndex = 0
        return sc
    }()

    let containerView: UIView = {
        let v = UIView()
        v.translatesAutoresizingMaskIntoConstraints = false
        return v
    }()

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.hidesBarsOnSwipe = true
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        //These are the two tableViewControllers that are being toggled
        upcomingTableViewController = UpcomingTableViewController()
        pastTransactionTableViewController = PastTransactionsTableViewController()

        setupNavigationBar()
        setupViews()
    }

    @objc func segmentedControlValueChanged(_ sender: UISegmentedControl) {
        let segmentedControl = sender

        if segmentedControl.selectedSegmentIndex == 0 {
          configureChildViewController(childController: upcomingTableViewController, onView: containerView)
        } else {
           configureChildViewController(childController: pastTransactionTableViewController, onView: containerView)
        }
    }

    func setupNavigationBar() {
        Helper.sharedInstance.setupNavigationBar(title: "Tickets", homeVC: self)
        navigationController?.navigationBar.isTranslucent = false
        navigationController?.navigationBar.shadowImage = UIImage()
        navigationController?.hidesBarsOnSwipe = true
        navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.plain, target: nil, action: nil)
    }

    func setupViews() {

        view.addSubview(segmentedControllerView)
        view.addSubview(containerView)

        segmentedControllerView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        segmentedControllerView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        segmentedControllerView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
        segmentedControllerView.heightAnchor.constraint(equalToConstant: 44).isActive = true

        containerView.topAnchor.constraint(equalTo: segmentedControllerView.bottomAnchor, constant: 0).isActive = true
        containerView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        containerView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
        containerView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true

        configureChildViewController(childController: upcomingTableViewController, onView: containerView)

    }

    func configureChildViewController(childController: UIViewController, onView: UIView?) {
        var holderView = UIView()

        if let onView = onView {
            holderView = onView
        } else {
            holderView = self.view
        }

        addChildViewController(childController)
        holderView.addSubview(childController.view)
        constraintViewEqual(to: holderView, childControllerView: childController.view)
        childController.didMove(toParentViewController: self)
    }


    func constraintViewEqual(to containerView: UIView, childControllerView: UIView) {
        childControllerView.translatesAutoresizingMaskIntoConstraints = false

        childControllerView.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true
        childControllerView.leftAnchor.constraint(equalTo: containerView.leftAnchor).isActive = true
        childControllerView.rightAnchor.constraint(equalTo: containerView.rightAnchor).isActive = true
        childControllerView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor).isActive = true
    }

}

上面的代码是我对这个ticketViewController的完整代码。感谢一些建议为什么hideBarsWhenSwipe并不隐藏我的导航栏。感谢。

1 个答案:

答案 0 :(得分:0)

尝试调整下面的元素大小以匹配视图控制器高度。