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;
Related
I have a detailview with a navigation bar with a back button and a name for the view.
The navigation bar is set programmatically.
The name presented is set like this.
self.title = NSLocalizedString(name, #"");
The name depends on the presented view.
Now I would like to also present a small icon on the navigation bar which also is depends on the view.
How do I do that?
You can set backGround image to navigationBar Using this
Put in didFinishLaunchingWithOptions
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"title_bar.png"] forBarMetrics:UIBarMetricsDefault];
Or you can set navigation bar image in any view using
UINavigationBar *navBar = [[self navigationController] navigationBar];
UIImage *image = [UIImage imageNamed:#"TopBar.png"];
[navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
Or you can set a view on your NavigationBar Link
[[UINavigationBar appearance] addSubview:yourView];
or
self.navigationItem.titleView = YourView;
And set title using h
self.navigationItem.title = #"Your Title";
And you can get navigationBarButton using this
-(void)getRightBarBtn
{
UIButton *Btn =[UIButton buttonWithType:UIButtonTypeCustom];
[Btn setFrame:CGRectMake(0.0f,0.0f,68.0f,30.0f)];
[Btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:#"yourImage.png"]] forState:UIControlStateNormal];
//[Btn setTitle:#"OK" forState:UIControlStateNormal];
//Btn.titleLabel.font = [UIFont fontWithName:#"Georgia" size:14];
[Btn addTarget:self action:#selector(yourBtnPress:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithCustomView:Btn];
[self.navigationItem setRightBarButtonItem:addButton];
}
And set simple imageView on navigation Bar
UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"star.png"]];
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithCustomView:image];
self.navigationItem.rightBarButtonItem = backBarButton;
You need to subclass UINavigationBar. Then in drawRect do something like:
[[UIImage imageNamed...] drawInRect:...]
and call the super method ofcourse
Add this code into viewDidLoad in your ViewController:
UIImage *image = [UIImage imageNamed: #"myIcon.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(5, 2, 100, 39);
[self.navigationController.navigationBar addSubview:imageView];
imageView can also be declared as an instance variable, so you can access it after you've added it (e.g. if you want to remove your icon from your navigationBar at some point).
UIButton *homeBtn=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 46, 28)];
[homeBtn setBackgroundImage:[UIImage imageNamed:#"homeBtn.png"] forState:UIControlStateNormal];
[homeBtn addTarget:self action:#selector(homeBtnPressed) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *navHomeBtn=[[[UIBarButtonItem alloc] initWithCustomView:homeBtn] autorelease];
self.navigationItem.rightBarButtonItem=navHomeBtn;
[homeBtn release];
I am able to give image to navigation bar and also changed image of navigation bar button item. For some reason navigation bar image, i guess, overlapped over navigation bar button image. Navigation bar button is functioning right when i click on navigation bar image at left but its not displaying over navigation bar image. here is the code ;
UIImage *imagebar = [UIImage imageNamed: #"schedule-strip.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: imagebar];
imageView.frame = CGRectMake(0, 0, 190, 44);
imageView.contentMode = UIViewContentModeScaleAspectFill;
self.navigationItem.titleView = imageView;
UIImage* image = [UIImage imageNamed:#"back-icon.png"];
CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
UIButton* someButton = [[UIButton alloc] initWithFrame:frame];
[someButton setBackgroundImage:image forState:UIControlStateNormal];
[someButton setShowsTouchWhenHighlighted:YES];
[someButton addTarget:self action:#selector(backToMore) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem* someBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:someButton];
[self.navigationItem setLeftBarButtonItem:someBarButtonItem];
[someBarButtonItem release];
[someButton release];
help me out.
use the code
[imageView addsubView someButton];
I hope now u will see the button.Actually you are adding the left bar button item to default navigation bar and as u r creating somebutton it is not getting added on imageview which is overlapping your navigation bar.So by clicking on that area it is working but button is not visible.
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: #"TopBg.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
extends #interface MyToolBar : UIToolbar and make object of MyToolBar. after that make your custom button for navigation bar. enjoy and like it
Ketan did you check for image ? is image exist in your project?
Why you are using frame and content mode property for Navigation bar title ?
May this link Help you
http://iosdevelopertips.com/user-interface/ios-5-customize-uinavigationbar-and-uibarbuttonitem-with-appearance-api.html
I'm building an app with a custom navigation bar. After some research I decided to do this using a category on UINavigationBar. The navigation bar needs to be a bit larger than usual to accomodate a drop shadow. Here is the code:
#import "UINavigationBar+CustomWithShadow.h"
#implementation UINavigationBar (CustomWithShadow)
- (void)drawRect:(CGRect)rect {
// Change the tint color in order to change color of buttons
UIColor *color = [UIColor colorWithHue:0.0 saturation:0.0 brightness:0.0 alpha:0.0];
self.tintColor = color;
// Add a custom background image to the navigation bar
UIImage *image = [UIImage imageNamed:#"NavBar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, 60)];
}
- (void)layoutSubviews {
self.frame = CGRectMake(0, 20, self.frame.size.width, 60);
}
#end
The only problem now is that the larger navigation bar means that the navigation bar buttons end up too far down, like so:
Does anyone know how I can correct the position of the buttons?
Thanks for all help!
Update:
I add the buttons to the nav bar in the init method of the view controller like so:
// Create "Add" button for the nav bar
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(createNewEntry:)];
[[self navigationItem] setRightBarButtonItem:addButton];
[addButton release];
You'll need to add the leftBarButtonItem and rightBarButtonItem as custom items and mess with the frames.... for example:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0,5,buttonImage.size.width,buttonImage.size.height)];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button addTarget:delegate action:#selector(barButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:titleString forState:UIControlStateNormal];
[button setTitleColor:CUSTOM_BAR_BUTTON_TITLE_COLOR forState:UIControlStateNormal];
[[button titleLabel] setFont:[UIFont boldSystemFontOfSize:14]];
[[button titleLabel] setShadowColor:CUSTOM_BAR_BUTTON_SHADOW_COLOR];
[[button titleLabel] setShadowOffset:CGSizeMake(0,-1)];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
[button release];
[[self navigationItem] setRightBarButtonItem:barButton];
[barButton release];
Try adding the buttons to the nav bar in the viewDidLoad method of the view controller instead.
My solution, not the best, but it works for me fine. My custom navigation bar has height 55 (default height is 44). I cut from my custom navigation bar only 44 of height and insert it to the navigation bar. Then I cut next part of my custom navigation bar (shadows etc.) and insert it as image view under the navigation bar. And that's it. Buttons are at right places...
how can i customize the navigation bar or add the background image in it and buttons of navigation bar?
please tell me in simple way not make the answer hard to understand i am not expert and?
explain step by step.
Follow this tutorial: How do iPhone apps Instagram/Reeder/DailyBooth implement custom NavigationBars with variable width back buttons?. It includes a source code example.
What you're looking for sadly isn't that easy to accomllish. The technique required rather reminds of a workaround than a working solution It's called method swizzling and here is a tutorial on how to apply it on a navbar in order to get a custom background image in there: http://sebastiancelis.com/2009/12/21/adding-background-image-uinavigationbar/
Adding buttons to your navigationbar is fairly easy. Say your navigation bar is instansiated as navigationController:
UIBarButtonItem *yourButton = [[UIBarButtonItem alloc] initWithTitle:#"Hello" style: UIBarButtonStylePlain target:self action:#selector(myTargetMethodSignature:)];
navigationController.navigationItem.rightBarButtonItem = yourButton;
[yourButton release];
Hope this helps.
Wasabi
Add the code in .m file above #implementation
- (void)drawRect:(CGRect)rect {
/ UIImage *image = [UIImage imageNamed: #"navigation_bar.png"];
UIImage *image = [UIImage imageNamed: #"navigationbarnew2.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, 44)];
}
#end
And to add button to navigation bar add the following code in viewdidload
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];
mailbutton =[[UIBarButtonItem alloc] initWithCustomView:someButton];
self.navigationItem.rightBarButtonItem=mailbutton;
[someButton release];
I have a navigation controller which also has a table view. I want to add an image left side of the navigation bar on top programmatically. When i tried to add by the below code in viewDidLoad:
CGRect frame = CGRectMake(0.0, 0.0, 94.0, 33.0);
UIImageView *image = [ [UIImageView alloc]initWithImage:[UIImage imageNamed:#"topBarImage.png"] ];
image.frame = frame;
[self.view addSubview:image];
but it is adding to top of the table view instead of navigation tab. I think, this image should be added as navigationItem. But i don't how to add an image on left side of navigation bar programmatically. Could some one guide please?
Clave/
you just use a custom view for a bar button item. Here is an example of one with a custom image button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"myImage.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(blah) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 32, 32)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
You want to do it this way because the title of the nav controller will be auto resized this way. If you do it your way (subview of the nav bar) the text will go behind or ontop of the image
edit: it will also stay on the screen as you pop and push view controllers.
edit2: once again, you can use an imageview as the custom view and not the button. Here is code
UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,32,32)];
[iv setImage:[UIImage imageNamed:#"myImage.png"]];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:iv];
[iv release];