gpt4 book ai didi

ios tabBarControl、navigationcontroller 标题和按钮编辑

转载 作者:行者123 更新时间:2023-11-29 10:43:07 24 4
gpt4 key购买 nike

我一直在我的应用程序中使用标准的 TabBarController 和顶部 NavigationBar。 tabBar 在 appDelegate 中加载,初始化 navigationBar 以使用图像而不是中心标题,这对于所有四个 tabBar View 都存在。有时我会将一个新 View 推送到 View Controller 上,并且效果很好。

我想要做的是能够在 4 个 tabBar View Controller 打开时更改它们中的每一个的 NavigationBar。我在做这件事时遇到了麻烦。我的初始 navigationBar 有一个图像,加载在 appDelegate 中。我的问题是:

  • 每个 viewController 通常应该根据需要在各自的 viewWillAppear 方法中从头开始创建一个新的 navigationBar 吗?
  • 应该编辑什么 navigationController 和 navigationBar——始终是 appDelegate 的 navigationController、tabBarController.navigationController,还是每个 View 中的 self.navigationController? (通常,大多数编辑对我来说都不起作用,不会导致任何更改)
  • 如果我用 imageView 覆盖标准标题(通常是 tabBar 当前 View 的标题),并希望另一个 tabBar View 使用标准标题,我应该如何删除 titleView 图像并重置回文本?反之亦然,取决于 View ?这就是我真正想要完成但未成功的事情。

我想我正在寻找管理每个 View 的导航栏并根据 tabBarItem 更改它的标准做法。

// in appDelegate
- (void)initNav {
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.delegate = self;
self.tabBarController.navigationController.view.backgroundColor = [UIColor whiteColor];

self.tabBarController.viewControllers = [NSArray arrayWithObjects:self.firstViewController,
self.secondViewController,
self.thirdViewController,
self.forthViewController,
nil];


[self.window addSubview:self.tabBarController.view];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.tabBarController];
self.navigationController.navigationBarHidden = NO;

// customize background color of nav bar to "blue"ish color
self.navigationController.navigationBar.backgroundColor = [UIColor colorWithHexString:@"00A3E1"];
self.navigationController.navigationBar.tintColor = [UIColor colorWithHexString:@"00A3E1"];
self.navigationController.navigationBar.barTintColor = [UIColor colorWithHexString:@"00A3E1"];

[self createNavs];
}

// also in appDelegate
- (void)createNavs {
// white text when present
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor],
UITextAttributeTextColor,
[UIColor clearColor],
UITextAttributeTextShadowColor, nil];

[[UIBarButtonItem appearance] setTitleTextAttributes: attributes
forState: UIControlStateNormal];


AppDelegate *delegateRef = (AppDelegate*)[[UIApplication sharedApplication] delegate];

[self.navigationController.navigationBar setTranslucent:NO];

// setup left button (currently unused -- no left button)
/*
/*
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"glyphicons_049_star.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(starClick:) forControlEvents:UIControlEventTouchDown];
[button setFrame:CGRectMake(0, 0, 25, 25)];
self.navigationController.navigationBar.topItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
*/

// setup logo in center
self.tabBarController.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"synced_top_logo.png"]];

// setup right button (currently unused -- no right button)
/*
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] init];
rightButton.title = @"edit";
[rightButton setTarget:self ];
[rightButton setAction:@selector(editClick:)];
self.navigationController.navigationBar.topItem.rightBarButtonItem = rightButton;
*/

[[self navigationController] setNavigationBarHidden:NO animated:YES];
[[delegateRef navigationController] setNavigationBarHidden:NO animated:YES];
}

编辑:这是下面发布的解决方案的简化版本,很有帮助,并允许进行所需的自定义。每个 View Controller 独立于此自定义其导航栏。可能从一开始就应该这样做。

tabBarController.viewControllers = [NSArray arrayWithObjects:[[UINavigationController alloc] initWithRootViewController:self.firstViewController], 
[[UINavigationController alloc] initWithRootViewController:self.secondViewController],
[[UINavigationController alloc] initWithRootViewController:self.thirdViewController],
[[UINavigationController alloc] initWithRootViewController:self.forthViewController],
nil];

最佳答案

这可能是您需要的:

- (void)initTabbar {

self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.delegate = self;

/*
You want each of your UIViewControllers to be wrapped in a UINavigationController. Then put each of those UINavigationControllers in a UITabBarController
*/

//You don't need to hang on to this becuase the proceeding UINavigationController will handle it
FirstViewController *firstViewController = [[FirstViewController alloc] ...];

//You'll need to declare this in your header
self.firstNavigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController];


//Second view allocation
SecondViewController *secondViewController = [[SecondViewController alloc] ...];
self.secondNavigationController = [[UINavigationController alloc] initWithRootViewController:secondViewController];

//Third view allocation
ThirdViewController *thirdViewController = [[ThirdViewController alloc] ...];
self.thirdNavigationController = [[UINavigationController alloc] initWithRootViewController:thirdViewController];


//Now you add each of the UINavigationControllers (which is a subclass of UIViewController) to the UITabBarController.
self.tabBarController.viewControllers = [NSArray arrayWithObjects:self.firstNavigationController,
self.secondNavigationController,
self.thirdNavigationController,
nil];


[self.window addSubview:self.tabBarController.view];

[self createNavs];
}

//This is more of a 'formatNavs' now
- (void)createNavs {

//Now you can customize each of the UINavigationController's UINavigationBars seperatly
self.firstNavigationController.navigationBar.backgroundColor = [UIColor colorWithHexString:@"00A3E1"];
self.firstNavigationController.navigationBar.tintColor = [UIColor colorWithHexString:@"00A3E1"];
self.firstNavigationController.navigationBar.barTintColor = [UIColor colorWithHexString:@"00A3E1"];

self.secondNavigationController.navigationBar.backgroundColor = [UIColor colorWithHexString:@"...."];
self.secondNavigationController.navigationBar.tintColor = [UIColor colorWithHexString:@"...."];
self.secondNavigationController.navigationBar.barTintColor = [UIColor colorWithHexString:@"...."];

self.thirdNavigationController.navigationBar.backgroundColor = [UIColor colorWithHexString:@"...."];
self.thirdNavigationController.navigationBar.tintColor = [UIColor colorWithHexString:@"...."];
self.thirdNavigationController.navigationBar.barTintColor = [UIColor colorWithHexString:@"...."];
}

关于ios tabBarControl、navigationcontroller 标题和按钮编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23502497/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com