很多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 =@[@"tabbar_home_selected",@"tabbar_mine_selected"];
NSArray *unselectImageName =@[@"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
}
效果图: