iOS UI入門——Objective-C和Swift下UITabBarController的使用
阿新 • • 發佈:2018-12-14
很多App啟動頁過後展示的就是選項卡也就是UITabBarController,這裡講的是最簡單的選項卡的實現,直接繼承自UITabBarController。
Objective-C程式碼:
#import "MainTabBarViewController.h"
#import "HomeViewController.h"
#import "UserCenterViewController.h"
@interface MainTabBarViewController ()
@end
@implementation MainTabBarViewController
- (void )viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
[self setupControllers];
[self setupItems];
}
-(void)setupControllers{
UINavigationController * nc1 = [[UINavigationController alloc] initWithRootViewController:[[HomeViewController alloc] init]];
UINavigationController * nc2 = [[UINavigationController alloc] initWithRootViewController:[[UserCenterViewController alloc] init]];
self.viewControllers = @[nc1,nc2];
}
-(void)setupItems{
NSArray *titleArray = @[@"首頁",@"個人中心"];
NSArray *selectImageName [email protected][@"tabbar_home_selected",@"tabbar_mine_selected" ];
NSArray *unselectImageName [email protected][@"tabbar_home",@"tabbar_mine"];
for (int i =0; i < titleArray.count; i++) {
//需要對圖片進行單獨處理,防止在bar上顯示為陰影而不是圖片
UIImage *selectImage = [UIImage imageNamed:selectImageName[i]];
selectImage = [selectImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *unselectImage = [UIImage imageNamed:unselectImageName[i]];
unselectImage = [unselectImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UITabBarItem *item = self.tabBar.items[i];
item.selectedImage = selectImage;
item.image = unselectImage;
item.title = titleArray[i];
}
//設定tabbar背景圖
//[self.tabBar setBackgroundImage:[UIImage imageNamed:@"img_tabBar_bg_iOS8.png"]];
//設定tabbar按鈕被選中時字型的顏色
[[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor redColor]}forState:UIControlStateSelected];
//設定tabbar按鈕未被選中時字型的顏色
[[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor darkGrayColor]}forState:UIControlStateNormal];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
其中MainTabBarViewController繼承自UITabBarController,如果專案啟動後就顯示,在Appdelegate中實現如下程式碼即可:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window.rootViewController = [[MainTabBarViewController alloc] init];
return YES;
}
Swift程式碼:
import UIKit
class MainTabbarViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.view.backgroundColor = UIColor.white
self.setupControllees()
self.setupItems()
}
func setupControllees() {
let nc1 = UINavigationController.init(rootViewController: HomeViewController())
let nc2 = UINavigationController.init(rootViewController: UserCenterViewController())
self.viewControllers = [nc1,nc2]
}
func setupItems() {
let titleArray = ["首頁","個人中心"]
let selectImageName = ["tabbar_home_selected","tabbar_mine_selected"]
let unselectImageName = ["tabbar_home","tabbar_mine"]
for index in 0...titleArray.count-1 {
//需要對圖片進行單獨處理,防止在bar上顯示為陰影而不是圖片
var selectImage = UIImage.init(named: selectImageName[index])
selectImage = selectImage?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
var unselectImage = UIImage.init(named: unselectImageName[index])
unselectImage = unselectImage?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
//初始化items
let item = self.tabBar.items![index]
item.selectedImage = selectImage
item.image = unselectImage
item.title = titleArray[index]
}
//設定tabbar背景圖
//self.tabBar.backgroundImage = UIImage.init(named: "")
//設定tabbar按鈕被選中時字型的顏色
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor:UIColor.red], for: UIControlState.selected)
//設定tabbar按鈕未被選中時字型的顏色
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor:UIColor.darkGray], for: UIControlState.normal)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
其中MainTabBarViewController繼承自UITabBarController,如果專案啟動後就顯示,在Appdelegate中實現如下程式碼即可:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
self.window?.rootViewController = MainTabbarViewController()
return true
}
效果圖: