標籤:gen make tom main origin tabbar string item turn
我們都知道,如果用storyBoard設定導覽列很容易,點擊左右item的時候,進入下一個介面,導覽列的顏色是跟上一層的是一樣的,用純程式碼寫的時候,可以在當前控制器,和從當前控制器進入到下一個控制器都用代碼實現對導覽列的控制,但是,每次都寫代碼設定,很麻煩,所以,可以這樣:
建立一個MainTabBarController的類,在Appdelegate.m裡面完成:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.rootViewController = [[MainTabBarController alloc] init];
[self.window makeKeyAndVisible];
return YES;
}
然後,在MainTabBarController.m裡面
- (void)viewDidLoad {
[super viewDidLoad];
[self setUpAllChildVc];
}
- (void)setUpAllChildVc
{
//有幾個介面,設定幾個
MapViewController *HomeVC = [[HFMapViewController alloc] init];
[self setUpOneChildVcWithVc:HomeVC Image:@"地圖-首頁" selectedImage:@"地圖選中-首頁" title:@"地圖"];
AppointmentViewController *appointmentVC = [[HFAppointmentViewController alloc] init];
[self setUpOneChildVcWithVc:appointmentVC Image:@"預約-首頁" selectedImage:@"預約選中-首頁" title:@"預約"];
}
- (void)setUpOneChildVcWithVc:(UIViewController *)Vc Image:(NSString *)image selectedImage:(NSString *)selectedImage title:(NSString *)title
{
MainNavigationController *nav = [[MainNavigationController alloc] initWithRootViewController:Vc];
UIImage *myImage = [UIImage imageNamed:image];
myImage = [myImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//tabBarItem,是系統提供模型,專門負責tabbar上按鈕的文字以及圖片展示
Vc.tabBarItem.image = myImage;
UIImage *mySelectedImage = [UIImage imageNamed:selectedImage];
mySelectedImage = [mySelectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
Vc.tabBarItem.selectedImage = mySelectedImage;
Vc.tabBarItem.title = title;
[self addChildViewController:nav];
}
只要需要導覽列的,都可以把當前控制器設定為MainNavigationController的跟試圖控制器
ios 用純程式碼寫程式的時候,navigationController的導覽列的設定