ios - 即使键盘关闭,也保持搜索栏取消按钮色调颜色

标签 ios swift uisearchbar uisearchcontroller

我的导航栏中有一个带有取消按钮的搜索栏,当键盘关闭时,我的取消按钮会改变颜色并变为非事件状态,直到我再次单击它。有没有办法让我的取消按钮每次都保持相同的状态?

这是在我关闭键盘之前

enter image description here

这是在我关闭键盘之后

enter image description here

我希望我的取消按钮在键盘关闭前后保持不变。感谢您的帮助!

最佳答案

我想你做不到,我的方法是在右边做一个宽度的按钮,当用户按下搜索栏时,按钮宽度会改变,即使用户搜索并关闭键盘它仍处于事件状态,但按下时宽度为零,搜索栏结束编辑 enter image description here enter image description here 然后当按下取消时,只需使其宽度为零

实现:

1-在 tabbar 的右侧添加一个按钮-用于按钮的约束:您想要的底部约束,宽度为零,您想要的高度,尾部也为零。

2-对于tab bar的约束:尾部必须为0,其他随意

3- 不要忘记使用 UISearchBarDelegate

4-in viewdidload set yoursearchbar.delegate = self

5-

 func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
        //wcha.hey = hello.text!
        this will fire when tuching search bar for first time
        self.allview.layoutIfNeeded()
             yourcancelbutton.constant = your desired width

        }

6-

 @IBAction func yourcancelbutton(_ sender: Any) {

         yourbutton.constant = 0
        self.view.endEditing(true)
    }

关于ios - 即使键盘关闭,也保持搜索栏取消按钮色调颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51344811/

相关文章:

objective-c - 需要使用自签名证书和 http 身份验证来查看网站

ios - 最近更新后 SwiftUI ForEach 不工作

ios - SpriteKit 中的定时定时器

ios - 如何检测所有 CloudKit 更新何时发生?

Swift:从字节数据中提取 float

ios - UIPopoverController 中的 UISearchBar 在点击时移动

swift2 - 在 Swift 中过滤字典数组

ios - 从数据/模糊使用中获取值(value)

iOS:从导航栏中删除以编程方式添加的搜索栏

objective-c - ios中的字节数组