Skip to content

Commit

Permalink
[Tabs] Add initial support for custom view. (#7678)
Browse files Browse the repository at this point in the history
Creates basic support for a custom view property on UITabBarItem. Although not
a category on UITabBarItem, it can be used in the same places UITabBarItem can
be used. This will allow clients of MDCTabBarView to provide a custom view to
replace the default presentation of the title and image.

Closes #7676
  • Loading branch information
Robert Moore committed Jun 24, 2019
1 parent b13f781 commit 325e269
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 0 deletions.
24 changes: 24 additions & 0 deletions components/Tabs/src/TabBarView/MDCTabBarItem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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 <UIKit/UIKit.h>

#import "MDCTabBarItemCustomViewing.h"

/**
A subclass of @c UITabBarItem that supports a custom view property.
*/
__attribute__((objc_subclassing_restricted)) @interface MDCTabBarItem
: UITabBarItem<MDCTabBarItemCustomViewing>
@end
21 changes: 21 additions & 0 deletions components/Tabs/src/TabBarView/MDCTabBarItem.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// 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 "MDCTabBarItem.h"

@implementation MDCTabBarItem

@synthesize mdc_customView = _mdc_customView;

@end
31 changes: 31 additions & 0 deletions components/Tabs/src/TabBarView/MDCTabBarItemCustomViewing.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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 "MDCTabBarSelectionIndicatorSupporting.h"

/**
Defines the necessary APIs for MDCTabBarView to use a UITabBarItem for a custom view property.
@note Although UIKit defines such a property on @c UIBarButtonItem, no similar property is publicly
documented on @c UITabBarItem.
*/
@protocol MDCTabBarItemCustomViewing

/** A custom view to be displayed for a tab bar item. */
@property(nullable, nonatomic, strong)
UIView<MDCTabBarSelectionIndicatorSupporting> *mdc_customView;

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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>

/**
A simple protocol that indicates the responder can be targeted by an
@c MDCTabBarSelectionIndicatorTemplate.
@seealso MDCTabBarIndicatorTemplate
*/
@protocol MDCTabBarSelectionIndicatorSupporting

/**
The bounds of the receiver.
*/
@property(readonly) CGRect bounds;

/**
The frame of the content of the receiver. Used to position the Selection Indicator relative to
the content.
*/
@property(readonly) CGRect contentFrame;

@end
3 changes: 3 additions & 0 deletions components/Tabs/src/TabBarView/MaterialTabs+TabBarView.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#import "MDCTabBarItem.h"
#import "MDCTabBarItemCustomViewing.h"
#import "MDCTabBarSelectionIndicatorSupporting.h"
#import "MDCTabBarView.h"
48 changes: 48 additions & 0 deletions components/Tabs/tests/unit/TabBarView/MDCTabBarItemTests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 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 "MaterialTabs+TabBarView.h"

@interface MDCTabBarItemTestCustomViewTestFake : UIView <MDCTabBarSelectionIndicatorSupporting>
@end

@implementation MDCTabBarItemTestCustomViewTestFake

- (CGRect)contentFrame {
return CGRectZero;
}

@end

@interface MDCTabBarItemTests : XCTestCase
@end

@implementation MDCTabBarItemTests

- (void)testGetterSetterForCustomView {
// Given
MDCTabBarItemTestCustomViewTestFake *customView =
[[MDCTabBarItemTestCustomViewTestFake alloc] init];
MDCTabBarItem *barItem = [[MDCTabBarItem alloc] init];

// When
barItem.mdc_customView = customView;

// Then
XCTAssertEqual(barItem.mdc_customView, customView);
}

@end

0 comments on commit 325e269

Please sign in to comment.