/
MDCTabBar.m
717 lines (583 loc) · 21.5 KB
/
MDCTabBar.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
// Copyright 2016-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 "MDCTabBar.h"
#import "MDCTabBarAlignment.h"
#import "MDCTabBarItemAppearance.h"
#import "MDCTabBarTextTransform.h"
#import "MDCItemBarDelegate.h"
#import <MDFInternationalization/MDFInternationalization.h>
#import "private/MDCItemBar.h"
#import "private/MDCItemBarAlignment.h"
#import "private/MDCItemBarStyle.h"
#import "MaterialInk.h"
#import "MaterialRipple.h"
#import "MDCTabBarExtendedAlignment.h"
#import "MDCTabBarDelegate.h"
#import "MDCTabBarUnderlineIndicatorTemplate.h"
#import "MaterialTypography.h"
/// Padding between image and title in points, according to the spec.
static const CGFloat kImageTitleSpecPadding = 10;
/// Adjustment added to spec measurements to compensate for internal paddings.
static const CGFloat kImageTitlePaddingAdjustment = -3;
// Heights based on the spec: https://material.io/go/design-tabs
/// Height for image-only tab bars, in points.
static const CGFloat kImageOnlyBarHeight = 48;
/// Height for image-only tab bars, in points.
static const CGFloat kTitleOnlyBarHeight = 48;
/// Height for image-and-title tab bars, in points.
static const CGFloat kTitledImageBarHeight = 72;
/// Height for bottom navigation bars, in points.
static const CGFloat kBottomNavigationBarHeight = 56;
/// Maximum width for individual items in bottom navigation bars, in points.
static const CGFloat kBottomNavigationMaximumItemWidth = 168;
/// Title-image padding for bottom navigation bars, in points.
static const CGFloat kBottomNavigationTitleImagePadding = 3;
/// Height for the bottom divider.
static const CGFloat kBottomNavigationBarDividerHeight = 1;
static MDCItemBarAlignment MDCItemBarAlignmentForTabBarAlignment(
MDCTabBarExtendedAlignment alignment) {
switch (alignment) {
case MDCTabBarExtendedAlignmentCenter:
return MDCItemBarAlignmentCenter;
case MDCTabBarExtendedAlignmentLeading:
return MDCItemBarAlignmentLeading;
case MDCTabBarExtendedAlignmentJustified:
return MDCItemBarAlignmentJustified;
case MDCTabBarExtendedAlignmentBestEffortJustified:
return MDCItemBarAlignmentBestEffortJustified;
case MDCTabBarExtendedAlignmentCenterSelected:
return MDCItemBarAlignmentCenterSelected;
}
NSCAssert(0, @"Invalid alignment value %ld", (long)alignment);
return MDCItemBarAlignmentLeading;
}
static inline UIColor *RippleColor() {
return [UIColor colorWithWhite:1 alpha:(CGFloat)0.7];
}
@protocol MDCTabBarSizeClassDelegate;
@protocol MDCTabBarDisplayDelegate;
@interface MDCTabBar ()
@property(nonatomic, weak, nullable) id<MDCTabBarSizeClassDelegate> sizeClassDelegate;
@property(nonatomic, weak, nullable) id<MDCTabBarDisplayDelegate> displayDelegate;
@end
@interface MDCTabBar () <MDCItemBarDelegate>
@end
@implementation MDCTabBar {
/// Item bar responsible for displaying the actual tab bar content.
MDCItemBar *_itemBar;
UIView *_dividerBar;
// Flags tracking if properties are unset and using default values.
BOOL _hasDefaultAlignment;
BOOL _hasDefaultItemAppearance;
// For properties which have been set, these store the new fixed values.
MDCTabBarAlignment _alignmentOverride;
MDCTabBarItemAppearance _itemAppearanceOverride;
UIColor *_selectedTitleColor;
UIColor *_unselectedTitleColor;
UIColor *_inkColor;
}
// Inherit UIView's tintColor logic.
@dynamic tintColor;
@synthesize alignment = _alignment;
@synthesize barPosition = _barPosition;
@synthesize itemAppearance = _itemAppearance;
@synthesize mdc_overrideBaseElevation = _mdc_overrideBaseElevation;
@synthesize mdc_elevationDidChangeBlock = _mdc_elevationDidChangeBlock;
#pragma mark - Initialization
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self commonMDCTabBarInit];
[self updateItemBarStyle];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self commonMDCTabBarInit];
}
return self;
}
- (void)commonMDCTabBarInit {
_bottomDividerColor = [UIColor clearColor];
_selectedItemTintColor = [UIColor whiteColor];
_unselectedItemTintColor = [UIColor colorWithWhite:1 alpha:(CGFloat)0.7];
_selectedTitleColor = _selectedItemTintColor;
_unselectedTitleColor = _unselectedItemTintColor;
_inkColor = RippleColor();
_rippleColor = RippleColor();
self.clipsToBounds = YES;
_barPosition = UIBarPositionAny;
_hasDefaultItemAppearance = YES;
_hasDefaultAlignment = YES;
// Set default values
_alignment = [self computedAlignment];
_titleTextTransform = MDCTabBarTextTransformAutomatic;
_itemAppearance = [self computedItemAppearance];
_selectionIndicatorTemplate = [MDCTabBar defaultSelectionIndicatorTemplate];
_selectedItemTitleFont = [MDCTypography buttonFont];
_unselectedItemTitleFont = [MDCTypography buttonFont];
// Create item bar.
_itemBar = [[MDCItemBar alloc] initWithFrame:self.bounds];
_itemBar.tabBar = self;
_itemBar.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
_itemBar.delegate = self;
_itemBar.alignment =
MDCItemBarAlignmentForTabBarAlignment((MDCTabBarExtendedAlignment)_alignment);
[self addSubview:_itemBar];
CGFloat dividerTop = CGRectGetMaxY(self.bounds) - kBottomNavigationBarDividerHeight;
_dividerBar = [[UIView alloc] initWithFrame:CGRectMake(0, dividerTop, CGRectGetWidth(self.bounds),
kBottomNavigationBarDividerHeight)];
_dividerBar.autoresizingMask =
UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
_dividerBar.backgroundColor = _bottomDividerColor;
[self addSubview:_dividerBar];
[self updateItemBarStyle];
_mdc_overrideBaseElevation = -1;
}
- (void)layoutSubviews {
[super layoutSubviews];
CGSize sizeThatFits = [_itemBar sizeThatFits:self.bounds.size];
_itemBar.frame = CGRectMake(0, 0, sizeThatFits.width, sizeThatFits.height);
}
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
[super traitCollectionDidChange:previousTraitCollection];
if (self.traitCollectionDidChangeBlock) {
self.traitCollectionDidChangeBlock(self, previousTraitCollection);
}
}
- (CGFloat)mdc_currentElevation {
return 0;
}
#pragma mark - Public
+ (CGFloat)defaultHeightForBarPosition:(UIBarPosition)position
itemAppearance:(MDCTabBarItemAppearance)appearance {
if ([self isTopTabsForPosition:position]) {
switch (appearance) {
case MDCTabBarItemAppearanceTitledImages:
return kTitledImageBarHeight;
case MDCTabBarItemAppearanceTitles:
return kTitleOnlyBarHeight;
case MDCTabBarItemAppearanceImages:
return kImageOnlyBarHeight;
}
} else {
// Bottom navigation has a fixed height.
return kBottomNavigationBarHeight;
}
}
+ (CGFloat)defaultHeightForItemAppearance:(MDCTabBarItemAppearance)appearance {
return [self defaultHeightForBarPosition:UIBarPositionAny itemAppearance:appearance];
}
- (void)setTitleColor:(nullable UIColor *)color forState:(MDCTabBarItemState)state {
switch (state) {
case MDCTabBarItemStateNormal:
_unselectedTitleColor = color;
break;
case MDCTabBarItemStateSelected:
_selectedTitleColor = color;
break;
}
[self updateItemBarStyle];
}
- (nullable UIColor *)titleColorForState:(MDCTabBarItemState)state {
switch (state) {
case MDCTabBarItemStateNormal:
return _unselectedTitleColor;
break;
case MDCTabBarItemStateSelected:
return _selectedTitleColor;
break;
}
}
- (void)setImageTintColor:(nullable UIColor *)color forState:(MDCTabBarItemState)state {
switch (state) {
case MDCTabBarItemStateNormal:
_unselectedItemTintColor = color;
break;
case MDCTabBarItemStateSelected:
_selectedItemTintColor = color;
break;
}
[self updateItemBarStyle];
}
- (nullable UIColor *)imageTintColorForState:(MDCTabBarItemState)state {
switch (state) {
case MDCTabBarItemStateNormal:
return _unselectedItemTintColor;
break;
case MDCTabBarItemStateSelected:
return _selectedItemTintColor;
break;
}
}
- (void)setDelegate:(id<MDCTabBarDelegate>)delegate {
if (delegate != _delegate) {
_delegate = delegate;
// Delegate determines the position - update immediately.
[self updateItemBarPosition];
}
}
- (NSArray<UITabBarItem *> *)items {
return _itemBar.items;
}
- (void)setItems:(NSArray<UITabBarItem *> *)items {
_itemBar.items = items;
}
- (UITabBarItem *)selectedItem {
return _itemBar.selectedItem;
}
- (void)setSelectedItem:(UITabBarItem *)selectedItem {
_itemBar.selectedItem = selectedItem;
}
- (void)setSelectedItem:(UITabBarItem *)selectedItem animated:(BOOL)animated {
[_itemBar setSelectedItem:selectedItem animated:animated];
}
- (void)setBarTintColor:(UIColor *)barTintColor {
if (_barTintColor != barTintColor && ![_barTintColor isEqual:barTintColor]) {
_barTintColor = barTintColor;
// Update background color.
self.backgroundColor = barTintColor;
}
}
- (void)setInkColor:(UIColor *)inkColor {
if (_inkColor != inkColor && ![_inkColor isEqual:inkColor]) {
_inkColor = inkColor;
[self updateItemBarStyle];
}
}
- (UIColor *)inkColor {
return _inkColor;
}
- (void)setRippleColor:(UIColor *)rippleColor {
if (_rippleColor == rippleColor || [_rippleColor isEqual:rippleColor]) {
return;
}
_rippleColor = rippleColor;
[self updateItemBarStyle];
}
- (void)setUnselectedItemTitleFont:(UIFont *)unselectedItemTitleFont {
if ((unselectedItemTitleFont != _unselectedItemTitleFont) &&
![unselectedItemTitleFont isEqual:_unselectedItemTitleFont]) {
_unselectedItemTitleFont = unselectedItemTitleFont;
[self updateItemBarStyle];
}
}
- (void)setSelectedItemTitleFont:(UIFont *)selectedItemTitleFont {
if ((selectedItemTitleFont != _selectedItemTitleFont) &&
![selectedItemTitleFont isEqual:_selectedItemTitleFont]) {
_selectedItemTitleFont = selectedItemTitleFont;
[self updateItemBarStyle];
}
}
- (void)setAlignment:(MDCTabBarAlignment)alignment {
[self setAlignment:alignment animated:NO];
}
- (void)setAlignment:(MDCTabBarAlignment)alignment animated:(BOOL)animated {
_hasDefaultAlignment = NO;
_alignmentOverride = alignment;
[self internalSetAlignment:[self computedAlignment] animated:animated];
}
- (void)setItemAppearance:(MDCTabBarItemAppearance)itemAppearance {
_hasDefaultItemAppearance = NO;
_itemAppearanceOverride = itemAppearance;
[self internalSetItemAppearance:[self computedItemAppearance]];
}
- (void)setSelectedItemTintColor:(UIColor *)selectedItemTintColor {
if (_selectedItemTintColor != selectedItemTintColor &&
![_selectedItemTintColor isEqual:selectedItemTintColor]) {
_selectedItemTintColor = selectedItemTintColor;
_selectedTitleColor = selectedItemTintColor;
[self updateItemBarStyle];
}
}
- (void)setUnselectedItemTintColor:(UIColor *)unselectedItemTintColor {
if (_unselectedItemTintColor != unselectedItemTintColor &&
![_unselectedItemTintColor isEqual:unselectedItemTintColor]) {
_unselectedItemTintColor = unselectedItemTintColor;
_unselectedTitleColor = unselectedItemTintColor;
[self updateItemBarStyle];
}
}
- (BOOL)displaysUppercaseTitles {
switch (self.titleTextTransform) {
case MDCTabBarTextTransformAutomatic:
return [MDCTabBar displaysUppercaseTitlesByDefaultForPosition:_barPosition];
case MDCTabBarTextTransformNone:
return NO;
case MDCTabBarTextTransformUppercase:
return YES;
}
}
- (void)setDisplaysUppercaseTitles:(BOOL)displaysUppercaseTitles {
self.titleTextTransform =
displaysUppercaseTitles ? MDCTabBarTextTransformUppercase : MDCTabBarTextTransformNone;
}
- (void)setTitleTextTransform:(MDCTabBarTextTransform)titleTextTransform {
if (titleTextTransform != _titleTextTransform) {
_titleTextTransform = titleTextTransform;
[self updateItemBarStyle];
}
}
- (void)setSelectionIndicatorTemplate:(id<MDCTabBarIndicatorTemplate>)selectionIndicatorTemplate {
id<MDCTabBarIndicatorTemplate> template = selectionIndicatorTemplate;
if (!template) {
template = [MDCTabBar defaultSelectionIndicatorTemplate];
}
_selectionIndicatorTemplate = template;
[self updateItemBarStyle];
}
- (void)setBottomDividerColor:(UIColor *)bottomDividerColor {
if (_bottomDividerColor != bottomDividerColor) {
_bottomDividerColor = bottomDividerColor;
_dividerBar.backgroundColor = _bottomDividerColor;
}
}
// UISemanticContentAttribute was added in iOS SDK 9.0 but is available on devices running earlier
// version of iOS. We ignore the partial-availability warning that gets thrown on our use of this
// symbol.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wpartial-availability"
- (void)mdf_setSemanticContentAttribute:(UISemanticContentAttribute)semanticContentAttribute {
if (semanticContentAttribute == self.mdf_semanticContentAttribute) {
return;
}
[super mdf_setSemanticContentAttribute:semanticContentAttribute];
_itemBar.mdf_semanticContentAttribute = semanticContentAttribute;
}
#pragma clang diagnostic pop
#pragma mark - MDCAccessibility
- (id)accessibilityElementForItem:(UITabBarItem *)item {
return [_itemBar accessibilityElementForItem:item];
}
#pragma mark - MDCItemBarDelegate
- (void)itemBar:(__unused MDCItemBar *)itemBar didSelectItem:(UITabBarItem *)item {
id<MDCTabBarDelegate> delegate = self.delegate;
if ([delegate respondsToSelector:@selector(tabBar:didSelectItem:)]) {
[delegate tabBar:self didSelectItem:item];
}
}
- (BOOL)itemBar:(__unused MDCItemBar *)itemBar shouldSelectItem:(UITabBarItem *)item {
id<MDCTabBarDelegate> delegate = self.delegate;
BOOL shouldSelect = YES;
if ([delegate respondsToSelector:@selector(tabBar:shouldSelectItem:)]) {
shouldSelect = [delegate tabBar:self shouldSelectItem:item];
}
if (shouldSelect && [delegate respondsToSelector:@selector(tabBar:willSelectItem:)]) {
[delegate tabBar:self willSelectItem:item];
}
return shouldSelect;
}
#pragma mark - UIView
- (void)tintColorDidChange {
[super tintColorDidChange];
[self updateItemBarStyle];
}
- (CGSize)intrinsicContentSize {
return _itemBar.intrinsicContentSize;
}
- (CGSize)sizeThatFits:(CGSize)size {
return [_itemBar sizeThatFits:size];
}
- (void)didMoveToWindow {
[super didMoveToWindow];
// Ensure the bar position is up to date before moving to a window.
[self updateItemBarPosition];
}
#pragma mark - Private
+ (MDCItemBarStyle *)defaultStyleForPosition:(UIBarPosition)position
itemAppearance:(MDCTabBarItemAppearance)appearance {
MDCItemBarStyle *style = [[MDCItemBarStyle alloc] init];
// Set base style using position.
if ([self isTopTabsForPosition:position]) {
// Top tabs
style.shouldDisplaySelectionIndicator = YES;
style.shouldGrowOnSelection = NO;
style.inkStyle = MDCInkStyleBounded;
style.rippleStyle = MDCRippleStyleBounded;
style.titleImagePadding = (kImageTitleSpecPadding + kImageTitlePaddingAdjustment);
style.textOnlyNumberOfLines = 2;
} else {
// Bottom navigation
style.shouldDisplaySelectionIndicator = NO;
style.shouldGrowOnSelection = YES;
style.maximumItemWidth = kBottomNavigationMaximumItemWidth;
style.inkStyle = MDCInkStyleUnbounded;
style.rippleStyle = MDCRippleStyleUnbounded;
style.titleImagePadding = kBottomNavigationTitleImagePadding;
style.textOnlyNumberOfLines = 1;
}
// Update appearance-dependent style properties.
BOOL displayImage = NO;
BOOL displayTitle = NO;
switch (appearance) {
case MDCTabBarItemAppearanceImages:
displayImage = YES;
break;
case MDCTabBarItemAppearanceTitles:
displayTitle = YES;
break;
case MDCTabBarItemAppearanceTitledImages:
displayImage = YES;
displayTitle = YES;
break;
default:
NSAssert(0, @"Invalid appearance value %ld", (long)appearance);
displayTitle = YES;
break;
}
style.shouldDisplayImage = displayImage;
style.shouldDisplayTitle = displayTitle;
// Update default height
CGFloat defaultHeight = [self defaultHeightForBarPosition:position itemAppearance:appearance];
if (defaultHeight == 0) {
NSAssert(0, @"Missing default height for %ld", (long)appearance);
defaultHeight = kTitleOnlyBarHeight;
}
style.defaultHeight = defaultHeight;
// Only show badge with images.
style.shouldDisplayBadge = displayImage;
return style;
}
+ (BOOL)isTopTabsForPosition:(UIBarPosition)position {
switch (position) {
case UIBarPositionAny:
case UIBarPositionTop:
return YES;
case UIBarPositionBottom:
return NO;
case UIBarPositionTopAttached:
NSAssert(NO, @"MDCTabBar does not support UIBarPositionTopAttached");
return NO;
}
}
+ (BOOL)displaysUppercaseTitlesByDefaultForPosition:(UIBarPosition)position {
switch (position) {
case UIBarPositionAny:
case UIBarPositionTop:
return YES;
case UIBarPositionBottom:
return NO;
case UIBarPositionTopAttached:
NSAssert(NO, @"MDCTabBar does not support UIBarPositionTopAttached");
return YES;
}
}
+ (MDCTabBarAlignment)defaultAlignmentForPosition:(UIBarPosition)position {
switch (position) {
case UIBarPositionAny:
case UIBarPositionTop:
return MDCTabBarAlignmentLeading;
case UIBarPositionBottom:
return MDCTabBarAlignmentJustified;
case UIBarPositionTopAttached:
NSAssert(NO, @"MDCTabBar does not support UIBarPositionTopAttached");
return MDCTabBarAlignmentLeading;
}
}
+ (MDCTabBarItemAppearance)defaultItemAppearanceForPosition:(UIBarPosition)position {
switch (position) {
case UIBarPositionAny:
case UIBarPositionTop:
return MDCTabBarItemAppearanceTitles;
case UIBarPositionBottom:
return MDCTabBarItemAppearanceTitledImages;
case UIBarPositionTopAttached:
NSAssert(NO, @"MDCTabBar does not support UIBarPositionTopAttached");
return YES;
}
}
+ (id<MDCTabBarIndicatorTemplate>)defaultSelectionIndicatorTemplate {
return [[MDCTabBarUnderlineIndicatorTemplate alloc] init];
}
- (MDCTabBarAlignment)computedAlignment {
if (_hasDefaultAlignment) {
return [[self class] defaultAlignmentForPosition:_barPosition];
} else {
return _alignmentOverride;
}
}
- (MDCTabBarItemAppearance)computedItemAppearance {
if (_hasDefaultItemAppearance) {
return [[self class] defaultItemAppearanceForPosition:_barPosition];
} else {
return _itemAppearanceOverride;
}
}
- (void)internalSetAlignment:(MDCTabBarAlignment)alignment animated:(BOOL)animated {
if (_alignment != alignment) {
_alignment = alignment;
[_itemBar
setAlignment:MDCItemBarAlignmentForTabBarAlignment((MDCTabBarExtendedAlignment)_alignment)
animated:animated];
}
}
- (void)internalSetItemAppearance:(MDCTabBarItemAppearance)itemAppearance {
if (_itemAppearance != itemAppearance) {
_itemAppearance = itemAppearance;
[self updateItemBarStyle];
}
}
- (void)updateItemBarPosition {
UIBarPosition newPosition = UIBarPositionAny;
id<MDCTabBarDelegate> delegate = _delegate;
if (delegate && [delegate respondsToSelector:@selector(positionForBar:)]) {
newPosition = [delegate positionForBar:self];
}
if (_barPosition != newPosition) {
_barPosition = newPosition;
[self updatePositionDerivedDefaultValues];
[self updateItemBarStyle];
}
}
- (void)updatePositionDerivedDefaultValues {
[self internalSetAlignment:[self computedAlignment] animated:NO];
[self internalSetItemAppearance:[self computedItemAppearance]];
}
/// Update the item bar's style property, which depends on the bar position and item appearance.
- (void)updateItemBarStyle {
MDCItemBarStyle *style;
style = [[self class] defaultStyleForPosition:_barPosition itemAppearance:_itemAppearance];
if ([MDCTabBar isTopTabsForPosition:_barPosition]) {
// Top tabs: Use provided fonts.
style.selectedTitleFont = self.selectedItemTitleFont;
style.unselectedTitleFont = self.unselectedItemTitleFont;
} else {
// Bottom navigation: Ignore provided fonts.
style.selectedTitleFont = [[MDCTypography fontLoader] regularFontOfSize:12];
style.unselectedTitleFont = [[MDCTypography fontLoader] regularFontOfSize:12];
}
style.selectionIndicatorTemplate = self.selectionIndicatorTemplate;
style.selectionIndicatorColor = self.tintColor;
style.inkColor = _inkColor;
style.rippleColor = _rippleColor;
style.enableRippleBehavior = _enableRippleBehavior;
style.displaysUppercaseTitles = self.displaysUppercaseTitles;
style.selectedTitleColor = _selectedTitleColor ?: self.tintColor;
style.titleColor = _unselectedTitleColor;
style.selectedImageTintColor = _selectedItemTintColor ?: self.tintColor;
style.imageTintColor = _unselectedItemTintColor;
[_itemBar applyStyle:style];
// Layout depends on -[MDCItemBar sizeThatFits], which depends on the style.
[self setNeedsLayout];
}
- (void)setEnableRippleBehavior:(BOOL)enableRippleBehavior {
if (_enableRippleBehavior == enableRippleBehavior) {
return;
}
_enableRippleBehavior = enableRippleBehavior;
[self updateItemBarStyle];
}
@end