Adjusting navigationItem.titleView's frame?

前端 未结 13 1553
小蘑菇
小蘑菇 2021-01-31 04:14

I noticed that the titleView\'s position depends on the rightbarbutton title. How can I set the frame of the titleView to be in the center instead?

I\'ve tried setting

13条回答
  •  执笔经年
    2021-01-31 04:44

    I was implementing a two row title similar to something WhatsApp is doing (title and subtitle).

    I fixed this problem by subclassing UINavigationController and overriding viewDidLayoutSubviews().

    My titleView looks like the following (in a viewController):

    let frame = self.navigationController?.navigationBar.bounds ?? CGRect.zero
    
    // This is a container view for the two labels, laying them out vertically
    let titleView = UIStackView(arrangedSubviews: [self._titleLabel, self._promptLabel])
    titleView.axis = .vertical
    titleView.alignment = .center
    titleView.distribution = .fill
    titleView.frame = frame // set the frame to the navbarFrame initially
    titleView.spacing = 0
    
    // The wrapper is necessary for laying out the stackView as the titleView
    let wrapperView = UIView(frame: frame)
    wrapperView.addSubview(titleView)
    
    self.navigationItem.titleView = wrapperView
    

    My viewDidLayoutSubviews():

    guard let navigationItem = self.topViewController?.navigationItem,
          let titleView = navigationItem.titleView,
          let wrapperView = titleView.subviews[0] as? UIStackView else { return }
    
    var width : CGFloat = 0
    
    for view in wrapperView.arrangedSubviews {
        view.sizeToFit()
        width = max(width, view.frame.size.width)
    }
    
    titleView.frame.size.width = width
    titleView.frame.size.height = self.navigationBar.frame.height
    wrapperView.frame = titleView.bounds
    wrapperView.center.x = self.navigationBar.convert(self.navigationBar.center, to: titleView).x
    wrapperView.center.y = titleView.frame.height / 2
    
    self.navigationBar.layoutIfNeeded()
    

    Note: The trick is to have a wrapper view around your titleView which can be layed out by the navigation bar. Then adjust the frame of your titleView to what you desire.

提交回复
热议问题