Is it possible for a UIButton (or any other control for that matter) to receive touch events when the UIButton's frame lies outside of it's parent's frame? Cause when I try this, my UIButton doesn't seem to be able to receive any events. How do I work around this?
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- back button text does not change
相关文章
- 现在使用swift开发ios应用好还是swift?
- Could I create “Call” button in HTML 5 IPhone appl
- TCC __TCCAccessRequest_block_invoke
- xcode 4 garbage collection removed?
- Unable to process app at this time due to a genera
- How can I add media attachments to my push notific
- How do you detect key up / key down events from a
- “Storyboard.storyboard” could not be opened
Why this is happening?
This is because when your subview lies outside of your superview's bounds, touch events that actually happens on that subview will not be delivered to that subview. However, it WILL be delivered to its superview.
What you need to do?
When your superview receives the touch event mentioned above, you'll need to tell UIKit explicitly that my subview should be the one to receive this touch event.
What about the code?
In your superview, implement
func hitTest(_ point: CGPoint, with event: UIEvent?)
Yes. You can override the
hitTest:withEvent:
method to return a view for a larger set of points than that view contains. See the UIView Class Reference.Edit: Example:
Edit 2: (After clarification:) In order to ensure that the button is treated as being within the parent's bounds, you need to override
pointInside:withEvent:
in the parent to include the button's frame.Note the code just there for overriding pointInside is not quite correct. As Summon explains below, do this:
Note that you'd very likely do it with
self.oversizeButton
as an IBOutlet in this UIView subclass; then you can just drag the "oversize button" in question, to, the special view in question. (Or, if for some reason you were doing this a lot in a project, you'd have a special UIButton subclass, and you could look through your subview list for those classes.) Hope it helps.