iOS修改UITabbar默認(rèn)點(diǎn)擊效果、文字圖片樣式

主頁面

看到上面需求,第一反應(yīng)是做成類似新浪自定義tabbar 按鈕形式,評估了一下工期,時(shí)間太緊,就想著用原生的Tabbar 能否實(shí)現(xiàn)功能。首先遇到的最大問題就是,如何阻斷tabbar 按鈕的點(diǎn)擊事件,讓其點(diǎn)擊的時(shí)候不顯示原定頁面,而是執(zhí)行我的自定義事件。

感謝前輩的分享,網(wǎng)上找到了對應(yīng)的解決方案
點(diǎn)擊了UITabBar的按鈕之后,UITabBarController會(huì)執(zhí)行UITabBarControllerDelegate的以下方法,并將要跳轉(zhuǎn)到的UIViewController作為參數(shù)傳遞給一下代理方法。如果代理方法返回YES那么UITabBarController就會(huì)正常跳轉(zhuǎn),如果返回NO,那么就不會(huì)做跳轉(zhuǎn)動(dòng)作。方法如下:

- (BOOL)tabBarController:(UITabBarController*)tabBarController shouldSelectViewController:(UIViewController*)viewController

然后我們來看具體實(shí)現(xiàn)

// tabbar 點(diǎn)擊之后 -(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController: (UIViewController *)viewController{ // 記錄當(dāng)前被點(diǎn)擊tabbar _currentIndex = tabBarController.selectedIndex; } // tabbar 是否點(diǎn)擊 -(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{ // 判斷是否點(diǎn)擊中間 (加號(hào))tabbar if ([viewController.childViewControllers[0] isKindOfClass:[SendViewController class]]) { // 點(diǎn)擊之后發(fā)送通知,做相應(yīng)處理 [[NSNotificationCenter defaultCenter]postNotificationName:@"sendVCSelected" object:[NSString stringWithFormat:@"%ld",(long)_currentIndex]]; return NO; } return YES; }

到這tabar 點(diǎn)擊事件問題就已經(jīng)解決了,下面來看下如何修改圖標(biāo)位置,設(shè)置正常與點(diǎn)擊后圖片效果、修改字體顏色等
修改按鈕圖片位置

tabItem.imageInsets = imageInsets;

修改前


修改前

修改后


修改后

設(shè)置正常與選中圖片,取消系統(tǒng)圖片渲染效果
圖片不被渲染

UIImage * selectedImage = [UIImage imageNamed:selectImageStr]; UIImage * deselectImage = [UIImage imageNamed:deselectImageStr]; // 設(shè)置圖片不被渲染 selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; deselectImage = [deselectImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

設(shè)置tabbar 選中與未選中圖片

UITabBarItem * tabItem = [[UITabBarItem alloc] initWithTitle:title image:deselectImage selectedImage:selectedImage];

設(shè)置消息提示

tabItem.badgeValue = @"1";

統(tǒng)一設(shè)置tabbar 文字(選中后效果,其他狀態(tài)下只需要修改UIControlStateSelected 即可)

設(shè)置tabbar 字體顏色、大小等(同理可設(shè)置navigationbar, 都是在bar) [[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor blackColor], NSFontAttributeName:[UIFont fontWithName:@"Marion-Italic" size:14.0] } forState:UIControlStateSelected];

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容