Get the current first responder without using a private API

前端 未结 28 2303
夕颜
夕颜 2020-11-22 05:03

I submitted my app a little over a week ago and got the dreaded rejection email today. It tells me that my app cannot be accepted because I\'m using a non-public API; specif

28条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-22 06:04

    Just it case here is Swift version of awesome Jakob Egger's approach:

    import UIKit
    
    private weak var currentFirstResponder: UIResponder?
    
    extension UIResponder {
    
        static func firstResponder() -> UIResponder? {
            currentFirstResponder = nil
            UIApplication.sharedApplication().sendAction(#selector(self.findFirstResponder(_:)), to: nil, from: nil, forEvent: nil)
            return currentFirstResponder
        }
    
        func findFirstResponder(sender: AnyObject) {
            currentFirstResponder = self
        }
    
    }
    

提交回复
热议问题