在swift中向下滑动时的搜索栏
我正在为iOS开发一款社交媒体应用程序 My在swift中向下滑动时的搜索栏,swift,uinavigationcontroller,storyboard,uisearchbar,Swift,Uinavigationcontroller,Storyboard,Uisearchbar,我正在为iOS开发一款社交媒体应用程序 MyViewController当前嵌入在NavigationController中。 在我的新闻提要屏幕上,当用户向下滑动(向上滑动时隐藏)时,我需要显示一个搜索栏,如果用户在其中键入内容,搜索结果将显示在新闻提要屏幕的顶部 我已经尝试过修补这个,但我对iOS还很陌生,到目前为止还没能让它工作 如果有任何帮助,我们将不胜感激,请记住,我只编写了几周的iOS程序,因此深入了解会有所帮助。 谢谢大家! 首先,您需要实现uisweegesturecognize
ViewController
当前嵌入在NavigationController
中。
在我的新闻提要屏幕上,当用户向下滑动(向上滑动时隐藏)时,我需要显示一个搜索栏,如果用户在其中键入内容,搜索结果将显示在新闻提要屏幕的顶部
我已经尝试过修补这个,但我对iOS还很陌生,到目前为止还没能让它工作
如果有任何帮助,我们将不胜感激,请记住,我只编写了几周的iOS程序,因此深入了解会有所帮助。
谢谢大家! 首先,您需要实现
uisweegesturecognizer
将setup()
func setup() {
let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(down))
swipeDown.direction = .down
let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(up))
swipeUp.direction = .up
self.view.addGestureRecognizer(swipeDown)
self.view.addGestureRecognizer(swipeUp)
searchBar = UISearchBar(frame: CGRect(x: 0.0, y: 0.0, width: self.view.frame.size.width, height: 40.0))
if let searchBar = searchBar
{
searchBar.backgroundColor = UIColor.red
self.view.addSubview(searchBar)
}
}
然后你的两个函数上下运行
func down(sender: UIGestureRecognizer) {
print("down")
//show bar
UIView.animate(withDuration: 1.0, animations: { () -> Void in
self.searchBar!.frame = CGRect(x: 0.0, y: 64.0, width: self.view.frame.width, height: 40.0)
}, completion: { (Bool) -> Void in
})
}
func up(sender: UIGestureRecognizer) {
print("up")
UIView.animate(withDuration: 1.0, animations: { () -> Void in
self.searchBar!.frame = CGRect(x: 0.0, y: 0.0, width: self.view.frame.width, height: 40.0)
}, completion: { (Bool) -> Void in
})
}
您可以添加Bool isShowing
,以避免不必要的动画。然后,实现搜索栏委托textDidChange
,根据用户类型更改搜索结果
func searchBar(_ searchBar: UISearchBar,textDidChange searchText: String)`
您现在需要做的就是在UISearchController
中显示结果
注意
使用向上/向下滑动可能会干扰UIScreachController的滚动