Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 232
Avinash Avatar asked May 15 '19 08:05

Avinash


1 Answers

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
    }
}
like image 108
Sergio Avatar answered Nov 05 '22 23:11

Sergio