简体   繁体   中英

UISplitViewController: titleView in DetailViewController disappears on landscape orientation, intended behaviour?

I'm adding a custom titleView inside a navigation bar using navigationItem.titleView for both Master/Detail VC. On changing the orientation of the device to landscape, titleView under MasterViewController works fine, but for DetailViewController titleView disappears. On changing the orientation back to portrait titleView appears back for DetailViewController . I have also attached a link for source code and video.

Is this an intended behavior or am I making a mistake from my side or is it an issue from Apple's side ?

//Custom Title View:
class TitleView: UIView {
    override func sizeThatFits(_ size: CGSize) -> CGSize {
        return CGSize(width: 50, height: 20)
    }
}

class DetailViewController: UIViewController {
    override func viewDidLoad() {
       super.viewDidLoad()
       //Adding titleView for Master/Detail VC:
       navigationItem.titleView = {
            //Setting frame size here, did not make any difference
            let view = TitleView(frame: .zero)
            view.backgroundColor = .red
            return view
       }()
    }
}

Full source code here: https://github.com/avitron01/SplitViewControllerIssue/tree/master/MathMonsters

Video highlighting the issue: https://vimeo.com/336288580

I had the same issue. It seems an iOS bug. My workaround was to reassign the title view on every view layout. I used this piece of code in my DetailViewController :

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    if let v = navigationItem.titleView {
        navigationItem.titleView = nil
        navigationItem.titleView = v
    }
}

For those who stumble upon this, see also iOS 11 navigationItem.titleView Width Not Set . Basically, there's two suggested workarounds:

  • use a custom UIView that tells iOS to treat its intrinsicContentSize to be as big as possible with UIView.layoutFittingExpandedSize
  • use widthAnchor / heightAnchor constraints to set width and height of your view

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM