Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Tabs] Give MDCTabBarDelegate pass through methods for "willDisplayCe…
…ll"/"didEndDisplayingCell" (#7518) One of the solutions proposed in #6275 is to provide pass through methods for the UICollectionViewDelegate methods `-collectionView:willDisplayCell:forItemAtIndexPath:` and `-collectionView:didEndDisplayingCell:forItemAtIndexPath:`. This is the one I chose, mainly because it seemed the most straightforward. [One comment](https://github.com/material-components/material-components-ios/issues/6275#issuecomment-453650714) said we should be careful about this approach, and [another](https://github.com/material-components/material-components-ios/issues/6275#issuecomment-454204635) said we needed to look more into the "pre-fetching" behavior of these methods. It seems like a good approach to me... Scrolling through the tabs in the TabBarTextOnlyExample with print statements seems to work how I would expect it to. 👍
- Loading branch information
1 parent
b8ea0ba
commit 08100bd
Showing
6 changed files
with
167 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright 2019-present the Material Components for iOS authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#import <Foundation/Foundation.h> | ||
#import "MDCTabBar.h" | ||
|
||
/** | ||
An additional delegate protocol for MDCTabBar that provides information about when UITabBarItems | ||
are about to be displayed and when they stop being displayed. | ||
*/ | ||
@protocol MDCTabBarDisplayDelegate | ||
|
||
/** | ||
This method is called sometime before the tab's view is displayed. | ||
*/ | ||
- (void)tabBar:(nonnull MDCTabBar *)tabBar willDisplayItem:(nonnull UITabBarItem *)item; | ||
|
||
/** | ||
This method is called sometime after the tab's view has stopped being displayed. | ||
*/ | ||
- (void)tabBar:(nonnull MDCTabBar *)tabBar didEndDisplayingItem:(nonnull UITabBarItem *)item; | ||
|
||
@end | ||
|
||
@interface MDCTabBar (MDCTabBarDisplayDelegate) | ||
|
||
/** | ||
A delegate that allows implementers to receive updates on when UITabBarItems are about to be | ||
displayed and when they stop being displayed. | ||
@note This property may be removed in a future version and should be used with that understanding. | ||
*/ | ||
@property(nonatomic, weak, nullable) NSObject<MDCTabBarDisplayDelegate> *displayDelegate; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
components/Tabs/tests/unit/MDCTabBarDisplayDelegateTests.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright 2019-present the Material Components for iOS authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#import <XCTest/XCTest.h> | ||
|
||
#import "MDCItemBar.h" | ||
#import "MDCTabBarDisplayDelegate.h" | ||
#import "MaterialTabs.h" | ||
|
||
@interface MDCTabBarDisplayDelegate : NSObject <MDCTabBarDisplayDelegate> | ||
@property(nonatomic, assign) BOOL willDisplayItemWasCalled; | ||
@property(nonatomic, assign) BOOL didEndDisplayingItemWasCalled; | ||
@end | ||
|
||
@implementation MDCTabBarDisplayDelegate | ||
|
||
- (void)tabBar:(MDCTabBar *)tabBar willDisplayItem:(UITabBarItem *)item { | ||
self.willDisplayItemWasCalled = YES; | ||
} | ||
|
||
- (void)tabBar:(MDCTabBar *)tabBar didEndDisplayingItem:(UITabBarItem *)item { | ||
self.didEndDisplayingItemWasCalled = YES; | ||
} | ||
|
||
@end | ||
|
||
@interface MDCTabBarDisplayDelegateTests : XCTestCase | ||
@end | ||
|
||
@implementation MDCTabBarDisplayDelegateTests | ||
|
||
- (void)testMDCTabBarDisplayDelegateTabBarWillDisplayItemWhenViewHasBeenLaidOut { | ||
// Given | ||
MDCTabBar *tabBar = [[MDCTabBar alloc] initWithFrame:CGRectZero]; | ||
MDCTabBarDisplayDelegate *displayDelegate = [[MDCTabBarDisplayDelegate alloc] init]; | ||
tabBar.displayDelegate = displayDelegate; | ||
CGFloat tabBarHeight = [MDCTabBar defaultHeightForItemAppearance:tabBar.itemAppearance]; | ||
tabBar.frame = CGRectMake(0, 0, 200, tabBarHeight); | ||
|
||
// When | ||
UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:@"first tab" image:nil tag:0]; | ||
UITabBarItem *item2 = [[UITabBarItem alloc] initWithTitle:@"second tab" image:nil tag:0]; | ||
tabBar.items = @[ item1, item2 ]; | ||
[tabBar setNeedsLayout]; | ||
[tabBar layoutIfNeeded]; | ||
|
||
// Then | ||
XCTAssertTrue(displayDelegate.willDisplayItemWasCalled); | ||
} | ||
|
||
- (void)testMDCTabBarDisplayDelegateTabBarDidEndDisplayingItemWhenViewHasBeenLaidOut { | ||
// Given | ||
MDCTabBar *tabBar = [[MDCTabBar alloc] initWithFrame:CGRectZero]; | ||
MDCTabBarDisplayDelegate *displayDelegate = [[MDCTabBarDisplayDelegate alloc] init]; | ||
tabBar.displayDelegate = displayDelegate; | ||
CGFloat tabBarHeight = [MDCTabBar defaultHeightForItemAppearance:tabBar.itemAppearance]; | ||
tabBar.frame = CGRectMake(0, 0, 200, tabBarHeight); | ||
UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:@"first tab" image:nil tag:0]; | ||
UITabBarItem *item2 = [[UITabBarItem alloc] initWithTitle:@"second tab" image:nil tag:0]; | ||
tabBar.items = @[ item1, item2 ]; | ||
[tabBar setNeedsLayout]; | ||
[tabBar layoutIfNeeded]; | ||
|
||
// When | ||
UITabBarItem *item3 = [[UITabBarItem alloc] initWithTitle:@"third tab" image:nil tag:0]; | ||
UITabBarItem *item4 = [[UITabBarItem alloc] initWithTitle:@"fourth tab" image:nil tag:0]; | ||
tabBar.items = @[ item3, item4 ]; | ||
[tabBar setNeedsLayout]; | ||
[tabBar layoutIfNeeded]; | ||
|
||
// Then | ||
XCTAssertTrue(displayDelegate.didEndDisplayingItemWasCalled); | ||
} | ||
|
||
@end |