UIButton fails to properly register touch in bottom region of iPhone screen

前端 未结 9 1168
南方客
南方客 2020-11-28 08:49

I have an app with many different buttons arranged in a calculator like, square / rectangular format. It is actually extremely similar to the default iOS calculator. There a

相关标签:
9条回答
  • 2020-11-28 09:05

    I was able to fix this issue by disabling delaysTouchesBegan in viewWillAppear

    self.navigationController?.interactivePopGestureRecognizer?.delaysTouchesBegan = false
    
    0 讨论(0)
  • 2020-11-28 09:07

    iOS 9.3, Xcode 7.3

    I would suggest that you should make a category to the UIButton class that implements Lukas' answer. For instructions on how to create a category see this post: How do I create a category in Xcode 6 or higher?

    Give it an appropriate name with the traditional "+" sign, i.e. if you name it "BottomOfScreen", then the resulting file name will be "UIButton+BottomOfScreen".

    If you are using objective-c, then you will get a *.h and *.m files with he new category.

    *.h

    #import <UIKit/UIKit.h>
    
    @interface UIButton (BottomOfScreen)
    
    @end
    

    *.m

    #import "UIButton+BottomOfScreen.h"
    
    @implementation UIButton (BottomOfScreen)
    
    - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
    {
        BOOL inside = [super pointInside:point withEvent:event];
    
        if (inside && !self.isHighlighted && (event.type == UIEventTypeTouches))
        {
            self.highlighted = true;
        }
        else
        {
            nil;
        }
    
        return inside;
    }
    
    @end
    
    0 讨论(0)
  • 2020-11-28 09:11

    I've run into this numerous times when working with updating older Storyboards to iOS 7+, usually when the ViewController in question has a form of a UIScrollView. Double check these 2 settings in your Storyboard on the ViewController Object (Not the view, the one with the yellow circle). When I unchecked the 'Extend Edges' under top & bottom bars, the scrollView's frame was adjusted down by 64 points (height of Nav & Status bars).

    UIButton not working on bottom of screen After setting the space between NavBar.bottom and scrollView.top back to 0, the button started working. This might be due to the fact the scrollView.frame.bottom was 64 pixels above the bottom of the window, so touches in that area were disregarded because they were technically out of the scrollView's frame but still displayed visually for some reason.

    0 讨论(0)
  • 2020-11-28 09:12

    answer of Lukas in swift and deselect highlighted

    extension UIButton {
    
      public override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {
    
        var inside = super.pointInside(point, withEvent: event)
    
        if inside != highlighted && event?.type == .Touches {
            highlighted = inside
        }
    
        return inside
    
      }
    
    }
    
    0 讨论(0)
  • 2020-11-28 09:16

    I've prepared this answer in the hopes that someone else might find it helpful.

    My problem was a little more difficult to discover the cause, but way easier to resolve. The button in question was in a custom sideBarView and XIB with four other buttons that I had programmatically initialized and loaded into the viewController. The top four worked fine. Only the bottom didn't seem to work...

    CAUSE: The CGRect defined programmatically for the custom sideBarView was actually smaller in height than the XIB needed. However, since the sideBarView wasn't clipToBounds, it showed it the lowest button, but any taps on it were registered as taps on the view below and not as taps on the lowest button.

    To discover this, I checked the 3D viewer, the order of the objects in the XIB and even took comparison snaps of each of the buttons in the simulator with Color-blended layers selected and with breakpoints on didTap... it wasn't until I shortened the spaces between the constraints between each button and discovered that nothing but only the top of the lowest button would accept the tap, which gave me the clue that it was height limitation somewhere (like the initialization code).

    0 讨论(0)
  • 2020-11-28 09:23

    Swift 3 Solution

    extension UIControl {
    
        open override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
            let inside = super.point(inside: point, with: event)
            if inside != isHighlighted && event?.type == .touches {
                isHighlighted = inside
            }
            return inside
        }
    }
    
    0 讨论(0)
提交回复
热议问题