Skip to content
This repository was archived by the owner on Jan 17, 2019. It is now read-only.

Commit fb558d1

Browse files
authored
Merge pull request #243 from krris/customizable-center-point-of-overlay-component
Customizable center point of overlay component
2 parents 4617f06 + dec2978 commit fb558d1

File tree

5 files changed

+56
-2
lines changed

5 files changed

+56
-2
lines changed

demo/sources/NavigationController.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,8 @@ class NavigationController: UINavigationController, HUBViewControllerDelegate {
6666
func viewControllerShouldAutomaticallyManageTopContentInset(_ viewController: HUBViewController) -> Bool {
6767
return true
6868
}
69+
70+
func centerPointForOverlayComponent(in viewController: HUBViewController, proposedCenter proposedCenterPoint: CGPoint) -> CGPoint {
71+
return proposedCenterPoint
72+
}
6973
}

include/HubFramework/HUBViewController.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,18 @@ NS_ASSUME_NONNULL_BEGIN
147147
*/
148148
- (BOOL)viewControllerShouldAutomaticallyManageTopContentInset:(HUBViewController *)viewController;
149149

150+
/**
151+
* Return the center point of overlay coponents used in a view controller.
152+
*
153+
* @param viewController The view controller in question
154+
* @param proposedCenterPoint The center point that the Hub Framework is proposing
155+
*
156+
* The Hub Framework will call this method every time a view controller is being laid out, which is usually in
157+
* response to that its view model has been changed. The returned value will be set as a center point of the overlay.
158+
*/
159+
- (CGPoint)centerPointForOverlayComponentInViewController:(HUBViewController *)viewController
160+
proposedCenterPoint:(CGPoint)proposedCenterPoint;
161+
150162
@end
151163

152164
/**

sources/HUBViewController.m

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1054,7 +1054,16 @@ - (CGPoint)overlayComponentCenterPoint
10541054
CGRect frame = self.view.bounds;
10551055
frame.origin.y = self.collectionView.contentInset.top;
10561056
frame.size.height -= self.visibleKeyboardHeight + CGRectGetMinY(frame);
1057-
return CGPointMake(CGRectGetMidX(frame), CGRectGetMidY(frame));
1057+
1058+
CGPoint proposedCenterPoint = CGPointMake(CGRectGetMidX(frame), CGRectGetMidY(frame));
1059+
1060+
id<HUBViewControllerDelegate> delegate = self.delegate;
1061+
if (delegate == nil) {
1062+
return proposedCenterPoint;
1063+
}
1064+
1065+
return [delegate centerPointForOverlayComponentInViewController:self
1066+
proposedCenterPoint:proposedCenterPoint];
10581067
}
10591068

10601069
- (void)updateOverlayComponentCenterPointsWithKeyboardNotification:(NSNotification *)notification

sources/HUBViewControllerDefaultScrollHandler.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ - (UIEdgeInsets)contentInsetsForViewController:(HUBViewController *)viewControll
5252
return proposedContentInsets;
5353
}
5454

55+
- (CGPoint)centerPointForOverlayComponentInViewController:(HUBViewController *)viewController
56+
proposedCenterPoint:(CGPoint)proposedCenterPoint
57+
{
58+
return proposedCenterPoint;
59+
}
60+
5561
- (void)scrollingWillStartInViewController:(HUBViewController *)viewController
5662
currentContentRect:(CGRect)currentContentRect
5763
{

tests/HUBViewControllerTests.m

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ @interface HUBViewControllerTests : XCTestCase <HUBViewControllerDelegate>
9797
@property (nonatomic, copy) void (^viewControllerDidFinishRenderingBlock)(void);
9898
@property (nonatomic, copy) BOOL (^viewControllerShouldStartScrollingBlock)(void);
9999
@property (nonatomic, copy) BOOL (^viewControllerShouldAutomaticallyManageTopContentInset)(void);
100+
@property (nonatomic, strong, nullable) NSValue *centerPointForOverlayComponents;
100101

101102
@end
102103

@@ -2818,7 +2819,7 @@ - (void)testAdaptingOverlayComponentCenterPointToKeyboard
28182819
NSNotification * const keyboardNotification = [NSNotification notificationWithName:UIKeyboardWillShowNotification
28192820
object:nil
28202821
userInfo:notificationUserInfo];
2821-
2822+
28222823
// Show keyboard, which should push the overlay component
28232824
NSNotificationCenter * const notificationCenter = [NSNotificationCenter defaultCenter];
28242825
[notificationCenter postNotification:keyboardNotification];
@@ -2831,6 +2832,17 @@ - (void)testAdaptingOverlayComponentCenterPointToKeyboard
28312832

28322833
HUBAssertEqualFloatValues(self.component.view.center.x, 160);
28332834
HUBAssertEqualFloatValues(self.component.view.center.y, 200);
2835+
2836+
self.centerPointForOverlayComponents = [NSValue valueWithCGPoint:CGPointMake(80, 100)];
2837+
[notificationCenter postNotification:keyboardNotification];
2838+
HUBAssertEqualFloatValues(self.component.view.center.x, 80);
2839+
HUBAssertEqualFloatValues(self.component.view.center.y, 100);
2840+
2841+
self.viewController.delegate = nil;
2842+
self.centerPointForOverlayComponents = nil;
2843+
[notificationCenter postNotificationName:UIKeyboardWillHideNotification object:nil];
2844+
HUBAssertEqualFloatValues(self.component.view.center.x, 160);
2845+
HUBAssertEqualFloatValues(self.component.view.center.y, 200);
28342846
}
28352847

28362848
- (void)testScrollingToComponentAfterViewModelFinishesRendering
@@ -3173,6 +3185,17 @@ - (BOOL)viewControllerShouldAutomaticallyManageTopContentInset:(HUBViewControlle
31733185
return NO;
31743186
}
31753187

3188+
- (CGPoint)centerPointForOverlayComponentInViewController:(HUBViewController *)viewController
3189+
proposedCenterPoint:(CGPoint)proposedCenterPoint
3190+
{
3191+
XCTAssertEqual(viewController, self.viewController);
3192+
if (self.centerPointForOverlayComponents == nil) {
3193+
return proposedCenterPoint;
3194+
} else {
3195+
return [self.centerPointForOverlayComponents CGPointValue];
3196+
}
3197+
}
3198+
31763199
#pragma mark - Utilities
31773200

31783201
- (void)simulateViewControllerLayoutCycle

0 commit comments

Comments
 (0)