Skip to content

Commit 87b2a50

Browse files
committed
changed how the keyboard resizes the collection view
1 parent 4b62910 commit 87b2a50

4 files changed

Lines changed: 37 additions & 29 deletions

File tree

ARMRef/ARMRef/Source/CollectionView/ACollectionView.m

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,39 @@ @implementation ACollectionView
3030
#pragma mark - ACollectionView
3131

3232
- (instancetype) init {
33-
if (self = [super initWithFrame:CGRectZero collectionViewLayout:UICollectionViewFlowLayout.new]) {
33+
if (self = [super initWithFrame:CGRectZero collectionViewLayout:ACollectionView._layout]) {
3434
self.backgroundColor = [UIColor colorFromHex:0x333e48];
3535
self.contentInset = UIEdgeInsetsMake(10.0f, 0.0f, 10.0f, 0.0f);
3636
self.alwaysBounceVertical = YES;
3737
self.translatesAutoresizingMaskIntoConstraints = NO;
3838

3939
[self registerClass:ACollectionViewCell.class forCellWithReuseIdentifier:ACollectionViewCell.identifier];
40+
41+
[NSNotificationCenter.defaultCenter addObserver:self
42+
selector:@selector(_keyboardWillChangeFrameNotification:)
43+
name:UIKeyboardWillChangeFrameNotification
44+
object:nil];
4045
}
4146

4247
return self;
4348
}
4449

50+
#pragma mark - Private
51+
52+
+ (UICollectionViewFlowLayout *) _layout {
53+
UICollectionViewFlowLayout *layout = UICollectionViewFlowLayout.new;
54+
layout.sectionInset = UIEdgeInsetsMake(0.0f, 10.0f, 0.0f, 10.0f);
55+
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
56+
layout.sectionInsetReference = UICollectionViewFlowLayoutSectionInsetFromContentInset;
57+
58+
return layout;
59+
}
60+
61+
- (void) _keyboardWillChangeFrameNotification:(NSNotification *)notification {
62+
CGRect endKeyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
63+
64+
CGFloat bottomInset = (endKeyboardFrame.origin.y > self.bounds.size.height ? 10.0f : endKeyboardFrame.size.height + 10.0f);
65+
self.contentInset = UIEdgeInsetsMake(10.0f, 0.0f, bottomInset, 0.0f);
66+
}
67+
4568
@end

ARMRef/ARMRef/Source/CollectionView/ACollectionViewCell.m

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ - (void) layoutSubviews {
5454
[super layoutSubviews];
5555

5656
// Top label
57-
CGSize topSize = [self.topLabel sizeThatFits:CGSizeMake(self.bounds.size.width - 10.0f, CGFLOAT_MAX)];
57+
CGSize topSize = [self.topLabel sizeThatFits:CGSizeMake(self.contentView.bounds.size.width - 20.0f, CGFLOAT_MAX)];
5858
self.topLabel.frame = CGRectMake(10.0f, 9.0f, topSize.width, topSize.height);
5959

6060
// Bottom label
61-
CGSize bottomSize = [self.bottomLabel sizeThatFits:CGSizeMake(self.bounds.size.width - 10.0f, CGFLOAT_MAX)];
61+
CGSize bottomSize = [self.bottomLabel sizeThatFits:CGSizeMake(self.contentView.bounds.size.width - 20.0f, CGFLOAT_MAX)];
6262
self.bottomLabel.frame = CGRectMake(10.0f, CGRectGetMaxY(self.topLabel.frame), bottomSize.width, bottomSize.height);
6363
}
6464

@@ -83,12 +83,12 @@ + (NSString *) identifier {
8383
- (CGFloat) maxSizeForInstruction:(AInstruction *)instruction withWidth:(CGFloat)width {
8484
self.topLabel.text = instruction.mnemonic;
8585
self.bottomLabel.text = instruction.shortDesc;
86+
87+
CGFloat padding = 18.0f;
88+
CGSize topSize = [self.topLabel sizeThatFits:CGSizeMake(width, CGFLOAT_MAX)];
89+
CGSize bottomSize = [self.bottomLabel sizeThatFits:CGSizeMake(width, CGFLOAT_MAX)];
8690

87-
CGFloat finalWidth = width - 10.0f;
88-
CGSize topSize = [self.topLabel sizeThatFits:CGSizeMake(finalWidth, CGFLOAT_MAX)];
89-
CGSize bottomSize = [self.bottomLabel sizeThatFits:CGSizeMake(finalWidth, CGFLOAT_MAX)];
90-
91-
return topSize.height + bottomSize.height + 18.0f;
91+
return topSize.height + bottomSize.height + padding;
9292
}
9393

9494
#pragma mark - Setters

ARMRef/ARMRef/Source/CollectionView/ACollectionViewDataHandle.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,17 @@ - (CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectio
6767
ACollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ACollectionViewCell.identifier forIndexPath:indexPath];
6868
cell.instruction = self.instructions[indexPath.row];
6969

70-
CGFloat maxSize = [cell maxSizeForInstruction:self.instructions[indexPath.row] withWidth:collectionView.bounds.size.width];
70+
CGFloat maxSize = [cell maxSizeForInstruction:self.instructions[indexPath.row] withWidth:collectionView.bounds.size.width - 20.0f];
7171

72-
return CGSizeMake(collectionView.bounds.size.width, maxSize);
72+
return CGSizeMake(collectionView.bounds.size.width - 20.0f, maxSize);
7373
}
7474

7575
- (UIEdgeInsets) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
76-
return UIEdgeInsetsZero;
76+
return UIEdgeInsetsMake(0.0f, 10.0f, 0.0f, 10.0f);
7777
}
7878

7979
- (CGFloat) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
80-
return 4.0f;
80+
return 10.0f;
8181
}
8282

8383
- (CGFloat) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {

ARMRef/ARMRef/Source/Main/AMainViewController.m

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ @interface AMainViewController () <UISearchBarDelegate, ACollectionViewDelegates
3535
@property (nonatomic, strong) UILabel *noDataLabel;
3636
@property (nonatomic, strong) ACollectionView *collectionView;
3737
@property (nonatomic, strong) ACollectionViewDataHandle *collectionViewDataHandle;
38-
@property (nonatomic, assign) CGRect keyboardFrame;
3938

4039
@end
4140

@@ -52,12 +51,6 @@ - (instancetype) initWithLoader:(AInstructionLoader *)loader {
5251
selector:@selector(_loaderNotification:)
5352
name:AInstructionLoaderFinishedNotificaton
5453
object:nil];
55-
56-
[NSNotificationCenter.defaultCenter addObserver:self
57-
selector:@selector(_keyboardWillChangeFrameNotification:)
58-
name:UIKeyboardWillChangeFrameNotification
59-
object:nil];
60-
6154
}
6255

6356
return self;
@@ -92,12 +85,10 @@ - (void) viewWillLayoutSubviews {
9285
self.noDataLabel.frame = self.view.bounds;
9386

9487
// Collection view
95-
CGFloat padding = (!leftRightInset ? 8.0f : 0.0f);
96-
CGRect collectionViewFrame = CGRectMake(self.view.safeAreaInsets.left + padding,
88+
CGRect collectionViewFrame = CGRectMake(self.view.safeAreaInsets.left,
9789
CGRectGetMaxY(self.searchBar.frame) + 5.0f,
98-
self.view.bounds.size.width - (leftRightInset + (padding * 2.0f)),
90+
self.view.bounds.size.width - leftRightInset,
9991
self.view.bounds.size.height - (CGRectGetMaxY(self.searchBar.frame) + 5.0f));
100-
if (self.searchBar.isFirstResponder) collectionViewFrame.size.height -= self.keyboardFrame.size.height;
10192

10293
self.collectionView.frame = collectionViewFrame;
10394
[self.collectionView.collectionViewLayout invalidateLayout];
@@ -137,12 +128,6 @@ - (void) _showNoDataStyle:(BOOL)show {
137128
self.collectionView.alpha = (show ? 0.0f : 1.0f);
138129
}
139130

140-
- (void) _keyboardWillChangeFrameNotification:(NSNotification *)notification {
141-
self.keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
142-
143-
[self.view setNeedsLayout];
144-
}
145-
146131
#pragma mark - Lazy
147132

148133
- (UISearchBar *) searchBar {

0 commit comments

Comments
 (0)