What similar functionality method (viewWillAppear) exists on UIView?

前端 未结 3 652
星月不相逢
星月不相逢 2021-02-06 23:36

viewWillAppear is called on UIViewController when a view is about to be shown on screen. Is it possible to get similar callback on UIView?

3条回答
  •  广开言路
    2021-02-07 00:03

    • Note, I was originally going to post this answer here but seeing as this was asked years prior I marked that one as a duplicate and will now answer here instead.

    Macro Solution

    I've made a macro solution for this that is very elegant and easy to use.


    .m

    Add the following to your .m

    -(void)viewDidLoad {
        //normal stuff
    }
    
    __METHOD_MACRO_wasRemovedFromSuperview() {
        //Code here will run whenever this view is removed from its superview
    }
    
    __METHOD_MACRO_wasAddedAsSubview() {
        //Code here will run whenever this view is added as the subview of another view
    }
    

    Yup! It really is that easy!

    (Due to how the ifdef toggling of the macro is set-up below, you can use one or the other or both!)


    .h

    Add the following to your .h beneath @end (or, if you want to keep things clean, you can simply add to a file called macros.h and #import it)

    Note: If you add this (or import this) to multiple files that also import each other or @class each other you can end up messing up the #ifdef logic and this macro could fail, I suggest importing it from a separate .h file for each class you need it in, this macro was created more as a proof of concept than something to be used in production code

    //Logic for __METHOD_MACRO_wasAddedAsSubview() and __METHOD_MACRO_wasRemovedFromSuperview()
    
    #define startSuppressingWarnings() \
    _Pragma("clang clang diagnostic push")\
    _Pragma("clang diagnostic ignored \"-Weverything\"")
    
    #define stopSuppressingWarnings() \
    _Pragma("clang clang diagnostic pop")
    
    #define __INTERNAL_METHOD_MACRO__didChangeSuperviewsSetup() \
    -(void)didMoveToWindow {\
    [super didMoveToWindow];\
    if (self.window && self.superview) {\
    startSuppressingWarnings()/*Hide potential undeclared selector warnings*/\
    if ([self respondsToSelector:@selector(__wasAddedAsSubview)]) {\
    [self performSelector:@selector(__wasAddedAsSubview)];\
    }\
    stopSuppressingWarnings()\
    } else {\
    startSuppressingWarnings()/*Hide potential undeclared selector warnings*/\
    if ([self respondsToSelector:@selector(__wasRemovedFromSuperview)]) {\
    [self performSelector:@selector(__wasRemovedFromSuperview)];\
    }\
    stopSuppressingWarnings()\
    }\
    }
    
    #ifdef __INTERNAL_METHOD_MACRO__didChangeSuperviewsSetup_wasSetup
    ///Called when UIView was added as subview OR moved to another superview OR if another VC was presented
    #define __METHOD_MACRO_wasAddedAsSubview() \
    -(void)__wasAddedAsSubview/*{ //perform extra processes here }*/
    #else
    #define __INTERNAL_METHOD_MACRO__didChangeSuperviewsSetup_wasSetup 1
    ///Called when UIView was added as subview OR moved to another superview
    #define __METHOD_MACRO_wasAddedAsSubview() \
    __INTERNAL_METHOD_MACRO__didChangeSuperviewsSetup() \
    -(void)__wasAddedAsSubview/*{ //perform extra processes here }*/
    #endif
    
    #ifdef __INTERNAL_METHOD_MACRO__didChangeSuperviewsSetup_wasSetup
    ///Called when UIView is removed as subview from superview or when its parent window is removed
    #define __METHOD_MACRO_wasRemovedFromSuperview() \
    -(void)__wasRemovedFromSuperview/*{ //perform extra processes here }*/
    #else
    #define __INTERNAL_METHOD_MACRO__didChangeSuperviewsSetup_wasSetup 1
    ///Called when UIView is removed as subview from superview
    #define __METHOD_MACRO_wasRemovedFromSuperview() \
    __INTERNAL_METHOD_MACRO__didChangeSuperviewsSetup() \
    -(void)__wasRemovedFromSuperview/*{ //perform extra processes here }*/
    #endif
    

提交回复
热议问题