     open var searchButton: UIButton!
     open var navigationItem: UINavigationItem!
     
+    open var isActive = false
+    
     open func setupWithDelegate(_ delegate: CNLSearchControllerDelegate, navigationItem: UINavigationItem, buttonImage: UIImage?, searchQueryText: String? = nil) {
         self.delegate = delegate
         searchButton = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))

    }
    
    open func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
        hideSearchBar(true)
    }
    
}

        searchBar.text = searchQueryText ?? ""
        searchBar.delegate = self
        searchBar.setShowsCancelButton(true, animated: false)
        updateState()
    }
    
    open func updateState() {
        if isActive {
            // remove the search button
            navigationItem.rightBarButtonItem = nil
            // add the search bar
            navigationItem.titleView = searchBar
            searchBar.becomeFirstResponder()
        } else {
            searchBar.resignFirstResponder()
            navigationItem.rightBarButtonItem = searchBarButtonItem
            navigationItem.titleView = nil
        }
    }
    
    open func activate(animated: Bool) {
        
        isActive = true
        updateState()
        
        if animated {
            searchBar.alpha = 0.0
            UIView.animate(
                withDuration: 0.5,
                animations: {
                    self.searchBar.alpha = 1.0
                },
                completion: { completed in
                    self.searchBar.becomeFirstResponder()
                }
            )
        } else {
            searchBar.alpha = 1.0
            searchBar.becomeFirstResponder()
        }
    }
    
    open func deactivate(animated: Bool) {
        searchBar.resignFirstResponder()
        hideSearchBar(animated)
        isActive = false
    }
    
    open func hideSearchBar(_ animated: Bool) {
        searchBar.resignFirstResponder()
        //searchBar.setShowsCancelButton(false, animated: true)
        delegate?.searchControllerCancelled(self)
