简体   繁体   English

UISplitViewController:DetailViewController 中的 titleView 在横向上消失,预期行为?

[英]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.我正在使用navigationItem.titleView为 Master/Detail VC 在导航栏中添加自定义titleView On changing the orientation of the device to landscape, titleView under MasterViewController works fine, but for DetailViewController titleView disappears.在将设备的方向更改为横向时, MasterViewController下的titleView工作正常,但对于DetailViewController titleView消失了。 On changing the orientation back to portrait titleView appears back for DetailViewController .在将方向改回纵向时, titleView出现在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完整源代码在这里: https : //github.com/avitron01/SplitViewControllerIssue/tree/master/MathMonsters

Video highlighting the issue: https://vimeo.com/336288580突出问题的视频: https : //vimeo.com/336288580

I had the same issue.我遇到过同样的问题。 It seems an iOS bug.这似乎是一个iOS错误。 My workaround was to reassign the title view on every view layout.我的解决方法是在每个视图布局上重新分配标题视图。 I used this piece of code in my DetailViewController :我在我的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 .对于那些偶然发现这一点的人,另请参阅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使用自定义 UIView 告诉 iOS 使用UIView.layoutFittingExpandedSize将其intrinsicContentSize处理得尽可能大
  • use widthAnchor / heightAnchor constraints to set width and height of your view使用widthAnchor / heightAnchor约束来设置视图的宽度和高度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iOS 14 中的 UISplitViewController 在横向模式下按预期工作,但在纵向模式下不起作用 - UISplitViewController in iOS 14 works as intended in landscape mode, but not in portrait UISplitViewController,重用DetailViewController(Swift) - UISplitViewController, reuse DetailViewController (Swift) 在横向方向上更改UISplitViewController的UITableViewController的背景图像 - Changing the background image of a UITableViewController Master of a UISplitViewController in Landscape orientation 更新UISplitViewController子类中的DetailViewController框架 - Update frame of DetailViewController in subclass of UISplitViewController UINavigationItem TitleView 消失 - UINavigationItem TitleView disappears UINavigationBar titleView用于不同的方向 - UINavigationBar titleView for different orientation 仅在UISplitViewController的DetailViewController中呈现视图控制器 - Presenting a View controller only in the DetailViewController of UISplitViewController 为什么detailViewController不在UISplitViewController.viewControllers中? - Why detailViewController is not in UISplitViewController.viewControllers anymore? 在UISplitViewController应用程序中从DetailViewController获取MasterViewController - getting the MasterViewController from the DetailViewController in a UISplitViewController app titleView图像立即出现并消失 - titleView image appears and disappears immediately
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM