Browse Source

Search: Add Cancel button to exit search

Carola Nitz 6 years ago
parent
commit
903419ce7c
1 changed files with 25 additions and 8 deletions
  1. 25 8
      Sources/MediaCategories/MediaCategoryViewController.swift

+ 25 - 8
Sources/MediaCategories/MediaCategoryViewController.swift

@@ -23,6 +23,7 @@ class VLCMediaCategoryViewController: UICollectionViewController, UICollectionVi
     private var services: Services
 
     var searchBar = UISearchBar(frame: .zero)
+    private var cancelSearchButton = UIButton(type: .custom)
     private var searchBarConstraint: NSLayoutConstraint?
     private let searchDataSource: LibrarySearchDataSource
     private let searchBarSize: CGFloat = 50.0
@@ -95,15 +96,20 @@ class VLCMediaCategoryViewController: UICollectionViewController, UICollectionVi
     }
 
     func setupSearchBar() {
-
-        searchBar.translatesAutoresizingMaskIntoConstraints = false
-        view.addSubview(searchBar)
-        searchBarConstraint = searchBar.topAnchor.constraint(equalTo: view.topAnchor, constant: -searchBarSize)
+        cancelSearchButton.setTitle(NSLocalizedString("BUTTON_CANCEL", comment: ""), for: .normal)
+        cancelSearchButton.addTarget(self, action: #selector(cancelSearch), for: .touchUpInside)
+        cancelSearchButton.setTitleColor(.vlcOrangeTint, for: .normal)
+        cancelSearchButton.translatesAutoresizingMaskIntoConstraints = false
+        cancelSearchButton.isHidden = true
+        let searchBarStackView = UIStackView(arrangedSubviews: [searchBar, cancelSearchButton])
+        searchBarStackView.translatesAutoresizingMaskIntoConstraints = false
+        view.addSubview(searchBarStackView)
+        searchBarConstraint = searchBarStackView.topAnchor.constraint(equalTo: view.topAnchor, constant: -searchBarSize)
         NSLayoutConstraint.activate([
             searchBarConstraint!,
-            searchBar.leadingAnchor.constraint(equalTo: view.leadingAnchor),
-            searchBar.trailingAnchor.constraint(equalTo: view.trailingAnchor),
-            searchBar.heightAnchor.constraint(equalToConstant: searchBarSize)
+            searchBarStackView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 10),
+            searchBarStackView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10),
+            searchBarStackView.heightAnchor.constraint(equalToConstant: searchBarSize)
             ])
     }
 
@@ -238,8 +244,19 @@ class VLCMediaCategoryViewController: UICollectionViewController, UICollectionVi
     // MARK: - Search
     func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
         reloadData()
+        UIView.animate(withDuration: 0.3) { [weak self] in
+            self?.cancelSearchButton.alpha = 1
+            self?.cancelSearchButton.isHidden = false
+        }
+    }
+    @objc func cancelSearch() {
+        searchBar.resignFirstResponder()
+        UIView.animate(withDuration: 0.3) { [weak self] in
+            self?.cancelSearchButton.alpha = 0
+            self?.cancelSearchButton.isHidden = true
+        }
+        reloadData()
     }
-
     func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
         searchDataSource.shouldReloadFor(searchString: searchText)
         reloadData()