Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass touches through a UIViewController

I have a view controller that I overlay on top of another view controller. I just need the top view controller so I can overlay a temporary pop up notification, and having an overlayed VC allows me to present this over a UITableViewController as you can't add subviews to tableview controllers directly.

Is it possible to interact with the bottom view controller while it has another view controller covering it. If this were a view or a window you would achieve this by setting user interaction to false or using hitTest but neither of these approaches works for a view controller.

like image 597
Mark Bridges Avatar asked Jan 07 '16 16:01

Mark Bridges


People also ask

How do I pass data between view controllers?

Photo by the author. In this article, we’re going to explore five ways to pass data between View Controllers, with Swift code snippets and examples. The five ways are: 1. Segues Segues are a storyboard mode to pass data. Imagine your app has an onboarding screen, and you want to ask the user's name to address them later on another screen:

How do I show two view controllers in one view controller?

Select the “Show” option from the “Action Segue” menu. Control + click the button and drag to the second ViewController, then select “Show.” Now, on the first ViewController class, you need to override the prepare (for segue) method: 2. Delegate Design Pattern

How to send notifications to multiple view controllers in Swift?

To handle the notification when it gets sent, you should implement a method and use it as the selector parameter for the addObserver implemented above: Finally, to send the notification from another View Controller, just use the post method with your notification name: So, these were five ways of passing data between View Controllers in Swift.

How to check if a view disappears from a viewcontroller?

Whenever a view controller is presented, it goes through the viewWillAppear method and then the viewDidAppear method is invoked. The same happens when the view disappears: First, the viewWillDisappear method is invoked and then the viewDidDisappear method is invoked. We can easily check this by modifying the code in our ViewController class.


1 Answers

Create subclass like this

class TouchDelegatingView: UIView {
    weak var touchDelegate: UIView? = nil


    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        guard let view = super.hitTest(point, with: event) else {
            return nil
        }

        guard view === self, let point = touchDelegate?.convert(point, from: self) else {
            return view
        }

        return touchDelegate?.hitTest(point, with: event)
    }
}

Then in your ViewController set view class (you can do it directly in xib, or inside loadView() method)

And add

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        if let delegatingView = view as? TouchDelegatingView {
            delegatingView.touchDelegate = presentingViewController?.view
        }
    }
}

Also you can use this mechanism to delegate touches to any other view, no matter if it

like image 169
Tiran Ut Avatar answered Sep 19 '22 13:09

Tiran Ut