ios tabBarControl, navigationcontroller title and button editing - ios

I've been using a standard TabBarController and top NavigationBar within my app. The tabBar loads in the appDelegate, initializes the navigationBar to use an image instead of center title, and this exists for all four tabBar views. Occasionally I'll push a new view onto the view controllers and that works well enough.
What I'd like to do is be able to change the NavigationBar for each of the 4 tabBar view controllers, when they are opened. I'm having trouble doing this. My initial navigationBar has an image, loaded in the appDelegate. My questions from here are:
Should each viewController generally create a new navigationBar from scratch, per its needs, in the respective viewWillAppear method?
What navigationController and navigationBar should be editted-- always the appDelegate's navigationController, the tabBarController.navigationController, or simply self.navigationController in each view? (generally, most editing most of these is not working for me, causes no changes to occur)
If I override the standard title (which is usually the tabBar's current view's title) with the imageView, and want another tabBar view to use the standard title, how should I remove the titleView image, and reset back to text? And viceVersa, dependent on view? This is what I'm really trying to accomplish unsuccessfully.
I guess I'm looking for standard practice in managing the navigationBar per view, and changing it per 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];
}
EDIT:
This is a simplified version of the solution posted below that was helpful, and allowed the customization that was needed. Each view controller customizations its navigationbar independently from this. It was probably how it should have been done from the start.
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];

This maybe what you are needing:
- (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:#"...."];
}

Related

iOS: TabBar controller above UIView

I am having a problem with my tab bar placed above my view
I am using iOS 8 and Autolayout
I have my main view which has a button that opens another view that has the tab bar:
-(IBAction)btnOpen:(id)sender
{
Home* homevc = [[Home alloc] initWithNibName:#"Home_iPhone" bundle:nil];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:homevc];
[nc.tabBarItem setTitle:#"Home"];
[nc.tabBarItem setImage:[UIImage imageNamed:#"home_icon.png"]];
Search *searchvc = [[Search alloc] initWithNibName:#"Search_iPhone" bundle:nil];
[searchvc.tabBarItem setTitle:#"Search"];
[searchvc.tabBarItem setImage:[UIImage imageNamed:#"search_icon.png"]];
UITabBarController *tabController = [[UITabBarController alloc] init];
[tabController setViewControllers:[NSArray arrayWithObjects:nc,searchvc, nil]];
UIColor * color = [UIColor colorWithRed:191/255.0f green:50/255.0f blue:46/255.0f alpha:1.0f];
[[UITabBar appearance] setTintColor:color];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:color, NSForegroundColorAttributeName, nil]
forState:UIControlStateSelected];
[self presentViewController:tabController animated:YES completion:nil];
}
How can I place the Home view above the tab bar?

How to add UIBarButton Programmatically to Programmatically created NavBar?

I am changing my view controller using the following code:
[self.slidingViewController performSegueWithIdentifier:#"FindScoresController" sender:self.slidingViewController];
The segue is PUSH
Now I want to add UINavigationBar Programatically. The code i am using for it is:
// Not required if viewController is pushed from previous viewController
UINavigationBar *navbar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
UIColor *barColor = [[UIColor alloc]initWithRed:82/255.0f green:167/255.0f blue:192/255.0f alpha:1.0f];
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
// iOS 6.1 or earlier
navbar.tintColor = barColor;
} else {
// iOS 7.0 or later
navbar.barTintColor = barColor;
navbar.translucent = NO;
}
[self.view addSubview:navbar];
It adds nav bar successfully.
Now I want to add left and right custom buttons with custom Image.
But I am unable to do so.
Code i am using to add right UIBarButton item:
UIBarButtonItem *flipButton = [[UIBarButtonItem alloc]
initWithTitle:#"Flip"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(flipView:)];
self.navigationItem.rightBarButtonItem = flipButton;
But it doesn't show any effect.
I also tried this code:
UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourimage2.jpg"]]];
self.navigationItem.rightBarButtonItem = item;
All these code i am writing in ViewDidLoad method.

UIDatePicker Moving up tableview content

I have a UIViewController with a Navigation bar and also a UITableView in it. I have button in the view controller that when it is pressed pops up a UIDatePicker. However, When i press the button to pop up the UIDatePicker, all my content in the tableview moves up about 30 pixels. Not cool. I want the UIDatePicker to just popup overtop the view controller without shifting any elements. Here is the code of my date picker. Thoughts?
- (IBAction)timeWasPressed:(id)sender {
if(self.pickerShown == NO){
self.slidePicker = [[UIDatePicker alloc] init];
self.slidePicker.datePickerMode = UIDatePickerModeTime;
self.slidePicker.backgroundColor = [UIColor whiteColor];
[self.navigationController setNavigationBarHidden:YES];
[self.view addSubview:self.slidePicker];
//make done button
self.toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,64)];
self.toolBar.barTintColor = [UIColor whiteColor];
UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStylePlain target:self
action:#selector(doneButton)];
self.toolBar.items = [[NSArray alloc] initWithObjects:barButtonDone,nil];
barButtonDone.tintColor=[UIColor blackColor];
self.pickerShown = YES;
[self.view addSubview:self.toolBar];
}
}
-(void)doneButton{
NSDate *pickerDate = [self.slidePicker date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterNoStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *selectionString = [[NSString alloc] initWithFormat:#"%#", [dateFormatter stringFromDate:pickerDate]]; //
[self.timeButton setTitle:selectionString forState:UIControlStateNormal];
self.pickerShown = NO;
[self.slidePicker removeFromSuperview];
[self.toolBar removeFromSuperview];
[self.navigationController setNavigationBarHidden:NO];
}
What jason said is right, but he forgot to say you'll have to erase the line
[self.navigationController setNavigationBarHidden:YES];
And so, you'll have to replace
//....
[self.view addSubview:self.slidePicker];
//...
[self.view addSubview:self.toolBar];
//...
with
//...
[self.navigationController.view addSubview:self.slidePicker];
//...
[self.navigationController.view addSubview:self.toolBar];
//...
So your "timeWasPressed:" code will be:
self.slidePicker = [[UIDatePicker alloc] init];
self.slidePicker.datePickerMode = UIDatePickerModeTime;
self.slidePicker.backgroundColor = [UIColor whiteColor];
//[self.navigationController setNavigationBarHidden:YES];
[self.navigationController.view addSubview:self.slidePicker];
//make done button
self.toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,64)];
self.toolBar.barTintColor = [UIColor whiteColor];
UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStylePlain target:self
action:#selector(doneButton)];
self.toolBar.items = [[NSArray alloc] initWithObjects:barButtonDone,nil];
barButtonDone.tintColor=[UIColor blackColor];
self.pickerShown = YES;
[self.navigationController.view addSubview:self.toolBar];
Don't forget you can also remove the
[self.navigationController setNavigationBarHidden:NO];
from your doneButton method.
Hope that helps.
I think the reason why everything shifts up is because you are hiding the navigation bar.
[self.navigationController setNavigationBarHidden:YES];
Replace that line with the following and if you see everything animating up then this is the reason why everything is shifting.
[self.navigationController setNavigationBarHidden:true animated:true];
If you want the date picker to show over the navigation controller then instead of adding it to the view controller's view, add the picker to the navigation controller's view instead.
So.. replace
[self.view addSubview:self.slidePicker];
with..
[self.navigationController.view addSubview:self.slidePicker];
Hope it works out.

UINavigationBar becomes black

First of all, when i call presentViewController:navigationController it appears with normal green color navigation bar. But after animation is finished navigation bar becomes a little darker.
if(!userPageViewController)
userPageViewController = [[UserPageViewController alloc]initWithUser:tempUser];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:userPageViewController];
[self presentViewController:navigationController animated:YES completion:nil];
When i press the home button, navigation bar becomes black like on this image.
https://dl.dropboxusercontent.com/u/14066789/2013-12-04%2012.27.45.png
Top bar in xib file is set to Translucent Navigation Bar
Why it becomes black?
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Личный кабинет";
[myTableView setBackgroundColor:[UIColor clearColor]];
UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithTitle:#"Закрыть" style:UIBarButtonItemStyleBordered target:self action:#selector(backPressed:)];
self.navigationItem.leftBarButtonItem = btn;
self.navigationItem.backBarButtonItem = nil;
UIBarButtonItem *exitBtn = [[UIBarButtonItem alloc] initWithTitle:#"Выход" style:UIBarButtonItemStyleBordered target:self action:#selector(logOut:)];
self.navigationItem.rightBarButtonItem = exitBtn;
scroll.contentSize = CGSizeMake(320.0f, 400.0f);
if ([self respondsToSelector:#selector(edgesForExtendedLayout)])
self.edgesForExtendedLayout = UIRectEdgeNone;
}
-(void)viewWillAppear:(BOOL)animated
{
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)
{
UIColor * barColor = [UIColor
colorWithRed:222.0/255.0
green:255.0/255.0
blue:229.0/255.0
alpha:1.0];
[self.navigationController.navigationBar setBarTintColor:barColor];
UIColor * barTintColor = [UIColor
colorWithRed:48.0/255.0
green:140.0/255.0
blue:76.0/255.0
alpha:1.0];
[self.navigationController.navigationBar setTintColor:barTintColor];
NSDictionary *navbarTitleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
barTintColor,UITextAttributeTextColor,
[UIColor clearColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 0)], UITextAttributeTextShadowOffset, nil];
[[UINavigationBar appearance] setTitleTextAttributes:navbarTitleTextAttributes];
}
else
{
UIColor * barColor = [UIColor
colorWithRed:73.0/255.0
green:208.0/255.0
blue:114.0/255.0
alpha:1.0];
[self.navigationController.navigationBar setTintColor:barColor];
}
}
I had the same problem.
Fixed by turning off "Transcluent" checkbox in properties of Navigation Bar of my root Navigation Controller in Storyboard.
You need to open left pane named "Document Outline" with small button in bottom left to find Navigation Bar.

IOS7 navigatinBar tintColor change in popover

I developed iPad application. I'm opening some screens in popover with navigation controller. But I did not change navigationcontroller tint color in IOS 7. How can I change this color. thanx
UINavigationController *navigationController = [[[UINavigationController alloc] initWithRootViewController:airportsSearch] autorelease];
navigationController.navigationBar.barTintColor = [UIColor blackColor];
navigationController.navigationBar.translucent = NO;
self.popOver=[[UIPopoverController alloc] initWithContentViewController:navigationController];
self.popOver.delegate = self;
[self.popOver setPopoverContentSize:CGSizeMake(285, 370)];
[self.popOver presentPopoverFromRect:tempButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
The magical word here is barStyle, you need to do the following if you need it black:
navigationController.navigationBar.barStyle = UIBarStyleBlack;
navigationController.navigationBar.translucent = NO;
And if you want to change its color:
navigationController.navigationBar.barTintColor = [UIColor redColor];
navigationController.navigationBar.barStyle = UIBarStyleBlack;
navigationController.navigationBar.translucent = NO;
Setting the NavigationBarStyle to UIBarStyleBlack also worked for me, but only via the Storyboard.
I tried
[[UINavigationBar appearance] setBarStyle:UIBarStyleBlack];
as well as
[[UINavigationBar appearanceWhenContainedIn:[UIPopoverController class], nil] setBarStyle:UIBarStyleBlack];
in the didFinishLaunchingWithOptions AppDelegate method. But nothing changed. Only changing the BarStyle of the NavigationControllers NavigationBar inside the Storyboard worked.

Resources