Adjust position of bar button item when using large titles with iOS 11

后端 未结 5 934
暗喜
暗喜 2021-02-05 06:32

I am using the large title navbar with iOS 11, but when I add a bar button item it looks weird positioned in the same location as the original title navbar. I would like to move

5条回答
  •  独厮守ぢ
    2021-02-05 06:50

    The good way is you can adjust the navigation title if its large so that your bar button will adjust automatically. Here is the code. Also iOS mail application does the same thing for your reference.

    func adjustsTitle() {
     guard let font = UIFont(name: "Helvetica-Medium", size: 16) else { return }
     let label = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 20))
     label.textColor = UIColor.black
     label.textAlignment = .center
     label.text = navigationItem.title
     label.adjustsFontSizeToFitWidth = true
     navigationItem.titleView = label
    }
    

    Updated Answer If you want to adjust button below the title if it grows then in this case you need to load the custom view on your navigation bar.

    //Hide back button. Since you are going to have custom button
    navigationItem.hidesBackButton = true
    //Increase the height based on your view intrinsic content size
    navigationController?.navigationBar.frame.size.height = 100
    guard let yourCustomView = UINib(nibName: "yourCustomXib", bundle: nil).instantiate(withOwner: nil, options: nil).first as? YourCustomView else {
     fatalError("Missing yourCustomXib")
    }
    navigationController?.navigationBar.addSubview(yourCustomView)
    

提交回复
热议问题