我在我的 View 中创建了一个导航栏,它应该左右各有 2 个按钮。问题是无论我做什么它都不会显示按钮/ View 。我该怎么做?这是到目前为止我的代码。
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "Aerovias Brasil NF", size: 30)!, NSForegroundColorAttributeName: UIColor.whiteColor()]
navTitleView = UIView(frame: CGRectMake(0, 0, 100, 44))
titleLabel.text = "SHOPOP"
navTitleView?.addSubview(titleLabel)
self.navigationItem.titleView?.addSubview(navTitleView!)
navigationHeight = UIApplication.sharedApplication().statusBarFrame.height+self.navigationController!.navigationBar.bounds.height
searchBar = UISearchBar(frame: CGRectZero)
searchBarWrapper = UINavigationBar(frame: CGRectMake(0, 100, self.navigationController!.navigationBar.bounds.size.width, self.navigationHeight!))
var buttonSearchBar:UIBarButtonItem = UIBarButtonItem(customView: searchBar!)
var cancelButton:UIBarButtonItem = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)
searchBarWrapper?.topItem?.leftBarButtonItem = buttonSearchBar
searchBarWrapper?.topItem?.rightBarButtonItem = cancelButton
self.navigationController?.view.addSubview(searchBarWrapper!)
}
@IBAction func showSearchBar(sender: UIBarButtonItem) {
searchBar?.becomeFirstResponder()
UIView.animateWithDuration(0.25, animations: {
// Optional chaining may return nil
self.searchBarWrapper!.center = CGPointMake(self.navigationController!.view.center.x, self.navigationHeight!/2)
// return
}, completion: {
finished in
println("Basket doors opened!")
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func hideSearchBar(sender: UIBarButtonItem) {
UIView.animateWithDuration(0.25, animations: {
// Optional chaining may return nil
self.searchBarWrapper!.center = CGPointMake(self.navigationController!.view.center.x, -self.navigationHeight!/2)
self.searchBar?.resignFirstResponder()
// return
}, completion: {
finished in
println("Basket doors opened!")
})
最佳答案
根据对您问题的更改,您应该查看 UISearchController class .粗略地浏览一下文档让我相信,在您正确配置它之后,您可以将它推送到 View Controller 堆栈上,它就会像您想要的那样运行。
旧答案:
虽然我不确定是否将 UISearchBar 包装在 UIBarButtonItem 中,但我可以肯定地说,这是将 UINavigationBar 添加到 UINavigationController 的错误方法:
self.navigationController?.view.addSubview(searchBarWrapper!)
为导航栏提供项目的正确方法是覆盖 UIViewController 的 navigationItem
属性并返回自定义的 UINavigationItem
实例。这样,当您从 UINavigationController 的堆栈中推送和弹出 View Controller 时,导航栏将自动更新。
这是我的一个项目中 UIViewController 子类的示例。
private var customNavigationItem = UINavigationItem(title:nil);
override var navigationItem:UINavigationItem
{
get
{
customNavigationItem.title = self.title;
customNavigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem:.Cancel,
target:self, action:"dismissForm:");
customNavigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem:.Save,
target:self, action:"saveForm:");
return customNavigationItem;
}
}
这样做将确保 UINavigationController 提供的所有“免费”功能都按预期工作。 扰乱 UINavigationController 的 View 通常不是一个好主意。
关于ios - UIBarButton 项目不会显示在导航栏中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27043004/