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
In my case, I had a
UICollectionViewCell
subclass that contained aUIButton
. I disabledclipsToBounds
on the cell and the button was visible outside of the cell's bounds. However, the button was not receiving touch events. I was able to detect the touch events on the button by using Jack's answer: https://stackoverflow.com/a/30431157/3344977Here's a Swift version:
swift 4.1 updated
To get touch events in same UIView you just need to add following override method, it will identify specific view tapped in UIView which is placed out of bound
Swift:
Where targetView is the view you wish to receive the event (which may be entirely or partially located outside of the frame of its parent view).
For me (as for others), the answer was not to override the
hitTest
method, but rather thepointInside
method. I had to do this in only two places.The Superview This is the view that if it had
clipsToBounds
set to true, it would make this whole problem disappear. So here's my simpleUIView
in Swift 3:The Subview Your mileage may vary, but in my case I also had to overwrite the
pointInside
method for the subview that was had decided the touch was out of bounds. Mine is in Objective-C, so:This answer (and a few others on the same question) can help clear up your understanding.
@jnic, I am working on iOS SDK 5.0 and in order to get your code working right I had to do this:
The container view in my case is a UIButton and all the child elements are also UIButtons that can move outside the bounds of the parent UIButton.
Best
In the parent view you can override the hit test method:
In this case, if the point falls within the bounds of your button, you forward the call there; if not, revert to the original implementation.