未调用UISearchBarDelegate方法

时间:2017-04-12 01:46:20

标签: ios swift search uisearchbar 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
}

我继续在searchBar中自定义textField:

    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()
}
}

以下是我的导航栏界面捕获图像和搜索栏:

Screen Capture Image

界面捕获中显示的屏幕捕获图像:从前到后查看名称:UITextFieldBorderViewUISearchBarTextFieldUIViewUISearchBar

注意我不确定UIView之前的UISearchBar是什么

我理解有很多关于为什么其他人无法访问他们的UISearchBar委托方法的问题。这些解决方案都没有对我有用。到目前为止,为什么我无法访问我的委托方法?

1 个答案:

答案 0 :(得分:1)

如果没有任何委托方法正常工作,我的猜测是你的searchBarDelegate由于某种原因被解除分配,很难说没有看到整个代码,但一定要强烈引用你的searchBarDelegate,因为代表本身是弱的意思,如果没有其他人保留它,它可以被解除分配,因此没有代表回应这些电话。