Dan*_*van 6 storyboard uinavigationcontroller uisearchbar swift
我正在为iOS开发一个社交媒体应用程序
我ViewControllers目前已经嵌入了NavigationController.在我的新闻提要屏幕上,我需要在用户向下滑动时显示搜索栏(并在他向上滑动时隐藏它),其中如果用户输入内容,搜索结果将显示在新闻提要屏幕的顶部.
我试图修补这个,但我对iOS很新,到目前为止还没有设法让它工作.
任何帮助将grately理解和记住,我只几个星期的编程的iOS这样一些深层次的将是有益的.谢谢 !
首先,您需要实施UISwipeGestureRecognizer
将该setup()函数包含在viewDidAppear
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)
}
}
Run Code Online (Sandbox Code Playgroud)
然后你的两个函数 up 和 down
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
})
}
Run Code Online (Sandbox Code Playgroud)
您可以添加Bool isShowing以避免不必要的动画。然后,实现搜索栏委托textDidChange以根据用户键入更改搜索结果。
func searchBar(_ searchBar: UISearchBar,textDidChange searchText: String)`
Run Code Online (Sandbox Code Playgroud)
您现在需要做的就是在UISearchController.
注意
使用向上/向下滑动动作可能会干扰屏幕的滚动。UIScreachController
| 归档时间: |
|
| 查看次数: |
1773 次 |
| 最近记录: |