Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在swift中向下滑动时的搜索栏_Swift_Uinavigationcontroller_Storyboard_Uisearchbar - Fatal编程技术网

在swift中向下滑动时的搜索栏

在swift中向下滑动时的搜索栏,swift,uinavigationcontroller,storyboard,uisearchbar,Swift,Uinavigationcontroller,Storyboard,Uisearchbar,我正在为iOS开发一款社交媒体应用程序 MyViewController当前嵌入在NavigationController中。 在我的新闻提要屏幕上,当用户向下滑动(向上滑动时隐藏)时,我需要显示一个搜索栏,如果用户在其中键入内容,搜索结果将显示在新闻提要屏幕的顶部 我已经尝试过修补这个,但我对iOS还很陌生,到目前为止还没能让它工作 如果有任何帮助,我们将不胜感激,请记住,我只编写了几周的iOS程序,因此深入了解会有所帮助。 谢谢大家! 首先,您需要实现uisweegesturecognize

我正在为iOS开发一款社交媒体应用程序

My
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的滚动