ios - 这是通过触摸导航栏中间的按钮显示 View 并通过触摸其他任何地方将其删除的好方法吗?

标签 ios swift uinavigationcontroller uinavigationbar

我试图在导航栏的中间放置一个按钮,当我触摸它时它会显示一个列表(我在这里添加了一个纯 UIView 而不是 UITabeView 只是为了使代码更简单)。然后当我触摸其他任何地方时,附加 View 将被删除。所以我添加了一个背景 View ,其大小与屏幕相同,以响应我的触摸。虽然它仍然在导航栏后面。 这是我的问题:
这是一个好的实现吗?

class ViewController: UIViewController {
    var optionView: UIView!
    var backgroundView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let button = UIButton(type: .system)
        button.addTarget(self, action: #selector(ViewController.titleButtonTapped), for: .touchUpInside)
        button.frame = CGRect(x: 0, y: 0, width: 20, height: 30)
        button.backgroundColor = .red
        navigationItem.titleView = button
    }

    func titleButtonTapped() {
        backgroundView = UIView(frame: UIScreen.main.bounds)
        backgroundView.backgroundColor = .clear
        let gesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.handleGesture)) // add this gesture to response my touch
        backgroundView.addGestureRecognizer(gesture)
        view.addSubview(maskView)

        optionView = UIView(frame: CGRect(x: -40, y: 30, width: 100, height: 100)) // x = button.wdith / 2  - optionView.width / 2 
        optionView.backgroundColor = .red
        navigationItem.titleView?.addSubview(alertView)
    }
    func handleGesture() {
        optionView.removeFromSuperview()
        backgroundView.removeFromSuperview()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

现在看起来像下面这样。 enter image description here

编辑:

以下是我对弹出 View 的实现。

func buttonTapped() {
    let popoverViewController = UIViewController()
    popoverViewController.preferredContentSize = CGSize(width: 300, height: 300)
    popoverViewController.modalPresentationStyle = .popover

    let presentationController = popoverViewController.popoverPresentationController!
    presentationController.delegate = self
    presentationController.sourceView = view
    presentationController.sourceRect = CGRect(x: 100, y: 100 , width: 100, height: 100)
    present(popoverViewController, animated: true, completion: nil)

}
// delegate
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
    return .none
}

它与 Apple 文档略有不同。他们建议我们最好在调用 present(:animated: completion:) 方法后配置 presentation controller。但如果我在演示前不配置它,它就不起作用。也许吧,因为我设置了委托(delegate)。

Configuring the popover presentation controller after calling present(_:animated:completion:) might seem counter-intuitive but UIKit does not create a presentation controller until after you initiate a presentation. In addition, UIKit must wait until the next update cycle to display new content onscreen anyway. That delay gives you time to configure the presentation controller for your popover.

最佳答案

是否使用弹出窗口取决于此弹出 View 的用途。如果它有很多信息,最好将它分离到另一个 View Controller 并在单击按钮时对其进行 segue。这将为用户提供全屏以查看它是什么。 对我来说,在导航栏中央添加一个按钮并不常见。你必须通知我,我才能点击它。

总结:

如果你想要一个弹出 View 来告诉用户提示或向他们展示一些东西,最好使用 UIPopoverPresentationController 这样你就不需要关心样式了。

如果你想让另一个 View 显示数据、图片列表等,最好使用分段控件或另一个 View Controller

关于ios - 这是通过触摸导航栏中间的按钮显示 View 并通过触摸其他任何地方将其删除的好方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44655412/

相关文章:

ios - 单击按钮快速推送 UIViewController 两次

将多个 ViewController 推送到 NavigationController 堆栈时的 iOS 委托(delegate)问题

json - 动态生成表格 View 单元格和标题

ios - "loaded nib but the view outlet was not set"异常

ios - UINavigationController 中的常量 UIBarButtonItem

ios - UITextView setText 没有更新文本

ios - 从 UItableviewcell 重新加载 TableView 数据

ios - UINavigationController 和自动布局顶部布局指南的奇怪行为

ios - 快速表部分标题在滚动上复制

ios - 适用于 iOS 的 AWS 开发工具包 : problems with conversion from Swift 2 to Swift 3