嵌入UISplitViewController时,UINavigationBar中缺少iOS11 UISearchBar [英] iOS11 UISearchBar missing in UINavigationBar when embedded in UISplitViewController

查看:131
本文介绍了嵌入UISplitViewController时,UINavigationBar中缺少iOS11 UISearchBar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UISplitViewController navigationItem.searchController 方法时,似乎发生了奇怪的事情>。

Strange things seem to happen when using the new iOS 11 navigationItem.searchController method on a detail view of a UISplitViewController.

searchBar 部分在第一个演示文稿中显示为空格,然后显示错误的 UITableViewController ,但在 UITableViewController 的几次推送和弹出后进行自我修正。

The searchBar partly appears as a blank space on the first presentation, then appears in the wrong UITableViewController, but corrects itself after a few push and pops of UITableViewController.

我曾经把 searchBar 放在 tableHeaderView 中,但是我根据WWDC建议更改了代码:

I used to put the searchBar in the tableHeaderView, but I changed the code according to the WWDC recommendation:

if (@available(iOS 11.0, *)) {
    self.navigationItem.searchController = self.searchController;
    self.navigationItem.hidesSearchBarWhenScrolling = NO;
} else {
    self.tableView.tableHeaderView = self.searchController.searchBar;
}

此示例使用标准示例代码( UISplitViewController 以及针对iOS 11更新的 UISearchController 的Apple演示(使用单个 UITableViewController ))。

This example is using standard sample code (default project for UISplitViewController and the Apple demo of UISearchController updated for iOS 11 (using a single UITableViewController)).

包含 searchController 的初始视图如下所示:

The initial view containing the searchController looks like this:

点击 UITableView 项会产生此:

但点击<$后c $ c> UITableView item并返回两次 - 看起来应该如此:

However after clicking on a UITableView item and returning twice - it looks as it should:

和:

我试图确定为什么 UISearchController 的Apple示例有效并且我的代码没有。主要区别在于它嵌入在详细信息视图中的 UISplitViewController 中。这意味着如果在紧凑模式下显示在堆栈中有一个额外的 UINavigationController 。我发现如果我的seque避免了额外的 UINavigationController - 它可以正常工作(但会中断设备轮换)。同样地,将segue更改为模态允许它工作。

I was trying to determine why the Apple example for UISearchController worked and my code didn't. The main difference was it was embedded in UISplitViewController in the Detail View. Which means if shown in Compact mode has an extra UINavigationController in the stack. I found if my seque avoided the extra UINavigationController - it works correctly (but breaks device rotation). Similarly change the segue to modal allows it to work.

我注意到这与旧的问题类似:带有新UISearchController问题的UISplitViewController使用UISearchBar

I note this is similar to this old question: UISplitViewController with new UISearchController issue with UISearchBar

我创建了一个演示问题的示例项目(示例代码:searchControllerDemo

I have created a sample project that demonstrates the problem (sample code: searchControllerDemo)

我对发生了什么感到难过。所以任何帮助都会非常感激。

I'm stumped as to what is going on. So any help would be very much appreciated.

推荐答案

自从这次爆发以来已经有一段时间了,但是想到这里留下一张纸条给谁将面临同样的问题...

It's been a while since this erupted but thought to leave a note here for whoever will face the same issue...

在紧凑宽度设备上,从主设备到细节的细分时,细节导航控制器位于主视图控制器的顶部,与常规设备不同两个导航控制器具有各自独立的根视图控制器的宽度。

On compact width devices, upon segueing from master to detail, the detail navigation controller is on top of the master view controller, unlike regular width where the two navigation controllers have their own separate root view controllers.

因此,详细视图的 UINavigationController 使用 UISplitViewControllerDelegate 方法: splitViewController(_:showDetail:sender :) ,需要在combact宽度设备中的segue上删除controller

So, the UINavigationController of the detail view controller needs to be removed upon segue in combact width devices using UISplitViewControllerDelegate method: splitViewController(_:showDetail:sender:)

func splitViewController(_ splitViewController: UISplitViewController, showDetail vc: UIViewController, sender: Any?) -> Bool {
    if splitViewController.isCollapsed, let navController = vc as? UINavigationController {
        if let detailVC = navController.topViewController {
            splitViewController.showDetailViewController(detailVC, sender: sender)
            return true
        }
    }
    return false
}

这篇关于嵌入UISplitViewController时,UINavigationBar中缺少iOS11 UISearchBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆