Add Navigation Bar in UICollectionView in swift 4,

2019-02-26 01:14发布

问题:

After upgrading the iOS 11 and swift 4(Xcode 9), I got lots of UI problem. :(
The most unsolvable problem is Navigation Problem.
I put the Navigation Bar in UICollectionView Controller.
Navigation bar height does not show properly and however I write the code.

I set navigation bar height "100". I write this code in ViewDidLoad().

  if #available(iOS 11.0, *) {

        print("IOS 11 Screen")

        UINavigationBar.appearance().largeTitleTextAttributes = [
            NSForegroundColorAttributeName: UIColor.white,

        ]
        navigationItem.largeTitleDisplayMode = .never
        let height: CGFloat = 100 //whatever height you want
        let bounds = self.navigationController!.navigationBar.bounds
        self.navigationController?.navigationBar.frame = CGRect(x: 0, y: 0, width: bounds.width, height: bounds.height + height)


    } else {
        print("lower than IOS 11 Screen")
        // Fallback on earlier versions
    }

 collectionview?.frame = CGRect(x: 0, y: 10, width: UIScreen.main.bounds.width, height: (UIScreen.main.bounds.height + 20)) <br>

 override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    let height: CGFloat = 100 //whatever height you want
    let bounds = self.navigationController!.navigationBar.bounds
    self.navigationController?.navigationBar.frame = CGRect(x: 0, y: 0, width: bounds.width, height: bounds.height + height)

}


override func viewWillAppear(_ animated: Bool) {
        let height: CGFloat = 100 //whatever height you want
        let bounds = self.navigationController!.navigationBar.bounds
        self.navigationController?.navigationBar.frame = CGRect(x: 0, y: 0, width: bounds.width, height: bounds.height + height)
    }

However, I tried with this code. Nothing works for me.


I also tried to open the storyboard with source code view and I edit the height of Navigation bar. But, it also does not work.
Can anyone help me please? I have been trying to solve this problem since last two days.

回答1:

Please check :

override func viewDidLoad() {
    if #available(iOS 11.0, *) {
        print("IOS 11 Screen")
        UINavigationBar.appearance().largeTitleTextAttributes = [
            NSForegroundColorAttributeName: UIColor.white,
        ]
        self.navigationController?.navigationBar.prefersLargeTitles = true // added this line
        navigationItem.largeTitleDisplayMode = .never
    } else {
        print("lower than IOS 11 Screen")
        // Fallback on earlier versions
    }
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    let height: CGFloat = 100 //whatever height you want
    let bounds = self.navigationController!.navigationBar.bounds
    self.navigationController?.navigationBar.frame = CGRect(x: 0, y: 0, width: bounds.width, height: bounds.height + height)
}