Skip to content

Commit fb2d9f8

Browse files
author
Zac White
committed
Added support to extend the backgroundView up or down independently to make it visible through a UIScrollView bounce.
1 parent cf2dc26 commit fb2d9f8

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

Classes/AQGridView.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ extern NSString * const AQGridViewSelectionDidChangeNotification;
128128
unsigned numColumns:6;
129129
unsigned separatorStyle:3;
130130
unsigned allowsSelection:1;
131+
unsigned backgroundViewExtendsUp:1;
132+
unsigned backgroundViewExtendsDown:1;
131133
unsigned usesPagedHorizontalScrolling:1;
132134
unsigned updating:1; // unused
133135
unsigned ignoreTouchSelect:1;
@@ -213,6 +215,8 @@ extern NSString * const AQGridViewSelectionDidChangeNotification;
213215
@property (nonatomic, assign) BOOL clipsContentWidthToBounds __attribute__((deprecated)); // default is YES. If you want to enable horizontal scrolling, set this to NO.
214216

215217
@property (nonatomic, retain) UIView * backgroundView; // specifies a view to place behind the cells
218+
@property (nonatomic) BOOL backgroundViewExtendsUp; // default is NO. If YES, the background view extends upward and is visible during a bounce.
219+
@property (nonatomic) BOOL backgroundViewExtendsDown; // default is NO. If YES, the background view extends downward and is visible during a bounce.
216220
@property (nonatomic) BOOL usesPagedHorizontalScrolling; // default is NO, and scrolls verticalls only. Set to YES to have horizontal-only scrolling by page.
217221

218222
@property (nonatomic) AQGridViewCellSeparatorStyle separatorStyle; // default is AQGridViewCellSeparatorStyleEmptySpace

Classes/AQGridView.m

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,26 @@ - (void) setAllowsSelection: (BOOL) value
220220
_flags.allowsSelection = (value ? 1 : 0);
221221
}
222222

223+
- (BOOL) backgroundViewExtendsDown
224+
{
225+
return ( _flags.backgroundViewExtendsDown);
226+
}
227+
228+
- (void) setBackgroundViewExtendsDown: (BOOL) value
229+
{
230+
_flags.backgroundViewExtendsDown = (value ? 1 : 0);
231+
}
232+
233+
- (BOOL) backgroundViewExtendsUp
234+
{
235+
return ( _flags.backgroundViewExtendsUp);
236+
}
237+
238+
- (void) setBackgroundViewExtendsUp: (BOOL) value
239+
{
240+
_flags.backgroundViewExtendsUp = (value ? 1 : 0);
241+
}
242+
223243
- (BOOL) requiresSelection
224244
{
225245
return ( _flags.requiresSelection );
@@ -596,6 +616,8 @@ - (void) reloadData
596616
_flags.allCellsNeedLayout = 1;
597617
}
598618

619+
#define MAX_BOUNCE_DISTANCE (500.0f)
620+
599621
- (void) layoutSubviews
600622
{
601623
if ( (_flags.needsReload == 1) && (_animationCount == 0) && (_reloadingSuspendedCount == 0) )
@@ -619,7 +641,18 @@ - (void) layoutSubviews
619641
rect.size.width = self.bounds.size.width;
620642
rect.size.height = self.contentSize.height - (_gridData.topPadding + _gridData.bottomPadding);
621643
rect.origin.y += _gridData.topPadding;
622-
self.backgroundView.frame = rect;
644+
645+
CGRect backgroundRect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
646+
647+
if ([self backgroundViewExtendsUp]) {
648+
backgroundRect.origin.y = backgroundRect.origin.y - MAX_BOUNCE_DISTANCE;
649+
}
650+
651+
if ([self backgroundViewExtendsDown]) {
652+
backgroundRect.size.height = backgroundRect.size.height + MAX_BOUNCE_DISTANCE;
653+
}
654+
655+
self.backgroundView.frame = backgroundRect;
623656

624657
if ( _headerView != nil )
625658
{
@@ -1091,7 +1124,18 @@ - (void) setBackgroundView: (UIView *) newView
10911124
_backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
10921125
CGRect frame = self.bounds;
10931126
frame.size = self.contentSize;
1094-
_backgroundView.frame = self.bounds;//UIEdgeInsetsInsetRect( frame, self.contentInset );
1127+
1128+
CGRect backgroundRect = CGRectMake(0.0f, 0.0f, self.bounds.size.width, self.bounds.size.height);
1129+
1130+
if ([self backgroundViewExtendsUp]) {
1131+
backgroundRect.origin.y = backgroundRect.origin.y - MAX_BOUNCE_DISTANCE;
1132+
}
1133+
1134+
if ([self backgroundViewExtendsDown]) {
1135+
backgroundRect.size.height = backgroundRect.size.height + MAX_BOUNCE_DISTANCE;
1136+
}
1137+
1138+
_backgroundView.frame = backgroundRect;
10951139

10961140
[self insertSubview: _backgroundView atIndex: 0];
10971141

0 commit comments

Comments
 (0)