UISearchBarDelegate方法未被调用

我有一个工作的搜索栏,不知从哪里,我甚至无法激活任何委托方法。我以编程方式创建了搜索栏。
这是我的初始化代码:

    self.searchBar.delegate = self
    self.searchBar.translatesAutoresizingMaskIntoConstraints = false
    self.searchBar.searchBarStyle = .minimal
    self.searchBar.isUserInteractionEnabled = true
    let image = self.getImageWithColor(color: UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 0.19), size: CGSize(width: self.customNavBar.frame.width * 0.9440, height: self.customNavBar.frame.height * 0.47727))
    searchBar.setSearchFieldBackgroundImage(image, for: .normal)
    self.customNavBar.addSubview(self.searchBar)
    var horizCenter = NSLayoutConstraint(item: self.searchBar, attribute: .centerX, relatedBy: .equal, toItem: self.customNavBar, attribute: .centerX, multiplier: 1, constant: 0)
    var vertConstraints = NSLayoutConstraint(item: self.searchBar, attribute: .top, relatedBy: .equal, toItem: self.customNavBar, attribute: .top, multiplier: 1, constant: 30)
    var widthConstraint = NSLayoutConstraint(item: self.searchBar, attribute: .width, relatedBy: .equal, toItem: self.customNavBar, attribute: .width, multiplier: 0.9440, constant: 0)
    var heightConstraint = NSLayoutConstraint(item: self.searchBar, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: self.customNavBar.frame.height * 0.47727 )
    NSLayoutConstraint.activate([horizCenter, vertConstraints, widthConstraint, heightConstraint])

这是我上面引用的getImageWithColor方法:
func getImageWithColor(color: UIColor, size: CGSize) -> UIImage {
    let rect = CGRect(x:0, y:0, width:size.width,height: size.height)
    let path = UIBezierPath(roundedRect: rect, cornerRadius: 5.0)
    UIGraphicsBeginImageContextWithOptions(size, false, 0)
    color.setFill()
    path.fill()
    let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()
    return image
}

我继续自定义搜索栏中的文本字段:
    UITextField.appearance().tintColor = UIColor.white
    let cancelButtonAttributes: NSDictionary = [NSForegroundColorAttributeName: UIColor.white, NSFontAttributeName : UIFont(name: "Gotham-Book", size: 14.0)!]
    UIBarButtonItem.appearance().setTitleTextAttributes(cancelButtonAttributes as? [String : AnyObject], for: UIControlState.normal)
    let textFieldInsideSearchBar = self.searchBar.value(forKey: "searchField") as? UITextField
    textFieldInsideSearchBar?.textColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)
    textFieldInsideSearchBar?.font = UIFont(name: "Gotham Medium", size: 20.0)
    textFieldInsideSearchBar?.clearsOnBeginEditing = true
    textFieldInsideSearchBar?.borderStyle = .none
    textFieldInsideSearchBar?.clearButtonMode = .whileEditing
    textFieldInsideSearchBar?.isUserInteractionEnabled = true

以及我所有的委托方法:
extension ListViewController : UISearchBarDelegate {


func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {

    print("\nfunc searchBarTextDidBeginEditing\n")
    self.hideDayDropDown()
    UIView.animate(withDuration: 0.25) {
        self.dayButton.alpha = 0.0
        self.dayButtonLabel.alpha = 0.0
        self.dayDropDownArrow.alpha = 0.0
        self.mapButton.alpha = 0.0
        self.mapButton.isEnabled  = false
        self.dayButton.isUserInteractionEnabled = false
        self.tableView.transform = CGAffineTransform(translationX: 0.0, y: -(self.tableView.frame.minY - self.customNavBar.frame.maxY))


        self.tableView.translatesAutoresizingMaskIntoConstraints = true
        self.tableView.frame = CGRect(x: 0, y: self.customNavBar.frame.maxY, width: self.view.frame.width, height: 300)

        self.dealOfTheDayLabel.alpha = 0.00
        self.exploreSpecialView.alpha = 0.0
        self.exploreSpecialsLabel.alpha = 0.0


    }
    searchActive = true
    self.searchBar.setShowsCancelButton(true, animated: true)

    UIView.animate(withDuration: 0.25) {
        self.searchIconImage.alpha = 0.0
        self.searchIconWhileEditing.alpha = 1.0
    }
}

func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
    print("\nfunc searchBarTextDidEndEditing\n")
    UIView.animate(withDuration: 0.25) {
        self.dayButton.alpha = 1.0
        self.dayButtonLabel.alpha = 1.0
        self.dayDropDownArrow.alpha = 1.0
        self.dayButton.isUserInteractionEnabled = true

        self.searchIconImage.alpha = 1.0
        self.searchIconWhileEditing.alpha = 0.0
        self.tableView.transform = CGAffineTransform(translationX: 0.0, y: 0.0)

        self.tableView.translatesAutoresizingMaskIntoConstraints = false
        self.mapButton.alpha = 1.0
        self.mapButton.isEnabled  = true

        self.dealOfTheDayLabel.alpha = 1.0
        self.exploreSpecialView.alpha = 1.0
        self.exploreSpecialsLabel.alpha = 1.0
    }
    searchActive = false
}

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
    print("\nfunc searchBarCancelButtonClicked\n")
    self.searchBar.setShowsCancelButton(false, animated: true)
    self.searchBar.text = ""
    self.searchBar.resignFirstResponder()
    searchActive = false
    self.searchIconImage.alpha = 1.0
    self.searchIconWhileEditing.alpha = 0.0
    self.dayDropDownArrow.alpha = 1.0
    self.tableView.transform = CGAffineTransform(translationX: 0.0, y: 0.0 )
    self.dealOfTheDayLabel.alpha = 1.0
    self.exploreSpecialView.alpha = 1.0
    self.exploreSpecialsLabel.alpha = 1.0
    tableView.reloadData()
}


func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    self.searchResults = self.library.filter({ (bar: BarLibrary) -> Bool in
        let tmp: String = bar.name!
        let range = tmp.localizedLowercase.range(of: searchText.localizedLowercase)
        return range != nil
    })
    if(self.searchResults.count == 0){
        searchActive = false
    } else {
        searchActive = true
    }
    self.tableView.reloadData()
}
}

下面是我的导航栏和搜索栏的界面截图:
界面捕获中的屏幕捕获图像:从前到后查看名称:UITextFieldBorderViewUISearchBarTextFieldUIViewUISearchBarUIView
注意,我不确定UISearchBar之前的UISearchBar是用来做什么的
我知道有很多问题,所以为什么其他人不能访问他们的委托方法。这些解决方案对我都不起作用。为什么到目前为止我不能访问委托方法?


最佳答案:

如果委托方法都不起作用,我猜是因为某种原因,您的searchBarDelegate正在被释放,很难说不看到整个代码,但是一定要保持对searchBarDelegate的强引用,因为委托本身是弱的,这意味着如果没有其他人保留它,它可以被释放,因此没有委托来响应调用。