Skip to content
This repository was archived by the owner on Jun 19, 2025. It is now read-only.

Commit aa28df4

Browse files
Merge pull request #399 from diogo-lins/issue/18840-19514-emptyview-height-v2
Fix position of empty view to avoid message disappearing when keyboard shows up
2 parents d1e5dba + 2fa2afa commit aa28df4

File tree

4 files changed

+98
-78
lines changed

4 files changed

+98
-78
lines changed

Example/Podfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PODS:
2-
- WPMediaPicker (1.8.6)
2+
- WPMediaPicker (1.8.7-beta.1)
33

44
DEPENDENCIES:
55
- WPMediaPicker (from `../`)
@@ -9,7 +9,7 @@ EXTERNAL SOURCES:
99
:path: "../"
1010

1111
SPEC CHECKSUMS:
12-
WPMediaPicker: 749ebfa75fb2b6df4f5e5d9d0847e9512ad74d28
12+
WPMediaPicker: 59135aebb058a95a507045f93e478516729e5c0f
1313

1414
PODFILE CHECKSUM: 31590cb12765a73c9da27d6ea5b8b127c095d71d
1515

Pod/Classes/WPMediaPickerViewController.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@
222222
* @return An empty view to display or `nil` to not display any.
223223
*
224224
* If this method is not implemented, a default UILabel will be displayed.
225+
*
226+
* `emptyViewControllerForMediaPickerController` takes precedence over this method.
225227
*/
226228
- (nullable UIView *)emptyViewForMediaPickerController:(nonnull WPMediaPickerViewController *)picker;
227229

@@ -235,6 +237,8 @@
235237
*
236238
* If this method is not implemented, a default ViewController with a default
237239
* UILabel will be displayed.
240+
*
241+
* This method takes precedence over `emptyViewForMediaPickerController`.
238242
*/
239243
- (nullable UIViewController *)emptyViewControllerForMediaPickerController:(nonnull WPMediaPickerViewController *)picker;
240244

Pod/Classes/WPMediaPickerViewController.m

Lines changed: 91 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,14 @@ @interface WPMediaPickerViewController ()
4646

4747
@property (nonatomic, strong, readwrite) UISearchBar *searchBar;
4848
@property (nonatomic, strong) NSLayoutConstraint *searchBarTopConstraint;
49+
@property (nonatomic, assign) CGFloat currentKeyboardHeight;
4950

5051
@property (nonatomic, strong) UIView *emptyView;
52+
@property (nonatomic, strong) UIView *emptyViewContainer;
5153
@property (nonatomic, strong) UILabel *defaultEmptyView;
5254
@property (nonatomic, strong) UIViewController *emptyViewController;
5355
@property (nonatomic, strong) UIViewController *defaultEmptyViewController;
54-
56+
@property (nonatomic, strong) NSLayoutConstraint *emptyViewBottomConstraint;
5557

5658
@property (nonatomic, strong) WPActionBar *accessoryActionBar;
5759
@property (nonatomic, strong) UIButton *selectedActionButton;
@@ -97,16 +99,13 @@ - (void)viewDidLoad
9799
{
98100
[super viewDidLoad];
99101

100-
self.refreshControl = [[UIRefreshControl alloc] init];
101-
[self.refreshControl addTarget:self action:@selector(pullToRefresh:) forControlEvents:UIControlEventValueChanged];
102-
[self.collectionView addSubview:self.refreshControl];
103-
104102
// Setup subviews
103+
[self setupPullToRefresh];
105104
[self addCollectionViewToView];
106-
[self addEmptyViewToView];
107105
[self setupCollectionView];
108106
[self setupSearchBar];
109107
[self setupLayout];
108+
[self addEmptyViewContainer];
110109

111110
//setup data
112111
[self.dataSource setMediaTypeFilter:self.options.filter];
@@ -119,6 +118,13 @@ - (void)viewDidLoad
119118
[self refreshDataAnimated:NO];
120119
}
121120

121+
- (void)setupPullToRefresh
122+
{
123+
self.refreshControl = [[UIRefreshControl alloc] init];
124+
[self.refreshControl addTarget:self action:@selector(pullToRefresh:) forControlEvents:UIControlEventValueChanged];
125+
[self.collectionView addSubview:self.refreshControl];
126+
}
127+
122128
- (void)registerDataSourceObservers {
123129
__weak __typeof__(self) weakSelf = self;
124130
self.changesObserver = [self.dataSource registerChangeObserverBlock:
@@ -227,7 +233,7 @@ - (void)setupLayout
227233
layout.minimumInteritemSpacing = photoSpacing;
228234

229235
[self resetContentInset];
230-
[self centerEmptyView];
236+
[self.view layoutIfNeeded];
231237
}
232238

233239
- (void)resetContentInset
@@ -582,6 +588,27 @@ - (void)showCapture {
582588

583589
#pragma mark - Empty View support
584590

591+
/** An empty view container to hold the emptyViewController or emptyView that comes from the delegate
592+
*/
593+
- (void)addEmptyViewContainer
594+
{
595+
self.emptyViewContainer = [[UIView alloc] initWithFrame:self.collectionView.frame];
596+
[self.emptyViewContainer setTranslatesAutoresizingMaskIntoConstraints:NO];
597+
[self.collectionView addSubview:self.emptyViewContainer];
598+
599+
self.emptyViewBottomConstraint = [self.emptyViewContainer.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor];
600+
[self.emptyViewBottomConstraint setConstant:-self.currentKeyboardHeight];
601+
602+
[NSLayoutConstraint activateConstraints:
603+
@[
604+
[self.emptyViewContainer.topAnchor constraintEqualToAnchor:self.collectionView.topAnchor],
605+
self.emptyViewBottomConstraint,
606+
[self.emptyViewContainer.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
607+
[self.emptyViewContainer.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor]
608+
]
609+
];
610+
}
611+
585612
- (UIView *)emptyView
586613
{
587614
if (_emptyView) {
@@ -597,14 +624,14 @@ - (UIView *)emptyView
597624
return _emptyView;
598625
}
599626

600-
- (void)addEmptyViewToView
627+
/** Checks if the parentViewController is providing a custom empty ViewController to be added, if not, add a provided custom emptyView
628+
*/
629+
- (void)populateEmptyViewContainer
601630
{
602631
if ([self usingEmptyViewController]) {
603-
[self addEmptyViewControllerToView];
632+
[self addEmptyViewControllerToContainer];
604633
} else {
605-
if (self.emptyView.superview == nil) {
606-
[self.collectionView addSubview:_emptyView];
607-
}
634+
[self addEmptyViewToContainer];
608635
}
609636
}
610637

@@ -619,24 +646,45 @@ - (UILabel *)defaultEmptyView
619646
return _defaultEmptyView;
620647
}
621648

622-
#pragma mark - Empty View Controller support
623-
624-
- (void)addEmptyViewControllerToView
649+
- (void)addEmptyViewToContainer
625650
{
626-
if (self.emptyViewController && self.emptyViewController.view.superview == nil) {
627-
[self.collectionView addSubview:self.emptyViewController.view];
628-
self.emptyViewController.view.frame = self.collectionView.frame;
629-
[self addChildViewController:self.emptyViewController];
630-
[self.emptyViewController didMoveToParentViewController:self];
631-
[self centerEmptyView];
651+
if (self.emptyView != nil && self.emptyView.superview != nil) {
652+
return;
632653
}
654+
655+
[self.emptyView setTranslatesAutoresizingMaskIntoConstraints:NO];
656+
[self.emptyViewContainer addSubview:self.emptyView];
657+
658+
[NSLayoutConstraint activateConstraints:
659+
@[
660+
[self.emptyView.centerYAnchor constraintEqualToAnchor:self.emptyViewContainer.centerYAnchor],
661+
[self.emptyView.centerXAnchor constraintEqualToAnchor:self.emptyViewContainer.centerXAnchor]
662+
]
663+
];
633664
}
634665

635-
- (void)removeEmptyViewControllerFromView
666+
#pragma mark - Empty View Controller support
667+
668+
- (void)addEmptyViewControllerToContainer
636669
{
637-
[_emptyViewController willMoveToParentViewController:nil];
638-
[_emptyViewController.view removeFromSuperview];
639-
[_emptyViewController removeFromParentViewController];
670+
if (self.emptyViewController != nil && self.emptyViewController.view.superview != nil) {
671+
return;
672+
}
673+
674+
[self addChildViewController:self.emptyViewController];
675+
[self.emptyViewController.view setTranslatesAutoresizingMaskIntoConstraints:NO];
676+
[self.emptyViewContainer addSubview:self.emptyViewController.view];
677+
678+
[NSLayoutConstraint activateConstraints:
679+
@[
680+
[self.emptyViewController.view.topAnchor constraintEqualToAnchor:self.emptyViewContainer.topAnchor],
681+
[self.emptyViewController.view.bottomAnchor constraintEqualToAnchor:self.emptyViewContainer.bottomAnchor],
682+
[self.emptyViewController.view.leadingAnchor constraintEqualToAnchor:self.emptyViewContainer.leadingAnchor],
683+
[self.emptyViewController.view.trailingAnchor constraintEqualToAnchor:self.emptyViewContainer.trailingAnchor]
684+
]
685+
];
686+
687+
[self.emptyViewController didMoveToParentViewController:self];
640688
}
641689

642690
- (UIViewController *)emptyViewController
@@ -663,9 +711,16 @@ - (UIViewController *)defaultEmptyViewController
663711

664712
_defaultEmptyViewController = [[UIViewController alloc] init];
665713
UILabel *emptyViewLabel = self.defaultEmptyView;
666-
emptyViewLabel.center = _defaultEmptyViewController.view.center;
714+
[emptyViewLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
667715
[[_defaultEmptyViewController view] addSubview:emptyViewLabel];
668716

717+
[NSLayoutConstraint activateConstraints:
718+
@[
719+
[emptyViewLabel.centerYAnchor constraintEqualToAnchor:self.defaultEmptyViewController.view.centerYAnchor],
720+
[emptyViewLabel.centerXAnchor constraintEqualToAnchor:self.defaultEmptyViewController.view.centerXAnchor]
721+
]
722+
];
723+
669724
return _defaultEmptyViewController;
670725
}
671726

@@ -859,18 +914,14 @@ - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSe
859914

860915
- (void)toggleEmptyViewFor:(NSInteger)numberOfAssets
861916
{
862-
if ([self usingEmptyViewController]) {
863-
if (numberOfAssets > 0) {
864-
[self removeEmptyViewControllerFromView];
865-
} else {
866-
[self addEmptyViewControllerToView];
867-
}
917+
if (numberOfAssets > 0) {
918+
[self.emptyViewContainer setHidden: YES];
868919
} else {
869-
[self.emptyView setHidden:(numberOfAssets != 0)];
920+
[self.emptyViewContainer setHidden: NO];
921+
[self populateEmptyViewContainer];
870922
}
871923
}
872924

873-
874925
- (id<WPMediaAsset>)assetForPosition:(NSIndexPath *)indexPath
875926
{
876927
NSInteger itemPosition = indexPath.item;
@@ -1423,9 +1474,11 @@ - (void)keyboardWillShowNotification:(NSNotification *)notification
14231474
}
14241475
self.collectionView.contentInset = contentInset;
14251476
self.collectionView.scrollIndicatorInsets = contentInset;
1477+
self.currentKeyboardHeight = keyboardFrameEnd.size.height;
1478+
[self.emptyViewBottomConstraint setConstant:-self.currentKeyboardHeight];
14261479

14271480
[UIView animateWithDuration:0.2 animations:^{
1428-
[self centerEmptyView];
1481+
[self.view layoutIfNeeded];
14291482
[self.collectionView.collectionViewLayout invalidateLayout];
14301483
}];
14311484
}
@@ -1436,52 +1489,15 @@ - (void)keyboardWillHideNotification:(NSNotification *)notification
14361489
contentInset.bottom = 0.f;
14371490
self.collectionView.contentInset = contentInset;
14381491
self.collectionView.scrollIndicatorInsets = contentInset;
1492+
self.currentKeyboardHeight = 0.f;
1493+
[self.emptyViewBottomConstraint setConstant:-self.currentKeyboardHeight];
14391494

14401495
[UIView animateWithDuration:0.2 animations:^{
1441-
[self centerEmptyView];
1496+
[self.view layoutIfNeeded];
14421497
[self.collectionView.collectionViewLayout invalidateLayout];
14431498
}];
14441499
}
14451500

1446-
/**
1447-
Centers the empty view taking into account the collection view height and content insets.
1448-
*/
1449-
- (void)centerEmptyView
1450-
{
1451-
if (self.emptyViewController) {
1452-
CGRect emptyViewFrame = [self getEmptyViewFrame];
1453-
1454-
if ([self.searchBar.text isEqualToString:@""]) {
1455-
emptyViewFrame.origin.y -= self.searchBar.frame.size.height/2;
1456-
}
1457-
emptyViewFrame.size.height -= self.view.layoutMargins.bottom;
1458-
emptyViewFrame.size.height -= self.view.layoutMargins.top;
1459-
_emptyViewController.view.frame = emptyViewFrame;
1460-
} else {
1461-
self.emptyView.center = self.collectionView.center;
1462-
self.emptyView.frame = [self getEmptyViewFrame];
1463-
}
1464-
}
1465-
1466-
- (CGRect)getEmptyViewFrame
1467-
{
1468-
CGRect emptyViewFrame;
1469-
1470-
if (_emptyViewController) {
1471-
emptyViewFrame = self.collectionView.frame;
1472-
} else {
1473-
emptyViewFrame = self.emptyView.frame;
1474-
}
1475-
1476-
CGFloat superviewHeight = self.collectionView.frame.size.height;
1477-
CGFloat totalInsets = self.collectionView.contentInset.top + self.collectionView.contentInset.bottom;
1478-
1479-
superviewHeight = superviewHeight - totalInsets > 0 ? superviewHeight - totalInsets : superviewHeight;
1480-
emptyViewFrame.origin.y = (superviewHeight / 2.0) - (emptyViewFrame.size.height / 2.0) + self.collectionView.frame.origin.y;
1481-
1482-
return emptyViewFrame;
1483-
}
1484-
14851501
#pragma mark - WPAssetViewControllerDelegate
14861502

14871503
- (void)assetViewController:(WPAssetViewController *)assetPreviewVC selectionChanged:(BOOL)selected

WPMediaPicker.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Pod::Spec.new do |s|
44
s.name = 'WPMediaPicker'
5-
s.version = '1.8.6'
5+
s.version = '1.8.7-beta.1'
66

77
s.summary = 'WPMediaPicker is an iOS controller that allows capture and picking of media assets.'
88
s.description = <<-DESC

0 commit comments

Comments
 (0)