IOS UIButton outside Frame does not fire touchinside event

I have three kinds of controllers and a view.

one contains the main view, the other contains a custom navigation bar, and the third contains the view (aka a subspecies of the third controller).

when the application is downloaded, the navigation bar view will be visible along with the preview. Inside the subview, I have a button that I want to place on top of the navigation bar view. Unfortunately, I can’t do it simply using something like navigationitem.setrightbutton ... or any other similar methods (everything is encoded, there is no need).

So, what I'm doing is setting the y parameter of the y button to a negative value. This means that the button goes beyond the scope of the subset, but it achieves what I want, which places the button above the navigation bar.

Unfortunately, no click events are passed to the button, since it is outside the view frame ...

I tried overriding pointinside and hittest views, but nothing works.

I hope I made it clear. Any code that would solve this problem would be greatly appreciated.

+4
source share
1 answer

Touch events are only visible. Although it is possible to post a review outside of your parents and still see it, events will not be shared with him.

The main event handling mechanism is the following UIView method:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event 

This method is responsible for determining which gaze this touch event should go to. When you touch the screen, the root view (window) hitTest method is called, and it will call the subviews method on it, and they will call the method in their subzones, etc.

Basically, you can create a custom window class and override the hitTest method in your window to make it forward events to the button, even though it is outside its parent boundaries, but it is quite difficult to do this correctly. It will be something like this:

 - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { CGRect rect = WORK_OUT_WHERE_THE_BUTTON_RECT_IS_RELATIVE_TO_THE_WINDOW; if (CGRectContainsPoint(rect, point)) { return theButtonView; } else { return [super hitTest:point withEvent:event]; } } 

Instead, I suggest you save a lot of hassle and change the way your application works by adding your button as a subzone of the navigation bar, instead of using negative positioning to place it there.

+2
source

Source: https://habr.com/ru/post/1413641/


All Articles