如何在AppDelegate中触发navigationController:willShowViewController委托方法

发布于 2024-12-06 04:42:45 字数 978 浏览 0 评论 0原文

如何为下面的实现触发 navigationController:willShowViewController 委托方法,以便导航控制器中的所有视图控制器都符合 colorWithHexString #faf6f5?

目前,我的 FirstViewController 将被显示,但它似乎不会调用委托方法来更改其导航栏的颜色(以及随后堆叠到导航控制器上的所有其他视图控制器)。请注意,我已经将“UINavigationControllerDelegate”添加到我的应用程序委托头文件中。

//In App Delegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //Set First View
    FirstViewController *firstView = [[FirstViewController alloc]init];

    // pushes a nav con 
    UINavigationController *tempNavcon = [[UINavigationController alloc]initWithRootViewController:firstView];
    self.navcon = tempNavcon;

    [self.window addSubview:navcon.view];

}

- (void)navigationController:(UINavigationController *)navigationController 
  willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{

    navigationController.navigationBar.tintColor = [UIColor colorWithHexString:@"#faf6f5"];

}

How can I trigger the navigationController:willShowViewController delegate method for my implementation below so that all the view controllers in the navigation controller will conform to the colorWithHexString #faf6f5?

Currently, my FirstViewController will be displayed but it doesn't seem to call the delegate method to change the color of it's navigation bar (as well as for all other view controllers that are stacked onto the navigation controller subsequently). Note that I have already added the "UINavigationControllerDelegate" to my app delegate header file.

//In App Delegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //Set First View
    FirstViewController *firstView = [[FirstViewController alloc]init];

    // pushes a nav con 
    UINavigationController *tempNavcon = [[UINavigationController alloc]initWithRootViewController:firstView];
    self.navcon = tempNavcon;

    [self.window addSubview:navcon.view];

}

- (void)navigationController:(UINavigationController *)navigationController 
  willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{

    navigationController.navigationBar.tintColor = [UIColor colorWithHexString:@"#faf6f5"];

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

趁微风不噪 2024-12-13 04:42:45

您尝试在事件方法中而不是在创建 UINavigationBar 实例时更改tintColor 是否有原因?

is there a reason why you are trying to change the tintColor in an event method rather than when the UINavigationBar instance is created?

猫弦 2024-12-13 04:42:45

操作方法如下。 (请注意,UIColor 不接受十六进制值;您应该使用 RGB 值,或检查 此页

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //Initialize your view controller.
    FirstViewController * firstView = [[FirstViewController alloc] init];

    // Create an instance of a UINavigationController. Its stack contains only firstView.
    UINavigationController *navController = [[UINavigationController alloc]
                                             initWithRootViewController:firstView];

    //Here is where you set the color of the navigationBar. See my note above for using RGB.
    navController.navigationBar.tintColor = [UIColor greenColor];

    // You can now release the firstView here, navController will retain it
    [firstView release];

    // Place navigation controller's view in the window hierarchy
    [[self window] setRootViewController:navController];

    [navController release];

    [self.window makeKeyAndVisible];
    return YES;
}

Here's how you do it. (Note that UIColor doesn't accept hex values; you should use an RGB value, or check this page.

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //Initialize your view controller.
    FirstViewController * firstView = [[FirstViewController alloc] init];

    // Create an instance of a UINavigationController. Its stack contains only firstView.
    UINavigationController *navController = [[UINavigationController alloc]
                                             initWithRootViewController:firstView];

    //Here is where you set the color of the navigationBar. See my note above for using RGB.
    navController.navigationBar.tintColor = [UIColor greenColor];

    // You can now release the firstView here, navController will retain it
    [firstView release];

    // Place navigation controller's view in the window hierarchy
    [[self window] setRootViewController:navController];

    [navController release];

    [self.window makeKeyAndVisible];
    return YES;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文