Add image to UINavigationBar with tintColor - iphone

I'm trying to add an image to a UINavigationBar that has a custom color. I was hoping to avoid having to use a .png for the entire thing, and to programmatically set the color, and drop the icon on top of the colored navigation bar.
self.navigationController.navigationBar.tintColor = [UIColor redColor];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"icon.png"] forBarMetrics:UIBarMetricsDefault];
The UIBarMetricsDefault causes a repeat for the image icon, and I just want one in the center. The UIBarMetricsDefault and UIBarMetricsLandscapePhone settings are not working for me, and I was hoping someone would know a better suggestion.
I am trying to avoid using a .png for the entire navigation bar, and would like to add the image to a color that is set programmatically. Thank you!

The way you set color is correct.
To set image in center use titleView property of navigationItem as shown below :
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"icon.png"]];

Related

iOS5 Toolbar background image

I'm very new to iOS programming.
I'm trying to set the toolbar background to a custom image.
I'm also using storyboards.
How do I go about that?
Do I edit UIToolbar in the UI Kit framework? Do I need to change something in Storyboard?
Thanks,
You can use UIToolbar's built-in -setBackgroundImage:forToolbarPosition:barMetrics: method:
// portrait
[yourToolbar setBackgroundImage:[UIImage imageNamed:#"YourToolbarBkg-Portrait.png"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
// landscape
[yourToolbar setBackgroundImage:[UIImage imageNamed:#"YourToolbarBkg-Landscape.png"] forToolbarPosition:UIToolbarPositionAny barMetrics: UIBarMetricsLandscapePhone];
YourToolbarBkg-Portrait.png will be 320x44 bkg image for portrait mode
YourToolbarBkg-Landscape.png will be 480x32 bkg image for landscape mode.
UIToolbar inherits from UIView. This just worked for me:
[topBar insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:BAR_BKG_IMG]] autorelease] atIndex:0];
UPDATED
topBar ------ is the outlet of the UIToolBar u are using
use this code where u are creating ur UIToolBar the class which implements the UIToolbar..
plus tell me y r u using Toolbar whats ur main purpose for it
Instead of editing UIToolBar, why not create a UIView of the same size and skin that however you would like? That would be easier if you are new.
Or if you want to override UIToolbar:
#implementation UIToolbar (CustomImage)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: #"image.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
Had a right faff with background images for ToolBars and NavBars. I know setting the background image of a NavBar and ToolBar is generally a doddle. But, when you're presenting modal VC's, for some insane reason, where a NavBar is being added and you DO change the background image, it appears to double in size. Altering the height has strange results.
My issue was where I was using a NavController all over my App, but needed a modal view for one or two aspects of it. Simple enough. However, I needed either a NavBar or ToolBar type header with a Done button to pop the VC off the stack. Again, not an issue. But I needed the NavBar or ToolBar to look the same as everywhere else in my App.
I settled on a ToolBar, seeing as the VC was being presented modally. So, there my ToolBar sat, in typical Apple-blue. Nice, but not how the rest of my App looked, where a blackened image was being used for each NavBar. Using the iOS 5 appearance proxy, I altered the background image of the ToolBar. And this worked. But, unless I had the UIImage in exactly the proportions and size expected by the Tool Bar, I was in a pickle. The image simply did not look right at all. So, I decided to create a UIIMageView, where I could control the content mode, then insert a subview onto the toolBar.
Take a look at my code below.
UIImageView *toolbarImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:NAV_BAR_BACKGROUND]];
[toolbarImageView setFrame:[self.IBOToolBar bounds]];
[toolbarImageView setContentMode:UIViewContentModeScaleAspectFill];
[self.IBOToolBar insertSubview:toolbarImageView atIndex:1];
It's a bit of a fluff but I do hope this helps someone alter the image on their ToolBar.
NAV_BAR_BACKGROUND can be defined as follows:
#define NAV_BAR_BACKGROUND #"navBarBlackMattDarkSquare.png"

changing tabbar image color when it selected ios

In my application i have Tab bar Controller. I want to change default color of Tab bar image when it get selected i.e. default blue color.
Please note: I do not want to change background color of Tab bar but only the color of selected tabbar item when it selected from default blue color.
Here is my code:
NSArray *tabObjects=[NSArray arrayWithObjects:nav,video,about, nil];
tabView=[[UITabBarController alloc] init];
tabView.viewControllers=tabObjects;
I have seen many questions here and searched internet as well. Some people suggest private API's that i am not interested. Some people say to put custom image when tab get selected.
Please suggest me proper way to do so.
Thanks in advance
Seems like you need
[[UITabBar appearance] setSelectedImageTintColor:[UIColor purpleColor]];
Note: Works only with iOS >=5
//set the tabbar into image
[[[self tabBarController] tabBar] setBackgroundImage:[UIImage imageNamed:#"menubar"]];
//set the color of selected image color
[[[self tabBarController] tabBar] setSelectedImageTintColor:[UIColor redColor]];

setting UINavigationBar tintColor only sets it's back button color

SO I am setting a UINavigationBar tintColor and here's what I get:
[navController.navigationBar setTintColor:[UIColor whiteColor]];
How is this even possible? Here's some more info if it helps:
I've got a UINavigationController that I customize as follows:
First, come up with a 44px-high image as the background for the nav bar. (In this case, if you wanted to use a 44px-high white image, that'd do the trick --- but it'll look much nicer if you use some type of vertical gradient)
Second, use the following code in your AppDelegate's didFinishLaunching method. (The image is called "background_44.png".
// Set the background image for *all* UINavigationBars
UIImage *gradientImage44 = [[UIImage imageNamed:#"background_44.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
// Set the background image for *all* UINavigationBars
[[UINavigationBar appearance] setBackgroundImage:gradientImage44
forBarMetrics:UIBarMetricsDefault];
I dropped that code in my project, worked fine. Even changed bar styles and colors. No problem. This line of code is fine, there is an issue somewhere else or its some crazy glitch.

UIBarButtonItem icon white when added via IB, black when added programmatically

When I add an icon to a UIBarButtonItem via the Interface Builder, the icon is displayed white. When I add the same icon file programmatically to another UIToolbar, the icon is displayed black. Why?
UIImage *image = [UIImage imageNamed:#"icon.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:image forState:UIControlStateNormal];
rootViewController.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:reloadButton] autorelease];
Everything Jongsma said is right, you should use the initWithImage:style: message.
The next problem is not the way you create the UIBarButtonItem, but the place you assign it. You create it with UIBarButtonItemStylePlain, which should normally render the icon's outline in white, but the rightBarButtonItem of a UINavigationItem (just like the left) is not allowed the UIBarButtonItemStylePlain. It's implicitly converted to UIBarButtonItemStyleBordered. In the bordered style the icon is rendered 'as is', which is black with a slight gradient.
I think if you want the item in white on a bordered barButton, you'll have to touch the image itself.
Answer: If you want it white, color your image white.
Details:
UIBarButtonItems behave a little differently depending on how you use them.
When adding to a UIToolbar:
initWithImage:style:target:action: creates "white icons" (image color is ignored, opaque pixels are used as a mask to create a white image).
This is true for bordered and plain styles (but on UIToolbar only).
initWithCustomView: displays normal colored image.
When adding to a UINavigationItem:
initWithImage:style:target:action: creates colored images and converts plain to bordered.
In your code, you are setting an UIButton as the subview of an UIBarButtonItem.
UIBarButtonItem is already a button, so you shouldn't add another button as the subview.
Try this:
UIImage *image = [UIImage imageNamed:#"icon.png"];
rootViewController.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:image] autorelease];
Had the same problem. I Noted that the #2X images were used instead...

iPhone UISegmentedControl button states on black UIToolbar

I have a UISegmentedControl on a black UIToolbar. I have set the style to Bar and set the background color to clear (also tried black). I have tried setting the tintColor clear (also tried black). My buttons turn black to match the black UIToolbar. However, the buttons no longer indicate a clicked state like they do when the UISegmentedControl is the default blue/grey. What do I have to do to make the buttons indicate a black/grey clicked state? Please let me know. Code used so far to set the color of the UISegmentedControl:
viewTypeSelection.segmentedControlStyle = UISegmentedControlStyleBar;
viewTypeSelection.backgroundColor = [UIColor clearColor];
While not a perfect solution, this works pretty well
// set the color
viewTypeSelection.segmentedControlStyle = UISegmentedControlStyleBar;
viewTypeSelection.tintColor = [UIColor darkGrayColor];
The buttons have state change and it looks OK. Here is a post that has a few more details and might help someone looking for a similar solution:
UISegmentedControl black?
You may have set the color of the bar with tintColor instead of setting barStyle like so:
self.navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;