Как отключить просмотр таблицы, не закрывая весь экран, как в Whatsapp? - PullRequest
0 голосов
/ 03 мая 2018

Как отключить просмотр таблицы, не закрывая весь экран, как в Whatsapp? Идея в том, что когда SearchBar в SearchController еще пуст, tableview темнеет. до SearchController, по умолчанию скрывать весь экран. Используя obscuresBackgroundDuringPresentation, также скрыть весь экран.

Я использую Xcode 9.3 - Swift 4.


example of when I click on whatsapp searchbar

1 Ответ

0 голосов
/ 03 мая 2018

Попробуйте это решение

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) Удалить использование 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()
        })

   }
...