Navigation bar (iphone) - iphone

I have created email functionality view in which everything is working fine, but the color of a navigation bar item is not changing like other views. I have used below code for navigation color but 1 navigation bar button item is left with default color. How do I change it?
controller.navigationBar.barStyle = UIBarStyleBlack;

How about use this?
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
of course on your viewDidLoad

Do you create the bar button item yourself? If so, you can change its background color by making sure it's a "custom view" type button item:
// Assuming you have some predetermined width w and height h
UIView *backgroundView = [[[UIView alloc]
initWithFrame:CGRectMake(0.0, 0.0, w, h)] autorelease];
backgroundView.backgroundColor = [UIColor blackColor];
UIBarButtonItem *myItem = [[[UIBarButtonItem alloc]
initWithCustomView:backgroundView] autorelease];
Then you can change the background color at a future date by setting the backgroundColor property on the bar button item's customView:
myItem.customView.backgroundColor = [UIColor yellowColor];

Related

iPhone Set Tint Color of Back Bar Button Item

I am trying to set the tint color of the back button within a navigation controller, but nothing is working. I have tried
[self.navigationController.backBarButtonItem setTintColor:myColor];
//myColor was previously set
But it stays the default tint
I believe barButtonItem is read-only. (I'm not too sure about this, someone correct me if I'm wrong)
To give a tint colour to these buttons, this is what I would do:
In your App Delegate, add these lines of code:
UIBarButtonItem *barButtonAppearance = [UIBarButtonItem appearance];
[barButtonAppearance setTintColor:[UIColor redColor]]; // Change to your colour
The first line sets an appearance proxy, so I believe this works too, if you like less code:
[[UIBarButtonItem appearance] setTintColor:[UIColor redColor]];
I hope this works for you! (This changes all instances of UIBarButtonItem)
You can't change the tint color of UINavigationItem directly. You have to change the tint color of navigation bar and your bar button will automatically change its color.
Add these lines in your viewWillAppear method
[[self.navigationController navigationBar] tintColor] = [UIColor myColor];
Or You can create a custom button and initialize your UIBarButtonItem as
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:yourUIButton];
This worked for me.
[self.navigationController.navigationBar setTintColor:[UIColor redColor]];
It is very similar to the second answer...
If you prefer to do it in Interface Builder without writing code:
Select your UINavigationBar (inside UINavigationController)
In Attributes Inspector, find "Tint" - this is what sets Back Button color. Note that "Bar Tint" is not the same - this sets Navigation Bar background color.
It is important to set Back Button appearance this way, and not try to implement leftBarButtonItem, because implementing leftBarButtonItem will disable the built-in back navigation gestures, like swipe to go back.
Swift way:
It changes all items in the nav.
In AppDelegate do something like this:
let navControl = UINavigationBar.appearance()
navControl.tintColor = UIColor.grayColor()
Well you can certainly change the color of just the Back Button or perhaps any Bar Button on the Navigation bar. Here's a small snippet to do it.
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStylePlain target:self action:nil];
backButton.tintColor = [UIColor blackColor];
[self.navigationItem setBackBarButtonItem:backButton];
[backButton release];
Well this is just one of the ways for doing this.
I hope this helps!! I know the answer for it is already accepted, but I thought to give a proper snippet to it.
Cheers!!! Happy Coding!!
If you want to set it to predefined style you can use
[navigationBar setBarStyle: UIBarStyleBlack];
Place the following code in AppDelegate that will set color for Back button globally
//Set color of BackButtonItem globally
[[UIBarButtonItem appearance] setTintColor:[UIColor colorWithRed:5.0/255.0
green:127.0/255.0 blue:173.0/255.0 alpha:1.0]];
This is supported in iOS 5 and above
It always work for me:
self.navigationItem.leftBarButtonItem = [self logicToAddBackButton];
GET DEFAULT BACK BUTTON
-
(UIBarButtonItem*) logicToAddBackButton
{
UIImageView *imageView;
// [imageView setTintColor:[UIColor redColor]];
UILabel *label=[[UILabel alloc] init];
if (WHITEBACK) {
imageView =[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"UiNavigationBackIPAD"]];
[label setTextColor:[UIColor whiteColor]];
}else{ //DEFAULTBACK
imageView =[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"UiNavigationBack"]];
[label setTextColor:[UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0]];
}
[label setText:#"Back"];
[label sizeToFit];
int space=6;
label.frame=CGRectMake(imageView.frame.origin.x+imageView.frame.size.width+space,
label.frame.origin.y, label.frame.size.width,
label.frame.size.height);
UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, label.frame.size.width+imageView.frame.size.width+space,
imageView.frame.size.height)];
view.bounds=CGRectMake(view.bounds.origin.x+8, view.bounds.origin.y-1, view.bounds.size.width,
view.bounds.size.height);
UIButton *button=[[UIButton alloc] initWithFrame:CGRectMake(view.bounds.origin.x, view.bounds.origin.y,
view.bounds.size.width, view.bounds.size.height)];
button.bounds=CGRectMake(view.bounds.origin.x, view.bounds.origin.y, view.bounds.size.width,
view.bounds.size.height);
[button addTarget:self action:#selector(eventBack) forControlEvents:UIControlEventTouchUpInside];
[button addSubview:imageView];
[button addSubview:label];
[UIView animateWithDuration:0.33 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
label.alpha = 0.0;
CGRect orig=label.frame;
label.frame=CGRectMake(label.frame.origin.x+25, label.frame.origin.y -5, label.frame.size.width,
label.frame.size.height+10);
label.alpha = 1.0;
label.frame=orig;
} completion:nil];
UIBarButtonItem *backButton =[[UIBarButtonItem alloc] initWithCustomView:button];
return backButton;
}
BACK BUTTON ACTION
(void)eventBack
{
[self.navigationController popViewControllerAnimated:YES];
}
UiNavigationBack Image (Please change colour of image as require with same size [25X41 144pixels/inch] to get
default look)
What worked for me was this :
self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor textPrimaryColor] forKey:NSForegroundColorAttributeName];

Unable to change text and text color of navigationitem's back button

I am trying to set the backbutton text color (to red) of my navbar using the code below. (NOTE: The code below is already in the 'previous' view controller, in this case in the 'Popular' view controller:
//Customize Back button
UIView *backButtonView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 30, 40)];
UILabel *backButtonLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 30, 40)];
backButtonLabel.text = #"back";
backButtonLabel.textColor = [UIColor colorWithHexString:#"cf212a"];
[backButtonView addSubview:backButtonLabel];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]initWithCustomView:backButtonView];
self.navigationItem.backBarButtonItem = backButton;
However, I am not getting the effect I want. I am still getting the default white color text in the backbutton. The text is not changed to 'back' as well
How can I resolve this?
Thats because the backbuttonitem is a uibarbuttonitem that will be seen on the next view controller which will be pushed on top of your current view controller and not of the current view controller.
If you want to set the backbutton of the current view like you are trying to do in your above code than just move the code to the view controller that is shown before it(below it in the stack.) so that when you push the view controller for which you want to show custom back button which was set in the previous view controller. This is because the back button belongs to the previous view controller that is gonna push your new view controller...
I believe that only the title property matters in the back button item; it is there to let you have a long title in the middle and a shorter title in the back button.
The easiest way to add a custom "back button" is to set UINavigationItem.leftBarButtonItem; you'll have to set its target/action appropriately.
I just put together a simple UIViewController subclass that adds a customizable back button that allows you to change text colors. It basically adds some willAppear/willDisappear logic to animate the back button the way the UINavigationController does while using the leftBarButtonItem property.
https://github.com/typeoneerror/BBCustomBackButtonViewController
This worked for me.
Using this you can change the color of all of the navigation buttons:
[[UIBarButtonItem appearance] setTintColor:[UIColor redColor]];
Replace redColor with the following to adjust the color of the buttons:
colorWithRed:0/255.0 green:144/255.0 blue:200/255.0 alpha:1.0// pick your color using this
Be sure to put this in the view controller that pushes. Not the view controller where you want to see this back button color. Like Robin explained.
Best way I found to set it globally or locally is
[[UIBarItem appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:220.0/255.0 green:104.0/255.0 blue:1.0/255.0 alpha:1.0], UITextAttributeTextColor,
[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset,
[UIFont fontWithName:#"AmericanTypewriter" size:0.0], UITextAttributeFont,
nil]
forState:UIControlStateNormal];
The problem is you don't have a good way to set back button's font and colors at global level. That's in the current iOS 5. For that I'm using typeoneerror's https://github.com/typeoneerror/BBCustomBackButtonViewController mentioned here.
However there is a good way to set Navigation Bar buttons' text and tint color properties at global level without destroying other button's and menus appearance. I'm using code like:
[[UIButton appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleColor:[UIColor colorWithRed:102.0f/255.0f green:102.0f/255.0f blue:102.0f/255.0f alpha:1.0f] forState:UIControlStateNormal];
[[UIButton appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleShadowColor:[UIColor colorWithWhite:1.0 alpha:0.75] forState:UIControlStateNormal];
[[[UIButton appearanceWhenContainedIn:[UINavigationBar class], nil] titleLabel] setShadowOffset:CGSizeMake(1.0, 1.0)];
In one of my projects. I'm calling it from - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
self.navigationItem.backBarButtonItem.tintColor = [UIColor blackColor];

change table view's cell selection color

can we change the selection color of table view, i.e cell, by default its blue, so, can we change it to any other color?
regards
this way in code
aCell.selectedBackgroundView = [[[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"selectedBackground.png"]] autorelease];
or you can set it in interface builder to gray, I think blue and grey is all that is available without creating a be view
Another way to do this, that doesn't require an image file is:
UIView *bgView = [[UIView alloc] init];
// At least on iOS6 you don't to size the view for this to work.
bgView.backgroundColor = [UIColor grayColor];
aCell.selectedBackgroundView = bgView;

Change position of navigationItem.title

In my ViewDidLoad I try something like that but its not working:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10,2,60,14)];
label.autoresizingMask = UIViewAutoresisingFlecibleWidth;
[self.navigationController.navigationBar addSubview:label];
I want to display a label in the left part of my NavigationBar.
Or maybe change the navigationBar.title position
Thanks,
You can do set navigation item's title view to UILabel with left text alignment:
UILabel* lbNavTitle = [[UILabel alloc] initWithFrame:CGRectMake(0,40,320,40)];
lbNavTitle.textAlignment = UITextAlignmentLeft;
lbNavTitle.text = NSLocalizedString(#"Hello World!",#"");
self.navigationItem.titleView = lbNavTitle;
[lbNavTitle release];
Created this way item's title also tolerates buttons on navigation bar and does not overlap them.

how to display an image in the navigation bar of an iPhone application?

how to display an image in the navigation bar of an iPhone application? (say, right after the title)
Here's how to have the image centered in the NavBar.
UIImage *image = [UIImage imageNamed: #"NavBarImage.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
self.navigationItem.titleView = imageView;
[imageView release];
This code is actually contained with the Apple source for the NavBar and can be found at the iPhone Developer Center at Apple.
The source shows you various ways to manipulate both the StatusBar & NavBar.
I haven't tested this but as UINavigationBar is a view you can add subViews to it.
UIImage* myImage = [UIImage imageNamed:#"Myimage.png"];
UIImageView* myImageView = [[UIImageView alloc] initWithImage:myImage];
myImageView.frame.origin.x = 30.0; // You will have to find suitable values for the origin
myImageView.frame.origin.y = 5.0;
[myTabbar addSubView:myImageView];
[myImageView release];
You can use things like the backItem property to calculate the position of your image view.
If you want the image at the right of the nav bar, you can define it as a custom button with no action when presed, like this
UIButton* fakeButton = (UIButton *) [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourimage.png"]];
UIBarButtonItem *fakeButtonItem = [[UIBarButtonItem alloc] initWithCustomView:fakeButton];
self.navigationItem.rightBarButtonItem = fakeButtonItem;
[fakeButtonItem release];
[fakeButton release];
Simply Place that code in - (void)viewWillAppear:(BOOL)animated; so it'll work fine
and add one image having size 320x40 named Top Bar
UIImage *image = [UIImage imageNamed: #"TopBar.png"];
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
the navigation bar has a property called title view - set this to the image you like. Since the titleView overwrites the title of the nav bar you have to include the desired title in the image file. Still set the title to what you want so it appears on the back button when you push a view Controller
I encountered the same problem.Found out the best solution
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"background_image.png"] forBarMetrics:UIBarMetricsDefault];
Hope this would help....
Just write your own navigation bar. Therefore you have to disable the Navigation Bar fist:
Disable the top bar in the interface builder by selecting your Navigation Controller in
your Storyboard: Attributes Inspector -> Simulated Metrics -> Top Bar: select None
Afterwards you can add any HeaderView you like...
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, sFrame.size.width, 100)];
UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"header_image.png"]];
self.headerView.backgroundColor = background;
// ...add buttons and labels
[self.view addSubview:headerView];