0

How do I disable a tableview without obscuring the entire screen like in Whatsapp? The idea is when the SearchBar in the SearchController is still empty, the tableview gets dark. to SearchController, by default obscure the entire screen. Using obscuresBackgroundDuringPresentation, also obscure the entire screen.

I'm using Xcode 9.3 - Swift 4.


example of when I click on whatsapp searchbar

a.masri
  • 2,439
  • 1
  • 14
  • 32

1 Answers1

1

Try this solution

1) Declare view

let keyboardView  = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width  , height: UIScreen.main.bounds.height))

2) Add Notification Observer and view color alpha in viewDidLoad

        keyboardView.backgroundColor = UIColor.black.withAlphaComponent(0.5)
        NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)

2) Remove Notification Observer use

deinit {
    NotificationCenter.default.removeObserver(self)
}

3) add Constraints to view

func addConstraints() {
        view.addConstraint(NSLayoutConstraint(item: keyboardView, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1, constant: 0))            
        view.addConstraint(NSLayoutConstraint(item: keyboardView, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1, constant: 0))

        view.addConstraint(NSLayoutConstraint(item: keyboardView, attribute: .top, relatedBy: .equal, toItem: self.view.safeAreaLayoutGuide, attribute: .bottom, multiplier: 1, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: keyboardView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute,multiplier: 1, constant: UIScreen.main.bounds.height))

    }

4) Add keyboard show and hide methods

@objc func keyboardWillShow(notification: NSNotification) {
        UIView.animate(withDuration: 0.1, animations: { () -> Void in

            self.storeCollectionView.addSubview(self.keyboardView)
             self.addConstraints()
        })
   }

  @objc func keyboardWillHide(notification: NSNotification) {

        UIView.animate(withDuration: 0.1, animations: { () -> Void in

            self.keyboardView.removeFromSuperview()
        })

   }
a.masri
  • 2,439
  • 1
  • 14
  • 32