iOS 14 中 UITableView 标题布局中的 UISearchBar 问题 [英] UISearchBar in UITableView header layout issue in iOS 14

查看:18
本文介绍了iOS 14 中 UITableView 标题布局中的 UISearchBar 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 tableview 标题中使用带有搜索栏的新 UISearchController.在纵向模式下一切正常,但是当我旋转到横向并且 uisearchbar 开始编辑它的框架时,它的框架超出了屏幕,剪掉了取消按钮.我创建搜索控制器的代码

tableView.tableHeaderView = searchController.searchBar

这是我的示例代码

当旋转回uisearchbar的纵向布局时仍然不正确如果我使用 UITableviewController 没有问题,但我必须使用 uitableview 来实现.iOS 14 中是否缺少任何设置?

注意:我必须使用 tableHeaderView 而不是 navigationItem.searchController = searchController

解决方案

尝试在相应的 UIViewController 方法中调整 viewWillTransition 方法中的搜索栏框架.viewWillTransition 每次在旋转期间都会被调用,使您可以在 UIViewController 旋转期间更改布局.

这是示例代码:

 override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {coordinator.animate(withsideTransition: { (UIViewControllerTransitionCoordinatorContext) -> Void in调整搜索栏框架()}, 完成: { (UIViewControllerTransitionCoordinatorContext) ->作废})super.viewWillTransition(to: size, with: coordinator)}

I am using the new UISearchController with searchbar inside tableview header.Everything works fine in portrait orientation but when I rotate to landscape and uisearchbar start editing its frame is out of screen,clipping the cancel button. my code for creating searchcontroller

tableView.tableHeaderView = searchController.searchBar

Here is my sample code UISearchController sample

Rotation has no frame issue in landscape but only when uisearchbar starts editing [

When rotate back to portrait layout of uisearchbar is still incorrect There is no issue if I use UITableviewController but I have to implement with uitableview.Is there any setting I am missing in iOS 14 ?

Note:I have to use tableHeaderView not navigationItem.searchController = searchController

解决方案

Try adjusting the frame of search bar in viewWillTransition method that in your corresponding UIViewController . viewWillTransition is called every time during rotation and enables you to change layout during rotation of your UIViewController.

here is example code:

    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    coordinator.animate( alongsideTransition: { (UIViewControllerTransitionCoordinatorContext) -> Void in
        adjustSearchbarFrame()
    }, completion: { (UIViewControllerTransitionCoordinatorContext) -> Void in
    })
    super.viewWillTransition(to: size, with: coordinator)
}

这篇关于iOS 14 中 UITableView 标题布局中的 UISearchBar 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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