關於iOS7-8的系統UITabBarController,uitabbarcontroller

來源:互聯網
上載者:User

關於iOS7-8的系統UITabBarController,uitabbarcontroller

眼看iOS9就要出了。最近做項目用的系統的UITabBarController,就mark一下遇到的問題。

系統預設的UITabBarController使用storyboard可以進行設定。

下面主要是講代碼設定tabbar的圖片與顏色。

1.tabbar的背景顏色或背景圖(灰色效果)

    tabBarController.tabBar.backgroundImage = [UIImage imageNamed:@"tabbar_unselected"];

這個圖片可以是一張寬度為2像素,高98的一個細線圖。

2.設定選中item的背景顏色或背景圖(,藍色效果)

    UIImage* selectedImage = [UIImage imageNamed:@"tabbar_select"];    selectedImage = [selectedImage resizableImageWithCapInsets:UIEdgeInsetsZero];    tabBarController.tabBar.selectionIndicatorImage = selectedImage;

這裡的圖片也可以跟上面的背景圖片一樣,可以是一張寬度為2像素,高98的一個細線圖。

selectedImage = [selectedImage resizableImageWithCapInsets:UIEdgeInsetsZero];

是為了用細線圖填充要設定的image

3.設定圖片顏色與字型顏色及字型大小

    NSMutableDictionary *textAttrs=[NSMutableDictionary dictionary];    textAttrs[NSForegroundColorAttributeName]=[UIColor whiteColor];    textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:12.f];

    MLNavigationController * oneController = [[MLNavigationController alloc] initWithRootViewController:VC1];
    oneController.tabBarItem.title = @"首頁";
    oneController.tabBarItem.image = [[UIImage imageNamed:@"tabbar_home"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    oneController.tabBarItem.selectedImage = [[UIImage imageNamed:@"tabbar_home"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [oneController.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];

我的需求是設定圖片與顏色在選中與未選中狀態下都為白色。

3.1字型顏色設定

字典textAttrs的key值可以雙擊去查看你要設定的選項來設定,這裡注意一點

- (void)setTitleTextAttributes:(NSDictionary *)attributes forState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

這個方法state只能設定UIControlStateNormal,UIControlStateSelected,UIControlStateDisabled三個type

3.2圖片顏色設定

圖片你會發現用了一個image的方法

- (UIImage *)imageWithRenderingMode:(UIImageRenderingMode)renderingMode NS_AVAILABLE_IOS(7_0);

這個方法是告訴系統取消渲染,使用原本的圖片顏色。

這樣你就可以用你提供的selectedImage與image進行設定了。

oneController.tabBarItem.image = [[UIImage imageNamed:@"tabbar_home"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
oneController.tabBarItem.selectedImage = [[UIImage imageNamed:@"tabbar_home"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.