Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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
Filter Swift 3:将搜索栏添加到UIViewController_Filter_Swift3_Uicollectionview_Searchbar - Fatal编程技术网

Filter Swift 3:将搜索栏添加到UIViewController

Filter Swift 3:将搜索栏添加到UIViewController,filter,swift3,uicollectionview,searchbar,Filter,Swift3,Uicollectionview,Searchbar,我正在尝试在Swift 3中的导航栏中添加搜索/筛选选项。目前,我正在从以前的视图推送到当前UICollectionView,所有这些视图都位于NavigationViewController堆栈上 在当前的UICollectionViewController中,我设置了一个UISearchBar对象,如下所示: let searchBar: UISearchBar = { let sb = UISearchBar() sb.placeholder = "Enter search

我正在尝试在Swift 3中的导航栏中添加搜索/筛选选项。目前,我正在从以前的视图推送到当前UICollectionView,所有这些视图都位于NavigationViewController堆栈上

在当前的UICollectionViewController中,我设置了一个UISearchBar对象,如下所示:

let searchBar: UISearchBar = {
    let sb = UISearchBar()
    sb.placeholder = "Enter search term"
    return sb
}()
在collectionViewController中,viewdidload()我设置了以下内容:

// searchbar
let navBar = self.navigationController?.navigationBar
navigationController?.navigationBar.addSubview(searchBar)


searchBar.anchor(top: navBar?.topAnchor, left: navBar?.leftAnchor   , bottom: navBar?.bottomAnchor, right: navBar?.rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0  )
锚定是通过自定义扩展完成的,尽管我不认为它会产生任何影响,但如下所示:

extension UIView{
    // anchor
    func anchor(top: NSLayoutYAxisAnchor?, left: NSLayoutXAxisAnchor?, bottom: NSLayoutYAxisAnchor?, right: NSLayoutXAxisAnchor?,paddingTop: CGFloat, paddingLeft: CGFloat, paddingBottom: CGFloat, paddingRight: CGFloat, width: CGFloat, height: CGFloat){

        translatesAutoresizingMaskIntoConstraints = false

        if let top = top{
            self.topAnchor.constraint(equalTo: top, constant: paddingTop).isActive = true
        }
        if let left = left{
            self.leftAnchor.constraint(equalTo: left, constant: paddingLeft).isActive = true
        }
        if let bottom = bottom{
            bottom.constraint(equalTo: bottom, constant: paddingBottom).isActive = true
        }
        if let right = right{
            rightAnchor.constraint(equalTo: right, constant: -paddingRight).isActive = true
        }
        if width != 0{
            widthAnchor.constraint(equalToConstant: width).isActive = true
        }
        if height != 0{
            heightAnchor.constraint(equalToConstant: height).isActive = true
        }

    }
}

但是,当我加载相应的UICollectionViewController时,我无法在navigationController导航栏区域中看到搜索栏。是否需要设置其他内容才能显示

您必须为
UISearchBar
指定一个
框架。试试这个:

let searchBar: UISearchBar = {
    let sb = UISearchBar(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 44))    //change the width & height according to your needs.
    sb.placeholder = "Enter search term"
    return sb
}