首页 > 代码库 > ios 用纯代码写程序的时候,navigationController的导航栏的设置

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的跟试图控制器

ios 用纯代码写程序的时候,navigationController的导航栏的设置