English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Preface
In daily development, we often deal with buttons, but sometimes we encounter difficult-to-handle problems, such as buttons not responding to click events. At this time, we need to investigate from the following aspects
Reasons why the button does not respond
1The button is added to a parent View that does not enable user interaction, such as UIImageView. In this case, enable the interaction of the parent view view.userInteractionEnabled = YES
Setting it to YES can solve the problem
2The button itself is obscured, and when clicked, it is not clicked on the button but on the upper layer View, which naturally will not respond
There is a method to view the layer, as shown in the figure below, clicking the button in the red box can see the UI elements of the current running interface, and you can see if any view is blocking the button
3The frame of the button exceeds the frame of the parent view, which is the most likely to occur. The button's frame must be clicked within the frame of the parent view to be effective. As shown in the figure below, the button click area in the red box does not respond.
What should you do if you encounter this problem? Please continue to read below
Solve the problem of clicking outside the click area
This situation is very likely to occur, let me give you an example: the height of the chat area is less than the height of the keyboard, and the input box is a child View of the chat area. After the keyboard pops up, the input box moves up and exceeds the frame of the parent view. At this time, clicking the button in the red box to switch to the expression keyboard action does not respond.
Solution
There are two methods here, and these two methods will be called every time there is a touch action
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event; - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event;
Override the method - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
This is a method in View, and the processing flow is as follows
Therefore, we can handle it when returning nil, because at this time the button is outside the parent View
//Returns a view to respond to the event - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{ UIView *view = [super hitTest:point withEvent:event]; if (view == nil){ //Coordinate Transformation CGPoint tempPoint = [self.testBtn convertPoint:point fromView:self]; //Determine whether the click point is within the button area if (CGRectContainsPoint(self.testBtn.bounds, tempPoint)){ //Return Button return _testBtn; } } return view; }
At this time, clicking outside the area of the button also has an effect
Summary
The above-mentioned solutions to the problem of iOS UIButton not responding to clicks are introduced by the editor for everyone. I hope it will be helpful to everyone. If you have any questions, please leave a message, and the editor will reply to everyone in time. I would also like to express my sincere gratitude to everyone for their support of the Yell Tutorial!
Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)