ios 用純代碼寫程序的時候,navigationController的導航欄的設置


  我們都知道,如果用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的跟試圖控制器


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com