@@ -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
0 commit comments