0

如何在不像 Whatsapp 那样遮挡整个屏幕的情况下禁用表格视图?这个想法是当里面还是空的时候,SearchBar里面就变黑了。to ,默认情况下会遮盖整个屏幕。使用,也会遮盖整个屏幕。SearchControllertableviewSearchControllerobscuresBackgroundDuringPresentation

我正在使用 Xcode 9.3 - Swift 4。


当我点击whatsapp搜索栏的例子

4

1 回答 1

1

试试这个解决方案

1) 声明视图

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

2)添加Notification Observer并查看颜色alphaviewDidLoad

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

   }
于 2018-05-03T14:47:47.163 回答