Swift 如何禁用tableview而不像Whatsapp中那样遮挡整个屏幕?

Swift 如何禁用tableview而不像Whatsapp中那样遮挡整个屏幕?,swift,uitableview,whatsapp,uisearchcontroller,obscured-view,Swift,Uitableview,Whatsapp,Uisearchcontroller,Obscured View,如何禁用tableview而不像Whatsapp中那样遮挡整个屏幕?这个想法是当SearchController中的SearchBar仍然为空时,tableview变暗。默认情况下,要搜索控制器,请隐藏整个屏幕。在演示过程中使用遮挡背景,也会遮挡整个屏幕 我使用的是Xcode 9.3-Swift 4 试试这个解决方案 1) 声明视图 let keyboardView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.

如何禁用tableview而不像Whatsapp中那样遮挡整个屏幕?这个想法是当
SearchController
中的
SearchBar
仍然为空时,
tableview
变暗。默认情况下,要搜索控制器,请隐藏整个屏幕。在演示过程中使用
遮挡背景
,也会遮挡整个屏幕

我使用的是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) 在
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) 删除通知观察员使用

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

   }

谢谢当我水平放置时,黑暗视图不会填满屏幕的其余部分。如果发生这种情况,我可以做些什么改变?@OséiasRibeiro请给我添加屏幕快照抱歉没有看到细节。我想保留WhatsApp中的取消按钮。我做了更改。它仍然有同样的问题。状态栏在与视图相关的动画中也有延迟。当我打开iphone时,我看到了这一点。