@@ -673,6 +673,7 @@ - (JHLayoutBottomOfView)centerYOffsetBottomOfView{
673673@interface JHFrameLayoutView ()
674674@property (nonatomic , assign ) BOOL initFlag;
675675@property (nonatomic , assign ) BOOL superviewObserveFlag;
676+ @property (nonatomic , unsafe_unretained) UIView *superView;
676677@end
677678
678679@implementation JHFrameLayoutView
@@ -689,9 +690,9 @@ - (instancetype)initWithFrame:(CGRect)frame
689690- (void )dealloc {
690691 [[NSNotificationCenter defaultCenter ] removeObserver: self ];
691692 if (_superviewObserveFlag) {
692- [self .superview removeObserver: self forKeyPath: @" frame" ];
693- [self .superview removeObserver: self forKeyPath: @" bounds" ];
694- [self .superview removeObserver: self forKeyPath: @" center" ];
693+ [_superView removeObserver: self forKeyPath: @" frame" ];
694+ [_superView removeObserver: self forKeyPath: @" bounds" ];
695+ [_superView removeObserver: self forKeyPath: @" center" ];
695696 }
696697}
697698
@@ -709,20 +710,30 @@ - (void)jh_initOnce
709710{
710711 _initFlag = YES ;
711712
712- // JHFrameLayoutView is not UIViewController's root view.
713- if (![self valueForKey: @" viewDelegate" ]) {
714-
715- // Screen Rotate Notification
716- [[NSNotificationCenter defaultCenter ] addObserver: self selector: @selector (jh_delayLayoutSubviews ) name: UIApplicationDidChangeStatusBarOrientationNotification object: nil ];
717-
718- // Observe superview's frame
719- if (self.superview ) {
720- _superviewObserveFlag = YES ;
721- [self .superview addObserver: self forKeyPath: @" frame" options: NSKeyValueObservingOptionNew context: nil ];
722- [self .superview addObserver: self forKeyPath: @" bounds" options: NSKeyValueObservingOptionNew context: nil ];
723- [self .superview addObserver: self forKeyPath: @" center" options: NSKeyValueObservingOptionNew context: nil ];
713+ id vc;
714+ UIView *view = self;
715+ while (view != nil ) {
716+ vc = [view valueForKey: @" viewDelegate" ];
717+ if (vc != nil ) {
718+
719+ // JHFrameLayoutView is not UIViewController's root view.
720+ if (![[vc view ] isKindOfClass: [JHFrameLayoutView class ]]){
721+
722+ // Screen Rotate Notification
723+ [[NSNotificationCenter defaultCenter ] addObserver: self selector: @selector (jh_delayLayoutSubviews ) name: UIApplicationDidChangeStatusBarOrientationNotification object: nil ];
724+
725+ // Observe superview's frame
726+ if (self.superview ) {
727+ _superviewObserveFlag = YES ;
728+ _superView = self.superview ;
729+ [self .superview addObserver: self forKeyPath: @" frame" options: NSKeyValueObservingOptionNew context: nil ];
730+ [self .superview addObserver: self forKeyPath: @" bounds" options: NSKeyValueObservingOptionNew context: nil ];
731+ [self .superview addObserver: self forKeyPath: @" center" options: NSKeyValueObservingOptionNew context: nil ];
732+ }
733+ }
734+ break ;
724735 }
725-
736+ view = view. superview ;
726737 }
727738}
728739
0 commit comments