Skip to content

Commit

Permalink
fix(ios): update view controller’s frame according to safe area
Browse files Browse the repository at this point in the history
  • Loading branch information
vijaysingh-axway committed Apr 15, 2020
1 parent 5ecb996 commit aaa681e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
1 change: 1 addition & 0 deletions iphone/Classes/TiUIiPadPopoverProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
NSCondition *closingCondition;
TiDimension poWidth;
TiDimension poHeight;
BOOL deviceRotated;
}

- (void)updatePopover:(NSNotification *)notification;
Expand Down
48 changes: 29 additions & 19 deletions iphone/Classes/TiUIiPadPopoverProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ - (id)init
poWidth = TiDimensionUndefined;
poHeight = TiDimensionUndefined;
}
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(deviceRotated:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
return self;
}

Expand All @@ -52,6 +56,7 @@ - (void)dealloc
//This shouldn't happen because we clear it on hide.
currentPopover = nil;
}
[[NSNotificationCenter defaultCenter] removeObserver:self];
[viewController.view removeObserver:self forKeyPath:@"safeAreaInsets"];
RELEASE_TO_NIL(viewController);
RELEASE_TO_NIL(popoverView);
Expand Down Expand Up @@ -260,6 +265,7 @@ - (void)cleanup

- (void)initAndShowPopOver
{
deviceRotated = NO;
currentPopover = self;
[contentViewProxy setProxyObserver:self];
if ([contentViewProxy isKindOfClass:[TiWindowProxy class]]) {
Expand Down Expand Up @@ -366,28 +372,20 @@ - (UIViewController *)viewController
[viewController.view addObserver:self forKeyPath:@"safeAreaInsets" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
}
}
viewController.view.clipsToBounds = YES;
return viewController;
}

- (void)updateContentViewWithSafeAreaInsets:(UIEdgeInsets)edgeInsets
- (void)updateContentViewWithSafeAreaInsets:(NSValue *)insetsValue
{
CGFloat oldTop = [[contentViewProxy valueForKey:@"top"] floatValue];
CGFloat oldLeft = [[contentViewProxy valueForKey:@"left"] floatValue];
CGFloat oldRight = [[contentViewProxy valueForKey:@"right"] floatValue];
CGFloat oldBottom = [[contentViewProxy valueForKey:@"bottom"] floatValue];

if (oldTop != edgeInsets.top) {
[contentViewProxy setTop:NUMFLOAT(edgeInsets.top)];
}
if (oldBottom != edgeInsets.bottom) {
[contentViewProxy setBottom:NUMFLOAT(edgeInsets.bottom)];
}
if (oldLeft != edgeInsets.left) {
[contentViewProxy setLeft:NUMFLOAT(edgeInsets.left)];
}
if (oldRight != edgeInsets.right) {
[contentViewProxy setRight:NUMFLOAT(edgeInsets.right)];
}
TiThreadPerformOnMainThread(
^{
UIViewController *viewController = [self viewController];
contentViewProxy.view.frame = viewController.view.frame;
UIEdgeInsets edgeInsets = [insetsValue UIEdgeInsetsValue];
viewController.view.frame = CGRectMake(viewController.view.frame.origin.x + edgeInsets.left, viewController.view.frame.origin.y + edgeInsets.top, viewController.view.frame.size.width - edgeInsets.left - edgeInsets.right, viewController.view.frame.size.height - edgeInsets.top - edgeInsets.bottom);
},
NO);
}

#pragma mark Delegate methods
Expand Down Expand Up @@ -463,12 +461,24 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
if ([TiUtils isIOSVersionOrGreater:@"13.0"] && object == viewController.view && [keyPath isEqualToString:@"safeAreaInsets"]) {
UIEdgeInsets newInsets = [[change objectForKey:@"new"] UIEdgeInsetsValue];
UIEdgeInsets oldInsets = [[change objectForKey:@"old"] UIEdgeInsetsValue];
NSValue *insetsValue = [NSValue valueWithUIEdgeInsets:newInsets];

if (!UIEdgeInsetsEqualToEdgeInsets(oldInsets, newInsets)) {
[self updateContentViewWithSafeAreaInsets:newInsets];
deviceRotated = NO;
[self updateContentViewWithSafeAreaInsets:insetsValue];
} else if (deviceRotated) {
// [self viewController] need a bit of time to set its frame while rotating
deviceRotated = NO;
[self performSelector:@selector(updateContentViewWithSafeAreaInsets:) withObject:insetsValue afterDelay:.05];
}
}
}

- (void)deviceRotated:(NSNotification *)sender
{
deviceRotated = YES;
}

@end

#endif

0 comments on commit aaa681e

Please sign in to comment.