以编程方式点击其他位置时关闭视图模态

时间:2019-01-23 20:59:37

标签: ios swift uiview uigesturerecognizer rx-swift

我正在为我的ios应用程序创建一个过滤器视图,并且不确定在其外部点击时如何隐藏弹出视图控制器。如果可能的话,我想使用RxSwift /一种反应式。

enter image description here

我对此进行了一些研究,只是对不同的解决方案感到困惑:

  • UIGestureRecognizer
  • UIPopoverController
  • 覆盖touchesBegan方法

此外,我对于应该隐藏还是显示视图控制器感到困惑。呈现/解雇有好处吗?

这是我用来创建视图并将其显示在按钮上的代码:

let filterView = UIView()

func initFilterView() {
    let filterNavBar = UINavigationBar()
    let filterNavItem = UINavigationItem()

    self.view.addSubview(self.filterView)
    self.filterView.addSubview(testButton)
    self.filterView.addSubview(filterNavBar)

    self.filterView.isHidden = true
    self.filterView.backgroundColor = .blue
    self.filterView.snp.makeConstraints { make in
        make.bottom.left.right.equalToSuperview()
        make.height.equalTo(300)
    }

    filterNavBar.snp.makeConstraints { make in
        make.height.equalTo(50)
        make.top.width.equalToSuperview()
    }

    let clearButton = UIBarButtonItem()
    let doneButton = UIBarButtonItem()

    clearButton.title = "Clear"
    doneButton.title = "Save"
    filterNavItem.leftBarButtonItem = clearButton
    filterNavItem.rightBarButtonItem = doneButton
    filterNavBar.items = [filterNavItem]
    testButton.backgroundColor = .red
    testButton.setTitle("test haha haha", for: .normal)
    testButton.snp.makeConstraints { make in
        make.centerX.centerY.equalToSuperview()
        make.height.width.equalTo(100)
    }
}
func initNavBar() {
    self.navigationItem.titleView = searchBar
    filterButton.title = "Filter"
    self.navigationItem.rightBarButtonItem = filterButton
}
filterButton.rx.tap.subscribe(onNext: { [unowned self] tap in
    self.filterView.isHidden = false
})

我希望代码可以隐藏视图,但是我不确定该怎么做。

0 个答案:

没有答案