How to change the uinavigationbar title's position?

前端 未结 4 440
广开言路
广开言路 2021-01-03 03:30

I\'ve managed to change the navigationbar height by using my own navigationbar, but the title is still centered. I want it to be at the 72px position from the left.

4条回答
  •  粉色の甜心
    2021-01-03 04:10

    Setting custom view with left align.

    Setting custom view as leftBarButtonItem is not conveniently, because we will have a problems with back button.

    Setting custom view as subview of navigationBar is not conveniently because:

    • we can't to easy control width of custom view when right buttons are set. We can get overlapping custom view and right buttons
    • we need to add custom navTitleView and remove it every viewWillAppear and viewWillDissapear. This is additional, not clean code.

    So? I've just added width constraint to the customTitleView.

    Inside of CustomTitleView class:

      private func layoutViewsConfig() {
        // Width constraint needed to align view to left
        let widthConstraint = NSLayoutConstraint.init(item: self, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: UIScreen.main.bounds.width)
        widthConstraint.priority = .init(748)
        let heightConstraint = NSLayoutConstraint.init(item: self, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 50)
       self.addConstraint(widthConstraint)
       self.addConstraint(heightConstraint)    
      }        
    

    Then, inside of your ViewController:

    var navigationTitleView = NavigationSubtitleView() 
    self.navigationItem.titleView = navigationTitleView
    

    And we have:

提交回复
热议问题