/ / ios tabBarControl, заглавие на навигационния контролер и редактиране на бутони - ios, uitabbarcontroller, uinavigationbar

ios tabBarControl, заглавие на навигационния контролер и редактиране на бутон - ios, uitabbarcontroller, uinavigationbar

Използвах стандартен TabBarController игоре NavigationBar в моето приложение. Разделът tabbar се зарежда в appDelegate, инициализира навигационния бар, за да използва изображение вместо заглавието в центъра и това съществува за всичките четири изгледа на раздела. Понякога ще натискам нов изглед към контролерите на изгледа и това работи достатъчно добре.

Това, което бих искал да направя, е да мога да променяNavigationBar за всеки от 4 контролера за изглед на раздела, когато те се отворят. Имам проблеми с това. Моят първоначален навигационен бар има изображение, заредено в appDelegate. Моите въпроси от тук са:

  • Трябва ли всеки viewController обикновено да създаде нов навигационен бар от нулата, според нуждите си, в съответния метод viewWillAppear?
  • Какво навигацияКонтролер и навигацияБартрябва да се редактира - винаги навигациятаController на appDelegate, раздела tabBarController.navigationController или просто self.navigationController при всеки изглед? (обикновено повечето редактиране не работят за мен, не предизвикват промени)
  • Ако отменя стандартното заглавие (което еобикновено заглавието на текущия изглед на tabBar) с imageView и искате друг изглед на раздела да използва стандартното заглавие, как трябва да премахна изображението titleView и да се върна обратно към текста? И viceVersa, зависи от гледката? Това наистина се опитвам да постигна безуспешно.

Предполагам, че търся стандартна практика в управлението на навигационния бар на изглед и да го променя на 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];
}

РЕДАКТИРАНЕ: Това е опростена версия на решениетопубликувано по-долу, което беше полезно и позволи персонализирането, което беше необходимо. Всеки контролер на изглед персонализира своята лента за навигация независимо от това. Вероятно е трябвало да се направи от самото начало.

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];

Отговори:

2 за отговор № 1

Това може би това, от което се нуждаете:

- (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:@"...."];
}