当弹出rootViewController时,大UINavigationBar会顺利调整大小

时间:2018-05-26 07:31:13

标签: ios swift uinavigationcontroller

嘿伙计我在子ViewController中使用了大型UINavigationBar,我希望在顺利弹回rootViewController时将我的navBar调整为默认大小。

vc' gif:https://giphy.com/gifs/1P0HwqlIqqMnzibxbH

修改

我不想从父vc中移除largeNavBar,我只想逐渐消失它并使用app store这样的动画:https://giphy.com/gifs/YXsTA6I5r0lGik1gC8

这是子vc代码:

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    self.initUI()
    super.enableLargeNavigationTitle(title: (self.favorty?.sellerProduct?.product?.name)!)

}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

}
override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    super.removeTitleImage()

}
override func viewDidDisappear(_ animated: Bool) {
    super.viewDidDisappear(true)        

}

这里是enableLargeNavigationBar函数:

func enableLargeNavigationTitle(title: String)  {

    self.navigationController?.view.backgroundColor = VVUtility.splashBackGroundColor()
    self.navigationItem.title = "\(title)".localized()
    self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.white, NSAttributedStringKey.font : VVUtility.normalFontWithPlusSize(increaseSize: -2.0)]

    if #available(iOS 11.0, *) {

        self.navigationController?.navigationBar.isTranslucent = false
        self.navigationController?.navigationBar.prefersLargeTitles = true
        self.navigationController?.navigationBar.backgroundColor = VVUtility.splashBackGroundColor()
        self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.white, NSAttributedStringKey.font : VVUtility.normalFontWithPlusSize(increaseSize: 0.0)]
    } else {
        // Fallback on earlier versions
    }

}

disableLargeNavigation函数:

func disableLargeNavigationTitle() {
    if #available(iOS 11.0, *) {
        self.navigationController?.navigationItem.largeTitleDisplayMode = .never
        self.navigationController?.navigationBar.prefersLargeTitles = false

    } else {
        // Fallback on earlier versions
    }
}

这里是父vc代码:

 override func viewDidLoad() {
    super.viewDidLoad()
    self.initUI()
    self.getData()
    super.disableLargeNavigationTitle()
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    self.navigationController?.navigationBar.addSubview(searchBarBoxView)
    self.timerDelegate?.startTimer()
    self.navigationController?.setNavigationBarHidden(false, animated: true)
}
override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    self.searchBarBoxView.removeFromSuperview()
    self.timerDelegate?.stopTimer()
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    self.tabBarController?.delegate = self
    super.disableLargeNavigationTitle()

}

1 个答案:

答案 0 :(得分:0)

这对我有用。尝试将此代码放在每个视图控制器的awakeFromNib()中,并根据需要更改设置。

override func awakeFromNib() {
    // Large titles
    if #available(iOS 11.0, *) {
        navigationController?.navigationBar.prefersLargeTitles = false // This could be true for other view controller
        navigationItem.largeTitleDisplayMode = .never // This could be .always for other view controller
        navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.black] // Or whatever you want
    } else {
        // Handle iOS 10 and below (no large titles)
    }
}

或者,我认为你可以在故事板中做到这一点,但这对我不起作用。