1. 屬性translucent
- UINavigationController -> UINavigationBar -> translucent
- UITabBarController -> UITabBar -> translucent
A Boolean value indicating whether the navigation bar is translucent (YES) or not (NO).
一個布爾值,指示導(dǎo)航欄是否為半透明(是)或非半透明(否)。
默認(rèn)為YES,自動布局時,導(dǎo)航欄、標(biāo)簽欄遮擋subvc.view視圖
UINavigationController *navVC;
nav.navigationBar.translucent = NO;
UITabBarController *tabBarVC;
MyTabBar *tabbar = [MyTabBar new];
tabbar.tintColor = [UIColor redColor];// 設(shè)置tabBarItem選中狀態(tài)時的顏色
[tabBarVC setValue:tabbar forKey:@"tabBar"];// 利用KVO來使用自定義的tabBar
tabBarVC.tabBar.translucent = NO; // KVO設(shè)置后,設(shè)置改屬性為NO
2. 自定義UITabBar
通過自定義UITabBar,重寫layoutSubviews方法,修改UI布局,可以實現(xiàn)想要的功能。
當(dāng)設(shè)置標(biāo)簽欄子控制器的title時,會觸發(fā)UITabBar的layoutSubviews方法,此時subiews中UITabBarButton順序變動,不能按獲取數(shù)組索引操作UITabBarButton的frame,否側(cè)會導(dǎo)致標(biāo)簽欄控制器錯序,所以必須以frame.origin.x升序排序后操作。
// 重新布局tabBarItem(這里需要具體情況具體分析,本例是中間有個按鈕,兩邊平均分配按鈕)
- (void)layoutSubviews
{
[super layoutSubviews];
// 把tabBarButton取出來(把tabBar的SubViews打印出來就明白了)
NSMutableArray *tabBarButtonArray = [NSMutableArray array];
for (UIView *view in self.subviews) {
if ([view isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
[tabBarButtonArray addObject:view];
}
}
/**
因為標(biāo)簽欄tabbar layoutSubviews 后更新某個標(biāo)簽欄子控制器的title時,會更新該標(biāo)簽欄對應(yīng)位置的標(biāo)題,
即會觸發(fā)layoutSubviews,并且最新更新title的子控制器在數(shù)組的低索引位置;
而下列按數(shù)組順序更新frame,會導(dǎo)致錯亂;
所以必須以fram.x 升序排序后再更新frame。
控制器vc的標(biāo)題:
@property(nullable, nonatomic,copy) NSString *title;
@Summary:A localized string that represents the view this controller manages.
@Discussion:Set the title to a human-readable string that describes the view.
【重點說明】If the view controller has a valid navigation item or tab-bar item,
assigning a value to this property updates the title text of those objects.
*/
NSArray *sortOriginXArr = [tabBarButtonArray sortedArrayUsingComparator:^NSComparisonResult(UIView * _Nonnull obj1, UIView * _Nonnull obj2) {
return obj1.frame.origin.x < obj2.frame.origin.x ? NSOrderedAscending : NSOrderedDescending;
}];
CGFloat barWidth = self.bounds.size.width;
CGFloat barHeight = self.bounds.size.height;
CGFloat centerBtnWidth = CGRectGetWidth(self.centerBtn.frame);
CGFloat centerBtnHeight = CGRectGetHeight(self.centerBtn.frame);
// 設(shè)置中間按鈕的位置,居中,凸起一丟丟
self.centerBtn.center = CGPointMake(barWidth / 2, barHeight - centerBtnHeight/2 - 5);
// 重新布局其他tabBarItem
// 平均分配其他tabBarItem的寬度
CGFloat barItemWidth = (barWidth - centerBtnWidth) / sortOriginXArr.count;
// 逐個布局tabBarItem,修改UITabBarButton的frame
[sortOriginXArr enumerateObjectsUsingBlock:^(UIView * _Nonnull view, NSUInteger idx, BOOL * _Nonnull stop) {
CGRect frame = view.frame;
if (idx >= sortOriginXArr.count / 2) {
// 重新設(shè)置x坐標(biāo),如果排在中間按鈕的右邊需要加上中間按鈕的寬度
frame.origin.x = idx * barItemWidth + centerBtnWidth;
} else {
frame.origin.x = idx * barItemWidth;
}
// 重新設(shè)置寬度
frame.size.width = barItemWidth;
view.frame = frame;
}];
// 把中間按鈕帶到視圖最前面
[self bringSubviewToFront:self.centerBtn];
}