我有一個問題,我目前的配置是:UISearchController - 不與子視圖控制器打得很好
的UITableViewController - >的UINavigationController - >一個視圖控制器換出2個視圖控制器。
每個子視圖控制器都有一個與它們關聯的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的孩子是否有任何奇怪的約束? – brkr
我不確定你的意思。 UISearchController獨立於子VC上的自動佈局約束。唯一會受到影響的是UISearchBar(它在展示模式中注入UISearchController時位於容器中)。 UISearchBar已將適當設置的掩碼約束轉換爲不使用指定的基於約束的佈局。 – TheCodingArt
絕大多數情況下,這絕對是溝通東西的東西,無論傳達什麼上下文偏移,我都知道。 – TheCodingArt