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

前端 未结 9 1180
南方客
南方客 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:30

    I've written a full solution in swift based on Luka's answer. Just make any conflicting buttons conform to this class and the problem will be gone:

    class BorderBugFixButton : UIButton {
    
        override func awakeFromNib() {
            super.awakeFromNib()
            NSNotificationCenter.defaultCenter().addObserver(self, selector: "unHighlight", name: UIApplicationWillResignActiveNotification, object: nil)
        }
    
        deinit {
            NSNotificationCenter.defaultCenter().removeObserver(self)
        }
    
        override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {
            let inside = super.pointInside(point, withEvent: event)
            if inside != highlighted && event?.type == .Touches {
                highlighted = inside
            }
            return inside
        }
    
        internal func unHighlight() {
            highlighted = false
        }
    
    }
    

    P.S.: For those of you that don't like Storyboards/Xib's, just migrate the implementation of awakeFromNib() to init()

提交回复
热议问题