itenyh itenyh - 9 months ago 42
iOS Question

How to click a button lying below UITableView

say, I have a button lying under

, how can I click the button through the
but do not trigger the cell click event:

enter image description here

The reason I put the button behind the tableview is that I want to see and click the button under the cell whose color set to be clear, and when I scroll the table, the button can be covered by cell which is not with clear color


You can write your custom view to touch button or special view behind the topview

class MyView: UIView {

    override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
        for subview in self.subviews {
            if subview is UIButton {
                let subviewPoint = self.convertPoint(point, toView: subview)
                if subview.hitTest(subviewPoint, withEvent: event) != nil { // if touch inside button view, return button to handle event
                    return subview
        // if not inside button return nomal action
        return super.hitTest(point, withEvent: event)

Then set your controller view to custom MyView class

enter image description here