Skip to content

Commit

Permalink
Fix layout issue with UINavigationController
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoNatan committed Oct 31, 2024
1 parent f500abb commit 5359222
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ inline __attribute__((always_inline)) LNPopupBarStyle _LNPopupResolveBarStyleFro

- (void)_traitCollectionForPopupBarDidChange:(LNPopupBar*)bar;
- (void)_popupBarMetricsDidChange:(LNPopupBar*)bar;
- (void)_popupBarMetricsDidChange:(LNPopupBar*)bar shouldLayout:(BOOL)layout;
- (void)_popupBarStyleDidChange:(LNPopupBar*)bar;
- (void)_popupBar:(LNPopupBar*)bar updateCustomBarController:(LNPopupCustomBarViewController*)customController cleanup:(BOOL)cleanup;
- (void)_removeInteractionGestureForPopupBar:(LNPopupBar*)bar;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1723,6 +1723,11 @@ - (void)_traitCollectionForPopupBarDidChange:(LNPopupBar*)bar
}

- (void)_popupBarMetricsDidChange:(LNPopupBar*)bar
{
[self _popupBarMetricsDidChange:bar shouldLayout:YES];
}

- (void)_popupBarMetricsDidChange:(LNPopupBar*)bar shouldLayout:(BOOL)layout
{
if(self.popupBar.acceptsSizing == NO)
{
Expand All @@ -1736,7 +1741,7 @@ - (void)_popupBarMetricsDidChange:(LNPopupBar*)bar
barFrame.origin.y -= (barFrame.size.height - currentHeight);
self.popupBar.frame = barFrame;

_LNPopupSupportSetPopupInsetsForViewController(_containerController, YES, UIEdgeInsetsMake(0, 0, self.popupBar.frame.size.height - [_containerController _ln_popupOffsetForPopupBarStyle:self.popupBar.resolvedStyle], 0));
_LNPopupSupportSetPopupInsetsForViewController(_containerController, layout, UIEdgeInsetsMake(0, 0, self.popupBar.frame.size.height - [_containerController _ln_popupOffsetForPopupBarStyle:self.popupBar.resolvedStyle], 0));
}

- (void)_popupBarStyleDidChange:(LNPopupBar*)bar
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ - (CGFloat)_ln_popupOffsetForPopupBarStyle:(LNPopupBarStyle)barStyle

- (CGRect)defaultFrameForBottomDockingView_internal
{
CGFloat safeAreaAddition = self.view.safeAreaInsets.bottom - _LNPopupSafeAreas(self).bottom;
CGFloat safeAreaAddition = self.view.safeAreaInsets.bottom - _LNPopupSafeAreaInsets(self).bottom;

if(self.presentingViewController != nil && [NSStringFromClass(self.nonMemoryLeakingPresentationController.class) containsString:@"Preview"])
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static inline __attribute__((always_inline)) UIEdgeInsets __LNEdgeInsetsSum(UIEd

extern BOOL __ln_popup_suppressViewControllerLifecycle;

UIEdgeInsets _LNPopupSafeAreas(id self);
UIEdgeInsets _LNPopupSafeAreaInsets(id self);
void _LNPopupSupportSetPopupInsetsForViewController(UIViewController* controller, BOOL layout, UIEdgeInsets popupEdgeInsets);

@interface _LNPopupBottomBarSupport : UIView @end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ static inline __attribute__((always_inline)) void _LNSetPopupSafeAreaInsets(id s
{
objc_setAssociatedObject(self, LNPopupAdditionalSafeAreaInsets, [NSValue valueWithUIEdgeInsets:additionalSafeAreaInsets], OBJC_ASSOCIATION_RETAIN_NONATOMIC);

UIEdgeInsets user = _LNUserSafeAreas(self);
UIEdgeInsets user = _LNUserSafeAreaInsets(self);

_LNUpdateUserSafeAreaInsets(self, user, additionalSafeAreaInsets);
}
Expand All @@ -345,7 +345,7 @@ - (void)_ln_setAdditionalSafeAreaInsets:(UIEdgeInsets)additionalSafeAreaInsets
{
objc_setAssociatedObject(self, LNUserAdditionalSafeAreaInsets, [NSValue valueWithUIEdgeInsets:additionalSafeAreaInsets], OBJC_ASSOCIATION_RETAIN_NONATOMIC);

UIEdgeInsets popup = _LNPopupSafeAreas(self);
UIEdgeInsets popup = _LNPopupSafeAreaInsets(self);

_LNUpdateUserSafeAreaInsets(self, additionalSafeAreaInsets, popup);
}
Expand All @@ -355,12 +355,12 @@ - (void)_ln_setChildAdditiveSafeAreaInsets:(UIEdgeInsets)childAdditiveSafeAreaIn
objc_setAssociatedObject(self, LNPopupChildAdditiveSafeAreaInsets, [NSValue valueWithUIEdgeInsets:childAdditiveSafeAreaInsets], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

UIEdgeInsets _LNPopupSafeAreas(id self)
UIEdgeInsets _LNPopupSafeAreaInsets(id self)
{
return [objc_getAssociatedObject(self, LNPopupAdditionalSafeAreaInsets) UIEdgeInsetsValue];
}

static inline __attribute__((always_inline)) UIEdgeInsets _LNUserSafeAreas(id self)
static inline __attribute__((always_inline)) UIEdgeInsets _LNUserSafeAreaInsets(id self)
{
return [objc_getAssociatedObject(self, LNUserAdditionalSafeAreaInsets) UIEdgeInsetsValue];
}
Expand All @@ -372,8 +372,8 @@ UIEdgeInsets _LNPopupChildAdditiveSafeAreas(id self)

- (UIEdgeInsets)_ln_additionalSafeAreaInsets
{
UIEdgeInsets user = _LNPopupSafeAreas(self);
UIEdgeInsets popup = _LNUserSafeAreas(self);
UIEdgeInsets user = _LNPopupSafeAreaInsets(self);
UIEdgeInsets popup = _LNUserSafeAreaInsets(self);

return __LNEdgeInsetsSum(user, popup);
}
Expand Down Expand Up @@ -637,7 +637,7 @@ - (void)_uCOIFPIN
{
CGFloat contentMargin = contentMarginFunc(self.popupPresentationContainerViewController, contentMarginSEL);

UIEdgeInsets insets = __LNEdgeInsetsSum(self.popupPresentationContainerViewController.view.safeAreaInsets, UIEdgeInsetsMake(0, 0, - _LNPopupSafeAreas(self.popupPresentationContainerViewController).bottom, 0));
UIEdgeInsets insets = __LNEdgeInsetsSum(self.popupPresentationContainerViewController.view.safeAreaInsets, UIEdgeInsetsMake(0, 0, - _LNPopupSafeAreaInsets(self.popupPresentationContainerViewController).bottom, 0));

_setContentOverlayInsets_andLeftMargin_rightMarginFunc(self, _setContentOverlayInsets_andLeftMargin_rightMarginSEL, insets, contentMargin, contentMargin);
setContentMarginFunc(self, setContentMarginSEL, contentMargin);
Expand Down Expand Up @@ -673,7 +673,7 @@ - (UIEdgeInsets)_vSAIFS
{
if([self _isContainedInPopupController])
{
return __LNEdgeInsetsSum(self.popupPresentationContainerViewController.view.safeAreaInsets, UIEdgeInsetsMake(0, 0, - _LNPopupSafeAreas(self.popupPresentationContainerViewController).bottom, 0));
return __LNEdgeInsetsSum(self.popupPresentationContainerViewController.view.safeAreaInsets, UIEdgeInsetsMake(0, 0, - _LNPopupSafeAreaInsets(self.popupPresentationContainerViewController).bottom, 0));
}

UIEdgeInsets insets = [self _vSAIFS];
Expand Down Expand Up @@ -825,7 +825,7 @@ - (void)_ln_popup_viewDidLayoutSubviews
{
UIEdgeInsets neededInsets = UIEdgeInsetsMake(0, 0, MAX(0, self.popupBar.frame.size.height - [self _ln_popupOffsetForPopupBarStyle:self.popupBar.resolvedStyle]), 0);

UIEdgeInsets safe = _LNPopupSafeAreas(self);
UIEdgeInsets safe = _LNPopupSafeAreaInsets(self);
UIEdgeInsets childAdditive = _LNPopupChildAdditiveSafeAreas(self);

if(neededInsets.bottom != MAX(safe.bottom, childAdditive.bottom))
Expand Down Expand Up @@ -964,21 +964,33 @@ static void __LNPopupUpdateChildInsets(UIViewController* controller)
_LNSetPopupSafeAreaInsets(controller, popupSafeAreaInsets);
}

void _LNPopupSupportSetPopupInsetsForViewController(UIViewController* controller, BOOL layout, UIEdgeInsets popupEdgeInsets)
void _LNPopupSupportSetPopupInsetsForViewController(UIViewController* controller, BOOL wantsLayout, UIEdgeInsets popupEdgeInsets)
{
BOOL shouldLayout = NO;

//Container classes with bottom bars have bugs if additional safe areas are applied directly to them.
//Instead, set a custom property and update their children recursively to take care of the additional safe area.
if(__LNPopupIsClassBuggyForAdditionalSafeArea(controller) == YES)
{
[controller _ln_setChildAdditiveSafeAreaInsets:popupEdgeInsets];
__LNPopupUpdateChildInsets(controller);
UIEdgeInsets current = _LNPopupChildAdditiveSafeAreas(controller);
if(UIEdgeInsetsEqualToEdgeInsets(current, popupEdgeInsets) == NO)
{
shouldLayout = YES;
[controller _ln_setChildAdditiveSafeAreaInsets:popupEdgeInsets];
__LNPopupUpdateChildInsets(controller);
}
}
else
{
_LNSetPopupSafeAreaInsets(controller, popupEdgeInsets);
UIEdgeInsets current = _LNPopupSafeAreaInsets(controller);
if(UIEdgeInsetsEqualToEdgeInsets(current, popupEdgeInsets) == NO)
{
shouldLayout = YES;
_LNSetPopupSafeAreaInsets(controller, popupEdgeInsets);
}
}

if(layout)
if(wantsLayout && shouldLayout)
{
[controller.view setNeedsUpdateConstraints];
[controller.view setNeedsLayout];
Expand Down Expand Up @@ -1987,7 +1999,7 @@ - (void)_sTH:(BOOL)hidden e:(UIRectEdge)edge d:(NSTimeInterval)duration;
[self _layoutPopupBarOrderForTransition];

void (^animations)(void) = ^ {
[self._ln_popupController_nocreate _popupBarMetricsDidChange:self._ln_popupController_nocreate.popupBar];
[self._ln_popupController_nocreate _popupBarMetricsDidChange:self._ln_popupController_nocreate.popupBar shouldLayout:NO];
//During the transition, animate the popup bar and content together with the toolbar transition.
[self._ln_popupController_nocreate _setContentToState:self._ln_popupController_nocreate.popupControllerInternalState];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,22 @@
leftButton.sizeToFit()
view.addSubview(leftButton)

preferredContentSize = CGSize(width: 0, height: 50)
self.preferredContentSize = CGSize(width: 0, height: 50)
animateSize()
}

var idx = 0
func animateSize() {
idx = 1 - idx;
UIView.animate(withDuration: 1.0, delay: 0.0, options: [.curveEaseInOut, .allowUserInteraction]) {
self.preferredContentSize = CGSize(width: 0, height: 50 + self.idx * 50)
} completion: { [weak self] _ in
guard let self else {
return
}

self.animateSize()
}
}

public override func viewDidLayoutSubviews() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,11 @@ - (void)viewDidDisappear:(BOOL)animated

- (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
[self updateNavigationBarTitlePositionForTraitCollection:newCollection];
[super willTransitionToTraitCollection:newCollection withTransitionCoordinator:coordinator];

[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
[self updateNavigationBarTitlePositionForTraitCollection:newCollection];
} completion:nil];
}

- (void)dealloc
Expand Down

0 comments on commit 5359222

Please sign in to comment.