UISearchController SearchBar 在旋转期间处于活动状态时未对齐 [英] UISearchController SearchBar misaligns while active during rotation
问题描述
我已经为 UISearchController 的搜索栏苦苦挣扎了一段时间.我需要在 tableview 上实现搜索功能,但与传统方法不同,我没有将搜索栏放在 table 标题视图上.相反,我创建了一个 UIView
并将搜索栏添加为它的子视图.充当搜索栏容器的 UIView
在具有自动布局的故事板上正确设置了其约束.
I have been struggling with UISearchController's search bar for quite a while now. I need a search feature to be implemented on a tableview but unlike conventional methods, I didn't put the search bar on a table header view. Instead, I created a UIView
and added the search bar as a subview to it. The UIView
which acts as a container to search bar has its constraints properly set on the storyboard with auto layout.
这是我的代码.请注意,我以编程方式执行此操作是因为 UISearchDisplayController 和 UISearchBar 自 iOS 8 起已被弃用,而支持 UISearchController,并且尚未加入 UIKit.
Here are my codes for it. Note that I did this programmatically because UISearchDisplayController and UISearchBar has been deprecated as of iOS 8 in favour of UISearchController and has yet to come to UIKit.
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.autoresizingMask = .FlexibleRightMargin
searchController.searchBar.delegate = self
definesPresentationContext = true
self.searchContainerView.addSubview(searchController.searchBar)
但是,我确实注意到搜索栏在旋转过程中的一种奇怪行为.当它在纵向上处于活动状态时,我将模拟器旋转到横向,然后按取消,搜索栏将返回纵向宽度.
However, I did notice one odd behaviour of the search bar during rotation. When it is active on Portrait, I rotate the simulator to Landscape, and press Cancel, the search bar goes back to Portrait Width.
反之亦然.
我将不胜感激,因为我至少已经在这个问题上工作了好几天了.非常感谢
I would appreciate any ideas or maybe some hints towards the correct direction to solve this as I have been at this for days at least. Thank you very much
推荐答案
经过这么多天的挣扎:
风景的肖像
- 使 SearchBar/SearchController 在纵向中处于活动状态
- 旋转到横向
- 按取消,SearchBar 将返回纵向宽度
从风景到肖像
- 使 SearchBar/SearchController 在横向中处于活动状态
- 旋转到纵向
- 按取消,SearchBar 将返回横向宽度
我终于自己解决了.似乎当用户在 SearchBar 上按下取消时,ViewController 将调用 viewDidLayoutSubviews
因此我试图通过在 viewDidLayoutSubviews
中调用此函数来重置宽度:
I finally solved it on my own. It seems that when a user presses Cancel on the SearchBar, the ViewController will call viewDidLayoutSubviews
thus I tried to reset the width by calling this function in viewDidLayoutSubviews
:
func setupSearchBarSize(){
self.searchController.searchBar.frame.size.width = self.view.frame.size.width
}
但这并没有我想象的那么好.所以这就是我认为会发生的事情.当用户激活 SearchController/SearchBar 时,SearchController 会在调整自身大小之前保存当前帧.然后,当用户按下取消或关闭它时,它将使用保存的框架并将其调整为该框架大小.
But that didn't work as well as I thought. So here is what I think happens. When a user activates the SearchController/SearchBar, the SearchController saves the current frame before it resizes itself. Then when a user presses Cancel or dismisses it, it will use the saved frame and resize to that frame size.
因此,为了在我按下取消键时强制重新对齐其宽度,我必须在我的 VC 中实现 UISearchControllerDelegate 并检测何时取消 SearchController,然后再次调用 setupSearchBarSize()
.
So in order to force its width to be realigned when I press Cancel, I have to implement the UISearchControllerDelegate in my VC and detect when the SearchController is dismissed, and call setupSearchBarSize()
again.
以下是解决这个问题的相关代码:
Here are the relevant codes to solve this question:
class HomeMainViewController : UIViewController, UISearchControllerDelegate, UISearchResultsUpdating, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate {
@IBOutlet weak var searchContainerView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.autoresizingMask = .FlexibleRightMargin
searchController.searchBar.delegate = self
searchController.delegate = self
definesPresentationContext = true
self.searchContainerView.addSubview(searchController.searchBar)
setupSearchBarSize()
}
func setupSearchBarSize(){
self.searchController.searchBar.frame.size.width = self.view.frame.size.width
}
func didDismissSearchController(searchController: UISearchController) {
setupSearchBarSize()
}
override func viewDidLayoutSubviews() {
setupSearchBarSize()
}
}
这篇关于UISearchController SearchBar 在旋转期间处于活动状态时未对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!