Get the current first responder without using a private API

前端 未结 28 1803
夕颜
夕颜 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 05:55

    In one of my applications I often want the first responder to resign if the user taps on the background. For this purpose I wrote a category on UIView, which I call on the UIWindow.

    The following is based on that and should return the first responder.

    @implementation UIView (FindFirstResponder)
    - (id)findFirstResponder
    {
        if (self.isFirstResponder) {
            return self;        
        }
        for (UIView *subView in self.subviews) {
            id responder = [subView findFirstResponder];
            if (responder) return responder;
        }
        return nil;
    }
    @end
    

    iOS 7+

    - (id)findFirstResponder
    {
        if (self.isFirstResponder) {
            return self;
        }
        for (UIView *subView in self.view.subviews) {
            if ([subView isFirstResponder]) {
                return subView;
            }
        }
        return nil;
    }
    

    Swift:

    extension UIView {
        var firstResponder: UIView? {
            guard !isFirstResponder else { return self }
    
            for subview in subviews {
                if let firstResponder = subview.firstResponder {
                    return firstResponder
                }
            }
    
            return nil
        }
    }
    

    Usage example in Swift:

    if let firstResponder = view.window?.firstResponder {
        // do something with `firstResponder`
    }
    

提交回复
热议问题