Hope you can help with this one too...
I wanted to customize my navigation bar by adding custom background picture. I found out perfect but a bit complicated method called:NavigationSwizzle. In previous project I worked without any serious problems, but now I'm stuck...
From my AppDelegate I am calling this static method:
#implementation SCAppUtils
+ (void)customizeNavigationController:(UINavigationController *)navController
{
//Customizing navigation BAR
UINavigationBar *navBar = [navController navigationBar];
[navBar setTintColor:kSCNavBarColor];
//Customizing TOOLBAR
[navController setToolbarHidden:NO animated:YES];
UIToolbar *toolBar = [navController toolbar];
UIImageView *imageView = (UIImageView *)[navBar viewWithTag:kSCNavBarImageTag];
UIImageView *imageView2 = (UIImageView *)[toolBar viewWithTag:kSCNavBarImageTag];
if (imageView == nil)
{
Utilities *utilities = [[Utilities alloc]init];
UIImage *img = [UIImage imageNamed:#"nav-bar-background.png"];
UIImage *img2 = [UIImage imageNamed:#"toolbar-background.png"];
imageView = [[UIImageView alloc] initWithImage:img];
imageView2 = [[UIImageView alloc] initWithImage:img2];
[imageView setTag:kSCNavBarImageTag];
[imageView2 setTag:kSCNavBarImageTag];
[navBar insertSubview:imageView atIndex:0];
[toolBar insertSubview:imageView2 atIndex:0];
[imageView release];
[imageView2 release];
[utilities release];
}
}
In rootNavigationController after calling second view controller by pushing it on the stack my right button, self.title, activityIndicator don't show. If I comment or set atIndex:-1 in line [navBar insertSubview:imageView atIndex:0]; then buttons show, but my customized background is gone and I get regular iPhone navigational tab.
In didSelectRowAtIndex of rootNavigationController I have:
//ADD BACK BUTTON TO VIEW
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Home" style:UIBarButtonItemStyleDone target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
//Home Button color
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:166.0 / 255 green:179.0 / 255 blue:191.0 / 255 alpha:1.0];
//PUSH TO NEXT VIEW
[self.navigationController pushViewController:detailViewController animated:YES];
Thank you in advance...
If you need more code or explanation, please don't hesitate to ask me...
Best regards,
Luka
EDIT:
Ok, I can make this question much simpler. I have a custom background image in my navigation bar. This image is obviously covering my
self.navigationItem.title = #"TITLE";
If I put alpha to: 0.3 of imageView that is in the background I can see my title:
imageView = [[UIImageView alloc] initWithImage:image];
[imageView setAlpha:0.3];
[imageView setTag:kSCNavBarImageTag];
[navBar insertSubview:imageView atIndex:0];
So the question is how can I send my navigationItem.title to front? Is it a UIView class descendent?
EDIT 2:
Really weird thing is that title is visible (above background image) in the rootController but after pushing another viewController title goes behind te background imageView ?!
I honestly think you forgot to add this code in the main.m. You main.m should look something like this:
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import "SCClassUtils.h"
int main(int argc, char *argv[])
{
[SCClassUtils swizzleSelector:#selector(insertSubview:atIndex:)
ofClass:[UINavigationBar class]
withSelector:#selector(scInsertSubview:atIndex:)];
[SCClassUtils swizzleSelector:#selector(sendSubviewToBack:)
ofClass:[UINavigationBar class]
withSelector:#selector(scSendSubviewToBack:)];
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
You might want to try this to customize your Navigationbar instead. I use it in my apps and it works like a charm
UIImage *theImage = [UIImage imageNamed:#"barImage.png"];
[self.navigationController.navigationBar insertSubview:[[[UIImageView alloc] initWithImage:theImage] autorelease] atIndex:0];
See if that fixes your issues with the buttons not appearing.
I found a solituion for my work. Create a view (viewBackground) with all the images and colors that conform the navigation bar and then y convert it in a image and use it like a background
UIGraphicsBeginImageContextWithOptions(viewBackground.bounds.size, viewBackground.opaque, 0.0);
[viewBackground.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[[UINavigationBar appearance] setBackgroundImage:img forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setBackgroundImage:img forBarMetrics:UIBarMetricsDefault];
Related
I have a UICollectionView that displays image thumbnails. When clicked, the thumbnails call another view, wallpaperView, to show the image in full size.
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
// ...insert the usual row number check here
UIViewController *wallpaperView = [QUOWallpaperViewController new];
[self.navigationController pushViewController:wallpaperView animated:YES];
}
(The UICollectionView itself is the root view controller of a UINavigationController object, as per usual)
Now, in wallpaperView, I want to hide the navigation bar, and display my own custom button. I have already found a solution here.
Following the top answer there, I put this code in wallpaperViewController.m:
- (void)viewWillAppear:(BOOL)animated
{
[self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
}
However, this does not work. The navbar still shows up as usual. Does anyone know why this is the case?
if you need to show your own button as bar button on navigation then there is no need to hide navigationbar i've gone through your code and made some modifications just use this one and do not hide navigation bar ok.
UIImage* image3 = [UIImage imageNamed:#"mail-48_24.png"];
CGRect frameimg = CGRectMake(0, 0, image3.size.width, image3.size.height);
UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg];
[someButton setBackgroundImage:image3 forState:UIControlStateNormal];
[someButton addTarget:self action:#selector(sendmail) forControlEvents:UIControlEventTouchUpInside];
[someButton setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *mailbutton =[[UIBarButtonItem alloc] initWithCustomView:someButton];
self.navigationItem.rightBarButtonItem=mailbutton;
[someButton release];
self.navigationController.navigationBar.translucent = YES; // Setting this slides the view up, underneath the nav bar (otherwise it'll appear black)
const float colorMask[6] = {222, 255, 222, 255, 222, 255};
UIImage *img = [[UIImage alloc] init];
UIImage *maskedImage = [UIImage imageWithCGImage: CGImageCreateWithMaskingColors(img.CGImage, colorMask)];
[self.navigationController.navigationBar setBackgroundImage:maskedImage forBarMetrics:UIBarMetricsDefault];
//remove shadow
[[UINavigationBar appearance] setShadowImage: [[UIImage alloc] init]];
Try by hiding navigation controller
Inside the viewDidLoad method for your class QUOWallpaperViewController
write
[self.navigationController setNavigationBarHidden:YES animated:YES];
You can write this code in ViewDidLoad method.
self.navigationController.navigationBarHidden=YES;
I have added Navigation bar and Navigation Item in xib. Now I need to add image to the left of Navigation bar, but its not getting added.
This is the code I'm working on:
- (void)viewDidLoad
{
UIImage *image = [UIImage imageNamed: #"i_launcher.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
self.navigationItem.titleView = imageView;
[imageView release];
}
I can't understand why the image is not getting added. How can I add it?
Looks like this is an old question with an accepted answer. However, I wanted to add:
In at least Xcode 5, if you are using storyboards then you can add the image as the navigationBar's title by dragging an empty view into the title area, and then placing the ImageView inside the empty view.
The following screenshot should give you an idea of how it looks:
#implementation UINavigationBar (CustomImage) -(void)drawRect:(CGRect)rect {
CGRect currentRect = CGRectMake(0,0,100,45);
UIImage *image = [UIImage imageNamed:#"ic_launcher.png"];
[image drawInRect:currentRect];
}
#end
UPDATE
just add this code in your viewDidLoad: method..
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourimage.png"]];
UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourimage2.jpg"]]];
self.navigationItem.rightBarButtonItem = item;
and also if you want to direct set image in background of NavigationBar then use bellow line...
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"i_launcher.png"] forBarMetrics:UIBarMetricsDefault];
also see this Link how-to-add-a-customized-image-button-to-uinavigationbar-in-iphone-sdk
i hope this help you...
- (void)viewDidLoad
{
UIImage *image = [UIImage imageNamed:#"Your Image"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(5, 10, 72, 19);
[self.navViewController.navigationBar addSubview:imageView];
}
If you want to set your image at particular point in navigation bar then you can make a view and then add your image inside the view, adjust your image frame and then add this view to your navBar.
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"Default.png"]];
[image setFrame:CGRectMake(0, 0, 44, 44)];
[view addSubview:image];
[self.navigationController.navigationBar addSubview:view];
If you are adding NavBar from xib then just make IBOutlet for your UINavigationBar connect it in xib then
[myNavigationBar addSubview:view];
For Swift 4:
override func viewDidAppear(_ animated: Bool) {
// 1
let nav = self.navigationController?.navigationBar
// 2
nav?.barStyle = UIBarStyle.black
nav?.tintColor = UIColor.yellow
// 3
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
imageView.contentMode = .scaleAspectFit
// 4
let image = UIImage(named: "Apple_Swift_Logo")
imageView.image = image
// 5
navigationItem.titleView = imageView
}
Try to set the imageView.frame also please cross check the image initailized properly
UIImage *image = [UIImage imageNamed: #"i_launcher.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
imageView.frame.origin.x = 30.0; // You will have to find suitable values for the origin
imageView.frame.origin.y = 5.0;
self.navigationItem.titleView = imageView;
[imageView release];
Your code looks ok. But I don't know why you need to add navigation bar and item in xib. If you create your own view controller derived from UIViewController, it has contained navigation bar. You may try to remove yourself added navigation bar and item from xib and see what's happen.
If you want to show nav bar on the first view of a view-based application, you need to modify your application class code as below:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[TestViewController alloc] initWithNibName:#"TestViewController" bundle:nil];
// REMOVE THIS LINE !!!!!!!!!!!!!
//self.window.rootViewController = self.viewController;
// ADD BELOW TWO LINES !!!!!!!!!!!!!!!!!!!!!!!
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
I just did a test like the following:
Create a new single-view based project.
Run it without any modification. It will show an empty screen.
Modify didFinishLaunchingWithOptions() as above in the application.m file.
In the viewcontroller.m, add the following lines.
-(void)viewDidLoad
{
[super viewDidLoad];
UIImageView *view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
view.image = [UIImage imageNamed:#"info.png"];
self.navigationItem.titleView = view;
}
Now run again. You will see a nav bar with a image as title.
To hide / show the image between movements to another controller do this. Adjust the CGRectMake dimensions accordingly.
Add this to your header file:
#property (weak, nonatomic) UIImageView *navBarLogo;
Add this to your .m controller file:
- (void)viewWillAppear:(BOOL)animated {
UIImage *image = [UIImage imageNamed:#"image_name.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(53, 7, 210, 30);
self.navBarLogo = imageView;
[self.navigationController.navigationBar addSubview:imageView];
}
- (void)viewWillDisappear:(BOOL)animated {
[self.navigationController.navigationBar sendSubviewToBack:self.navBarLogo];
}
I hope this will help you sure. if not work mean check your image name is correct.
UIImage *yourimage = [UIImage imageNamed: #"i_launcher.png"];
UIButton *buttonBarButton = [ UIButton buttonWithType:UIButtonTypeCustom];
buttonBarButton.frame = CGRectMake(0, 0,yourimage.size.width,yourimage.size.height);
[buttonBarButton setBackgroundImage:yourimage forState:UIControlStateNormal];
[buttonBarButton addTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *buttonBarButtonItem = [[[UIBarButtonItem alloc]initWithCustomView:buttonBarButton] autorelease];
self.navigationItem.leftBarButtonItem = buttonBarButtonItem;
if you wish add action means :
[buttonBarButton addTarget:self action:#selector(your function) forControlEvents:UIControlEventTouchUpInside];
I'd like an image to take up all of a navigation bar. This is the navigation that comes with a navigation based app. It appears on the RootViewController with the accompanying UITableView. I've seen some examples of how this might work.
Set navigation bar title:
UIImage *image = [UIImage imageNamed:#"TableviewCellLightBlue.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.navigationController.navigationBar.topItem setTitleView:imageView];
The problem there is it only covers the title rather than the entire navigation bar.
There is also this thread: http://discussions.apple.com/message.jspa?messageID=9254241#9254241. Towards the end, the solution looks to use a tab bar, which I'm not using. It is that complicated to set a navigation bar background? Is there some other simpler technique?
I'd like to have a background for the navigation and still be able to use title text.
In your case, this solution found in another answer would work well.
With the "CustomImage" category added to UINavigationBar,
you can then just call:
UINavigationBar *navBar = self.navigationController.navigationBar;
UIImage *image = [UIImage imageNamed:#"yourNavBarBackground.png"];
[navBar setBackgroundImage:image];
This code should go in the method
- (void)viewWillAppear:(BOOL)animated
of the view controller where you want to have the custom image.
And, in that case you should better call:
[navBar clearBackgroundImage]; // Clear any previously added background image
before setBackgroundImage (otherwise it will be added multiple times...)
its changed for ios6, to make it work in ios 6 use:
UINavigationBar *navBar = self.navigationController.navigationBar;
UIImage *image = [UIImage imageNamed:#"image.png"];
[navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
UIImage *image = [UIImage imageNamed:#"YourImage.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.navigationController.navigationBar addSubview:imageView];
There is actually a much easier way to add a background image to any UIView class or subclass. It requires no class categorization or extension (subclassing), and you can do this on an "as needed" basis. For example, to add a background image to a view controller's navigation bar, do the following:
self.navigationController.navigationBar.layer.contents = (id)[UIImage
imageNamed:#"background.png"].CGImage;
You'll need to remember to add the Quartz Core framework to your project and add #import <QuartzCore/QuartzCore.h> wherever you need to do this. This is a much cleaner, simpler way to alter the drawing layer of anything that inherits from UIView. Of course, if you want to accomplish a similar effect for all navigation bars or tab bars, then subclassing makes sense.
UIImage *logo = [UIImage imageNamed:#"my_logo"];
UIImageView *logoView = [[UIImageView alloc]initWithImage:logo];
logoView.frame = CGRectMake(0, 0, 320, 37);
UINavigationController *searchNavCtrl = [[UINavigationController alloc] initWithRootViewController:searchViewController];
searchNavCtrl.navigationBar.barStyle = UIBarStyleBlack;
//searchNavCtrl.navigationItem.titleView = logoView;
//[searchNavCtrl.navigationController.navigationBar.topItem setTitleView:logoView];
[searchNavCtrl.navigationBar addSubview:logoView];
[logoView release];
Just add this line .
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"NavBar.png"] forBarMetrics:UIBarMetricsDefault];
Bam! One line and done.
I used cmp's solution and added some logic to remove it as I only wanted a custom background image on home screen within on view appear.
HomeViewController.m
UIImage *image = [UIImage imageNamed:#"HomeTitleBG.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.tag = 10;
UIImageView *testImgView = (UIImageView *)[self.navigationController.navigationBar viewWithTag:10];
if ( testImgView != nil )
{
NSLog(#"%s yes there is a bg image so remove it then add it so it doesn't double it", __FUNCTION__);
[testImgView removeFromSuperview];
} else {
NSLog(#"%s no there isn't a bg image so add it ", __FUNCTION__);
}
[self.navigationController.navigationBar addSubview:imageView];
[imageView release];
I also tried to use the suggested clearBackgroundImage method but couldn't get it to work so I gave the image a tag and then removed it in the other viewcontrollers on view will appear.
OtherViewController.m
UIImageView *testImgView = (UIImageView *)[self.navigationController.navigationBar viewWithTag:10];
if ( testImgView != nil )
{
NSLog(#"%s yes there is a bg image so remove it", __FUNCTION__);
[testImgView removeFromSuperview];
}
`
just go the view controller and paste in super viewdidload
and replace your image in mainlogo and then set the navigation title in set your image logo
- (void)viewDidLoad
{
[super viewDidLoad];
//set your image frame
UIImageView *image=[[UIImageView alloc]initWithFrame:CGRectMake(0,0,70,45)] ;
//set your image logo replace to the main-logo
[image setImage:[UIImage imageNamed:#"main-logo"]];
[self.navigationController.navigationBar.topItem setTitleView:image];
}
Add code in appdelegate did finish with launching method
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
if([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0)
{
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"navigation_or.png"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:0.0 forBarMetrics:UIBarMetricsDefault];
}
else
{
[[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0x067AB5)];
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
// Uncomment to assign a custom backgroung image
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"navigation_or.png"] forBarMetrics:UIBarMetricsDefault];
// Uncomment to change the back indicator image
[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
[[UINavigationBar appearance] setBackgroundColor:[UIColor whiteColor]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:#""]];
// Uncomment to change the font style of the title
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
shadow.shadowOffset = CGSizeMake(0, 0);
[[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttributeName,shadow, NSShadowAttributeName,[UIFont fontWithName:#"HelveticaNeue-Bold" size:17], NSFontAttributeName, nil]];
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:0.0 forBarMetrics:UIBarMetricsDefault];
}
Is it possible to give a UIToolBar a custom background from an image rather than the usual tinted blue/black fade out?
I've tried giving the view a background and setting the opacity of the UIToolBar but that also affects the opacity of any UIBarButtons on it.
Answering my own question here!!! Overriding the drawRect function and creating an implementation of the UIToolbar does the trick :)
#implementation UIToolbar (CustomImage)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: #"nm010400.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
UIToolbar inherits from UIView. This just worked for me:
[topBar insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:BAR_BKG_IMG]] autorelease] atIndex:0];
Slightly modified version of loreto's answer, which works for me on ios 4 and 5:
// Set the background of a toolbar
+(void)setToolbarBack:(NSString*)bgFilename toolbar:(UIToolbar*)toolbar {
// Add Custom Toolbar
UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:bgFilename]];
iv.frame = CGRectMake(0, 0, toolbar.frame.size.width, toolbar.frame.size.height);
iv.autoresizingMask = UIViewAutoresizingFlexibleWidth;
// Add the tab bar controller's view to the window and display.
if([[[UIDevice currentDevice] systemVersion] intValue] >= 5)
[toolbar insertSubview:iv atIndex:1]; // iOS5 atIndex:1
else
[toolbar insertSubview:iv atIndex:0]; // iOS4 atIndex:0
toolbar.backgroundColor = [UIColor clearColor];
}
This is the approach I use for iOS 4 and 5 compatibility:
if ([toolbar respondsToSelector:#selector(setBackgroundImage:forToolbarPosition:barMetrics:)]) {
[toolbar setBackgroundImage:[UIImage imageNamed:#"toolbar-background"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
} else {
[toolbar insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"toolbar-background"]] autorelease] atIndex:0];
}
just add this piece to your -(void)viewDidLoad{}
[toolBarName setBackgroundImage:[UIImage imageNamed:#"imageName.png"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
If you use idimmu's answer and want your barbuttonitems to be colored instead of the defaults, you can add these couple of lines of code as well to your category:
UIColor *color = [UIColor redColor];
self.tintColor = color;
You can use the Appearance API since iOS5:
[[UIToolbar appearance] setBackgroundImage:[UIImage imageNamed:#"navbar_bg"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
To be iOS 5 compliant you can do something like this
-(void) addCustomToolbar {
// Add Custom Toolbar
UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"customToolbar.png"]];
img.frame = CGRectMake(-2, -20, img.frame.size.width+4, img.frame.size.height);
// Add the tab bar controller's view to the window and display.
if( SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO( #"5.0" ) )
[self.tabBarController.tabBar insertSubview:img atIndex:1]; // iOS5 atIndex:1
else
[self.tabBarController.tabBar insertSubview:img atIndex:0]; // iOS4 atIndex:0
self.tabBarController.tabBar.backgroundColor = [UIColor clearColor];
// Override point for customization after application launch.
[self.window addSubview:tabBarController.view];
}
this one works fine for me:
ToolbarOptions *tbar = [[ToolbarOptions alloc] init];
[tbar setToolbarBack:#"footer_bg.png" toolbar:self.toolbarForPicker];
[tbar release];
#import <Foundation/Foundation.h>
#interface ToolbarOptions : NSObject {
}
-(void)setToolbarBack:(NSString*)bgFilename toolbar:(UIToolbar*)toolbar;
#end
#import "ToolbarOptions.h"
#implementation ToolbarOptions
-(void)setToolbarBack:(NSString*)bgFilename toolbar:(UIToolbar*)bottombar {
// Add Custom Toolbar
UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:bgFilename]];
iv.frame = CGRectMake(0, 0, bottombar.frame.size.width, bottombar.frame.size.height);
iv.autoresizingMask = UIViewAutoresizingFlexibleWidth;
// Add the tab bar controller's view to the window and display.
if([[[UIDevice currentDevice] systemVersion] intValue] >= 5)
[bottombar insertSubview:iv atIndex:1]; // iOS5 atIndex:1
else
[bottombar insertSubview:iv atIndex:0]; // iOS4 atIndex:0
bottombar.backgroundColor = [UIColor clearColor];
}
#end
You can do this with a category that basically adds a new property to UIToolBar. Overriding drawRect can work but it's not necessarily future proof. That same strategy for custom UINavigationBar stopped working with iOS 6.
Here's how I'm doing it.
.h file
#interface UIToolbar (CustomToolbar)
#property (nonatomic, strong) UIView *customBackgroundView;
#end
.m file
#import "CustomToolbar.h"
#import
static char TIToolbarCustomBackgroundImage;
#implementation UIToolbar (CustomToolbar)
- (void)setCustomBackgroundView:(UIView *)newView {
UIView *oldBackgroundView = [self customBackgroundView];
[oldBackgroundView removeFromSuperview];
[self willChangeValueForKey:#"tfCustomBackgroundView"];
objc_setAssociatedObject(self, &TIToolbarCustomBackgroundImage,
newView,
OBJC_ASSOCIATION_RETAIN);
[self didChangeValueForKey:#"tfCustomBackgroundView"];
if (newView != nil) {
[self addSubview:newView];
}
}
- (UIView *)customBackgroundView {
UIView *customBackgroundView = objc_getAssociatedObject(self, &TIToolbarCustomBackgroundImage);
return customBackgroundView;
}
#end
In your view controller code, e.g. viewDidLoad
if (self.navigationController.toolbar.customBackgroundView == nil) {
self.navigationController.toolbar.customBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"navigation_bar_background.png"]];
self.navigationController.toolbar.customBackgroundView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
}
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];