如何在不像 Whatsapp 那样遮挡整个屏幕的情况下禁用 tableview?

How do I disable a tableview without obscuring the entire screen like in Whatsapp?

如何禁用表格视图而不像在 Whatsapp 中那样遮挡整个屏幕?这个想法是当 SearchController 中的 SearchBar 仍然是空的时,tableview 变暗。到 SearchController,默认遮挡整个屏幕。使用obscuresBackgroundDuringPresentation,也遮挡了整个屏幕。

我正在使用 Xcode 9.3 - Swift 4.


试试这个解决方案

1) 声明视图

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

2) 添加Notification Observer并在viewDidLoad

中查看颜色alpha
        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) 移除 Notification Observer 使用

deinit {
    NotificationCenter.default.removeObserver(self)
}

3) 添加约束以查看

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) 添加键盘显示和隐藏方法

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

   }