gpt4 book ai didi

ios - 搜索栏 - 导航栏中的取消按钮

转载 作者:可可西里 更新时间:2023-11-01 02:19:24 26 4
gpt4 key购买 nike

我在导航栏中做搜索栏。当我编辑时,导航栏右侧没有取消按钮。

尝试过:

func searchBarTextDidBeginEditing(searchBar: UISearchBar) {

var barButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Done, target: self, action: "")
self.navigationItem.rightBarButtonItem = barButton
}

enter image description here

enter image description here

编辑:

我像这样将搜索栏添加到导航栏:

lazy var searchBar:UISearchBar = UISearchBar(frame: CGRectMake(0, 0, 300, 20))

searchBar.placeholder = "Hľadať"
searchBar.barStyle = UIBarStyle.BlackTranslucent
var leftNavBarButton = UIBarButtonItem(customView:searchBar)
self.navigationItem.leftBarButtonItem = leftNavBarButton

最佳答案

这样做:

func searchBarShouldBeginEditing(searchBar: UISearchBar) -> Bool {
var cancelSearchBarButtonItem = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: "cancelBarButtonItemClicked")
self.navigationItem.setRightBarButtonItem(cancelSearchBarButtonItem, animated: true)
return true
}

在“取消”处理程序中:

func searchBarCancelButtonClicked(searchBar: UISearchBar) {
// closes the keyboard
searchBar.resignFirstResponder()

// If you are using a search controller
// self.searchDisplayControllerCustom.setActive(false, animated: true)

// remove the cancel button
self.navigationItem.setRightBarButtonItem(nil, animated: true)
}

func cancelBarButtonItemClicked() {
self.searchBarCancelButtonClicked(self.searchBar)
}

并为导航栏标题文本设置正确的颜色:

// This sets the textcolor for all navigation bars in the app
// Do this in the app delegate on startup
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()]

并添加委托(delegate):

searchBar.delegate = self

关于ios - 搜索栏 - 导航栏中的取消按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32025960/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com