[我在iOS 13中无法使用navigationItem.searchController
。搜索控制器的搜索栏最初是关闭的,但是在滚动表格视图后它不会折叠回去。当我尝试隐藏栏时,它会“反弹”。
[此外,出现搜索栏后,导航栏也会变成白色。(导航栏具有自定义的颜色;请参阅所附的屏幕截图。)请注意,在iOS <13中不是这种情况。
这是iOS 13中的错误吗?如果是,是否有变通办法来保持导航栏的色调颜色并在向上滚动表视图时隐藏搜索栏?
[在表格视图中填充单元格以填充屏幕时,搜索栏似乎隐藏了。
以下是我的ViewController
的代码,它嵌入在UINavigationController
中:
class ViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.searchController = UISearchController(searchResultsController: nil)
}
}
屏幕截图:
我设法使用新的UINavigationBarAppearance
API'修复'了新的行为,但是我仍然无法获得导航栏的原始半透明外观并使它折叠。
UINavigationBar.appearance().isTranslucent = true
似乎无法与新API一起使用,因此我只是使用颜色选择器将颜色调整为与半透明版本匹配。
而且,似乎也没有办法正确折叠搜索栏。
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?)
-> Bool
{
if #available(iOS 13.0, *) {
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = .redAdjusted // adjusted to match the translucent version
appearance.titleTextAttributes = [.foregroundColor: UIColor.white]
appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
UINavigationBar.appearance().standardAppearance = appearance
UINavigationBar.appearance().scrollEdgeAppearance = appearance
} else {
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().barTintColor = .red
UINavigationBar.appearance().isTranslucent = true
}
}
}