【发布时间】:2016-09-06 19:58:23
【问题描述】:
我有一个问题,我的当前配置是:
UITableViewController -> UINavigationController -> 用于交换 2 个子视图控制器的 ViewController。
每个子视图控制器都有一个与之关联的 UISearchController。当 UISearchBar 激活时,它似乎从来没有正确的位置。
extension MySearchViewController: UISearchControllerDelegate {
func willPresentSearchController(searchController: UISearchController) {
var adjustedOrigin = searchController.searchBar.frame.origin
//FIXME: There's some odd behavior with embedded child VCs where the status bar adjustments are not taken into consideration
adjustedOrigin.y += UIApplication.sharedApplication().statusBarFrame.height
searchController.searchBar.frame.origin = adjustedOrigin
definesPresentationContext = false
navigationController?.definesPresentationContext = true
navigationController?.extendedLayoutIncludesOpaqueBars = true
}
func didPresentSearchController(searchController: UISearchController) {
definesPresentationContext = true
}
func didDismissSearchController(searchController: UISearchController) {
var adjustedOrigin = searchController.searchBar.frame.origin
//FIXME: There's some odd behavior with embedded child VCs where the status bar adjustments are not taken into consideration
adjustedOrigin.y -= UIApplication.sharedApplication().statusBarFrame.height
searchController.searchBar.frame.origin = adjustedOrigin
}
}
您可以在上面看到,当我尝试手动更正 UISearchBar 的偏移量时,原点已被调整,这是迄今为止不受欢迎的。我尝试(在许多区域)从情节提要和通过代码在层次结构中的几乎所有位置检查显示。我似乎找不到抵消的罪魁祸首。默认情况下,UISearchBar 会一直显示在状态栏下方,如下所示:
这没有我的手动调整,这仍然有点偏离。
有人对此有解决方案吗?
编辑 1:
进一步证明父 VC 中的某些东西弄乱了偏移量,UISearchBar 的实际 superView 在呈现时偏移了 -20。因此,以下内容更正了该问题:
import UIKit
class MySearchController: UISearchController {
override func viewDidLoad() {
super.viewDidLoad()
edgesForExtendedLayout = .Top
extendedLayoutIncludesOpaqueBars = true
automaticallyAdjustsScrollViewInsets = true
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
print(active)
if active && searchBar.superview!.frame.origin != CGPoint.zero {
searchBar.superview?.frame.origin = CGPoint.zero
}
}
}
【问题讨论】:
-
你有没有检查过VC的孩子是否有任何奇怪的约束?
-
我不确定你的意思。 UISearchController 与子 VC 上的自动布局约束是分开的。唯一会受到影响的是 UISearchBar(在演示模式期间注入 UISearchController 时位于容器中)。 UISearchBar 已将掩码约束设置适当地转换为不使用指定的基于约束的布局。
-
在大多数情况下,这绝对是在链上传达一些东西,无论是传达上下文偏移量,我都知道。
-
@TheCodingArt 你有没有设法解决这个问题?
-
@jbouaziz 除了我用来手动纠正我看到的行为的小技巧之外,没有具体的答案。我将在下面附上一个答案,也许至少可以提供帮助。
标签: ios swift layout uisearchbar uisearchcontroller