iOS 在TabViewController中設定Tab Bar,且實現自訂

來源:互聯網
上載者:User

標籤:style   blog   http   io   color   ar   os   使用   sp   

開發環境:Xcode 6.1 iOS 7.1.2 & iOS 8.1

實現功能一,在開啟APP時即顯示第三個Tab對應的View;

實現功能二,給每一個Tab設定圖片和選中圖片。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    // Override point for customization after application launch.        //---------------------------------------------    // set the first view as camera view and custom tab bar    //---------------------------------------------    [self setTabBar];}

實現setTab Bar:
//---------------------------------------------#pragma mark - Tab Bar//---------------------------------------------- (void) setTabBar{    UITabBarController *tabViewController = (UITabBarController *) self.window.rootViewController;    [tabViewController setSelectedIndex:2];    UITabBar *tabBar = tabViewController.tabBar;    [tabBar setTintColor: [UIColor greenColor]];        // First    UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];    UIImage* tabBarItem1Image = [UIImage imageNamed:@"XXX"];    UIImage *scaledTabBarItem1Image = [UIImage imageWithCGImage:[tabBarItem1Image CGImage] scale:(tabBarItem1Image.scale * 3) orientation:(tabBarItem1Image.imageOrientation)];    UIImage* tabBarItem1SelectedImage = [UIImage imageNamed:@"XXX"];    UIImage *scaledTabBarItem1SelectedImage = [UIImage imageWithCGImage:[tabBarItem1SelectedImage CGImage] scale:(tabBarItem1SelectedImage.scale * 3) orientation:(tabBarItem1SelectedImage.imageOrientation)];    (void)[tabBarItem1 initWithTitle:@"First" image:scaledTabBarItem1Image selectedImage:scaledTabBarItem1SelectedImage];        // Second    UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];    UIImage* tabBarItem2Image = [UIImage imageNamed:@"XXX"];    UIImage *scaledTabBarItem2Image = [UIImage imageWithCGImage:[tabBarItem2Image CGImage] scale:(tabBarItem2Image.scale * 3) orientation:(tabBarItem2Image.imageOrientation)];    UIImage* tabBarItem2SelectedImage = [UIImage imageNamed:@"XXX"];    UIImage *scaledTabBarItem2SelectedImage = [UIImage imageWithCGImage:[tabBarItem2SelectedImage CGImage] scale:(tabBarItem2SelectedImage.scale * 3) orientation:(tabBarItem2SelectedImage.imageOrientation)];    (void)[tabBarItem2 initWithTitle:@"Second" image:scaledTabBarItem2Image selectedImage:scaledTabBarItem2SelectedImage];        // And Others Tabs - 同樣的方法    }

在didFinishLaunchingWithOptions就自訂跳轉直接到某一個Tab:

    UITabBarController *tabViewController = (UITabBarController *) self.window.rootViewController;    [tabViewController setSelectedIndex:2];

為了保證像素,使表徵圖比較清晰,使用了大的圖片,然後用代碼將其縮小:

[UIImage imageWithCGImage:[tabBarItem1Image CGImage] scale:(tabBarItem1Image.scale * 3) orientation:(tabBarItem1Image.imageOrientation)];
另:

因為selected image系統總是會將圖片設定成和tint color一樣顏色的圖片,預設是藍色,所以需要自己設定你需要的顏色:

[tabBar setTintColor: [UIColor greenColor]];


參考連結:

http://stackoverflow.com/questions/18894985/uitabbar-not-showing-selected-item-images-in-ios-7

http://stackoverflow.com/a/19386156/3458781

http://www.appcoda.com/ios-programming-how-to-customize-tab-bar-background-appearance/

iOS 在TabViewController中設定Tab Bar,且實現自訂

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.