     // MARK: - UITextFieldDelegate
     
     public func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
-        if let result = delegate?.searchBarShouldBeginEditing?(self) where result {
-            setCancelButtonVisibility(true)
+        if let result = delegate?.searchBarShouldBeginEditing?(self) {
+            if result {
+                setCancelButtonVisibility(true)
+            }
             return result
         }
         setCancelButtonVisibility(true)

    }
    
    public func textFieldShouldClear(_ textField: UITextField) -> Bool {
        if let result = delegate?.searchBarShouldClear?(self) where result {
            return result
        }
        return true
    }
    
    public func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        if let result = delegate?.searchBarShouldReturn?(self) where result {
            return result
        }
        return true

let kDefaultAnimationDuration: NSTimeInterval = 0.25

public class SHSearchBar: UIView, UITextFieldDelegate {
    private(set) var backgroundView: UIImageView = UIImageView()

    public var cancelButton: UIButton = UIButton(type: .Custom)
    public var textField: UITextField = UITextField()

    // Constraints for showing and hiding the cancel button
    private(set) var bgToCancelButtonConstraint: NSLayoutConstraint!
    private(set) var bgToParentConstraint: NSLayoutConstraint!
    
    public var delegate: SHSearchBarDelegate?
    
