-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIViewController+ScrollViewInsets.m
87 lines (61 loc) · 3.11 KB
/
UIViewController+ScrollViewInsets.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#import "UIViewController+ScrollViewInsets.h"
#import <objc/runtime.h>
#pragma mark Constants
static void * const _ScrollingTabBarControllerContentInsetAdjustmentKey = (void *)&_ScrollingTabBarControllerContentInsetAdjustmentKey;
#pragma mark - Class Definition
@implementation UIViewController (ScrollViewInsets)
#pragma mark - Public Methods
- (UIScrollView *)adjustableScrollView
{
UIScrollView *adjustableScrollView = nil;
// First check if the controller's view is a scroll view.
if ([self.view isKindOfClass: [UIScrollView class]] == YES)
{
adjustableScrollView = (UIScrollView *)self.view;
}
// Otherwise check if the controller's view's first child is a scroll view.
else if ([[[self.view subviews] firstObject] isKindOfClass: [UIScrollView class]] == YES)
{
adjustableScrollView = (UIScrollView *)[[self.view subviews] firstObject];
}
return adjustableScrollView;
}
- (void)setScrollingTabBarControllerContentInsetAdjustment: (UIEdgeInsets)scrollingTabBarControllerContentInsetAdjustment
{
// Locate the scroll view to be adjusted.
UIScrollView *adjustableScrollView = [self adjustableScrollView];
// If there is no scroll view to adjust return immediately.
if (adjustableScrollView == nil)
{
return;
}
// Calculate the difference between the stored content inset adjustment and the adjustment being set.
UIEdgeInsets currentContentInset = self.scrollingTabBarControllerContentInsetAdjustment;
UIEdgeInsets contentInsetToAdd = scrollingTabBarControllerContentInsetAdjustment;
contentInsetToAdd.top -= currentContentInset.top;
contentInsetToAdd.bottom -= currentContentInset.bottom;
// Calculate the expected content offset once the content inset has been updated so it can be used later in a hack.
CGPoint expectedContentOffset = adjustableScrollView.contentOffset;
expectedContentOffset.y -= contentInsetToAdd.top;
// Update the content insets of the scroll view.
UIEdgeInsets contentInset = adjustableScrollView.contentInset;
contentInset.top += contentInsetToAdd.top;
contentInset.bottom += contentInsetToAdd.bottom;
adjustableScrollView.contentInset = contentInset;
adjustableScrollView.scrollIndicatorInsets = contentInset;
// HACK: For some reason the content offset does not get set correctly after the insets have been updated. This is a hack to ensure that the content offset is set correctly.
if (CGPointEqualToPoint(adjustableScrollView.contentOffset, expectedContentOffset) == NO)
{
adjustableScrollView.contentOffset = expectedContentOffset;
}
// Store the adjustment as an associated object so it can be used later.
NSValue *boxedValue = [NSValue valueWithUIEdgeInsets: scrollingTabBarControllerContentInsetAdjustment];
objc_setAssociatedObject(self, _ScrollingTabBarControllerContentInsetAdjustmentKey, boxedValue, OBJC_ASSOCIATION_COPY);
}
- (UIEdgeInsets)scrollingTabBarControllerContentInsetAdjustment
{
NSValue *boxedValue = objc_getAssociatedObject(self, _ScrollingTabBarControllerContentInsetAdjustmentKey);
UIEdgeInsets scrollingTabBarControllerContentInsetAdjustment = [boxedValue UIEdgeInsetsValue];
return scrollingTabBarControllerContentInsetAdjustment;
}
@end