[英]Using Back on Navigation Bar bugs UIBarButtonItem
I have a NavigationBar at the top of a TableView. 我在TableView的顶部有一个NavigationBar。 It looks nice opening/closing the search.
打开/关闭搜索看起来不错。
However, if I click on a button in a cell and get directed to another page (with segue); 但是,如果我单击单元格中的按钮并转到另一个页面(带有segue); and then use Back button to unwind, it seems like bugged.
然后使用“ 后退”按钮放松,好像很麻烦。
So it looks like it is pressed and opened but it shouldn't have. 因此,它看起来像是被按下和打开的,但不应这样做。 It should be looked like the top picture instead (just UIBarButtonItem - search button)
它应该看起来像最上面的图片(只是UIBarButtonItem-搜索按钮)
I couldn't figure out the issue creating this problem. 我无法弄清楚造成此问题的原因。
Please note that < Back
is created automatically and I didn't write any code to create it. 请注意,
< Back
是自动创建的,我没有编写任何代码来创建它。 Is there something I am doing wrong? 我做错什么了吗?
Update: Added some snippets... 更新:添加了一些片段...
First, created a different class for handling the search 首先,创建一个不同的类来处理搜索
class SearchBarViewController: UIViewController, UISearchBarDelegate {
var searchBar : UISearchBar?
var searchBarWrapper : UIView?
var searchBarButtonItem : UIBarButtonItem?
func constructSearchBar()
{
searchBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Search, target: self, action: "showSearchBar")
self.navigationItem.rightBarButtonItem = searchBarButtonItem
}
func showSearchBar() {
// styling & configuration
}
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
UIView.animateWithDuration(0.2, animations: {
self.searchBar?.resignFirstResponder()
self.searchBarWrapper?.alpha = 0
}, completion: { (success) -> Void in
self.searchBar = nil
self.searchBarWrapper = nil
self.navigationItem.rightBarButtonItem = self.searchBarButtonItem
})
}
}
And my ViewController: 还有我的ViewController:
class ViewController: SearchBarViewController {
override func viewDidLoad() {
super.viewDidLoad()
constructSearchBar()
}
}
Regarding to emrys57 's answer, I tried adding viewWillAppear()
in my ViewController but I couldn't make it work, as my cancel looks a little different: 关于emrys57的答案,我尝试在ViewController中添加
viewWillAppear()
,但是由于取消似乎有些不同,因此无法正常工作:
override func viewWillAppear(animated: Bool) {
super.viewDidAppear(animated)
// Here, I couldn't figure out what to put because
// my searchBarCancelButtonClicked() needs searchBar and
// forces me to use (!) but then it says, it's optional..
}
The answer is... 答案是...
override func viewWillAppear(animated: Bool) {
super.viewDidAppear(animated)
navigationItem.titleView = nil
constructSearchBar()
}
You have not posted code, so it's not entirely clear what's gone wrong. 您尚未发布代码,因此尚不清楚发生了什么问题。 Using
UISearchBar
, I think you must be handling the buttons separately yourself, as opposed to using UISearchController
. 使用
UISearchBar
,我想你必须单独处理自己的按钮,而不是使用UISearchController
。 I think that you may not be clearing away the search bar when coming back from the second VC. 我认为从第二个VC返回时,您可能不会清除搜索栏。 This code clears out the search bar in
viewWillAppear
: 此代码清除了
viewWillAppear
的搜索栏:
class ViewController: UIViewController {
var cancelButton: UIBarButtonItem?
var searchButton: UIBarButtonItem?
override func viewDidLoad() {
super.viewDidLoad()
cancelButton = UIBarButtonItem(barButtonSystemItem: .Cancel, target: self, action: Selector("searchCancelPressed:"))
searchButton = UIBarButtonItem(barButtonSystemItem: .Search, target: self, action: Selector("searchPressed:"))
}
override func viewWillAppear(animated: Bool) {
super.viewDidAppear(animated)
searchCancelPressed(nil)
}
func searchPressed(sender: AnyObject) {
navigationItem.titleView = UISearchBar()
navigationItem.rightBarButtonItem = cancelButton
}
func searchCancelPressed(sender: AnyObject?) {
navigationItem.titleView = nil
navigationItem.rightBarButtonItem = searchButton
}
}
and that is working nicely for me when I do a push from a button to the second VC and then hit back
. 当我从按钮推到第二个VC然后
back
时,这对我来说很好。
Following the edit to the original question, this code seems to work, although it may not be the most elegant way of constructing the answer: 在对原始问题进行编辑之后,此代码似乎可行,尽管它可能不是构造答案的最优雅的方式:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
navigationItem.titleView = nil
searchBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Search, target: self, action: "showSearchBar")
navigationItem.rightBarButtonItem = searchBarButtonItem
}
The function constructSearchBar
no longer needs to be called in viewDidLoad
, and can be deleted. 不再需要在
viewDidLoad
调用函数constructSearchBar
,并且可以将其删除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.