UINavigation bar with custom image - ios

I want Custom navigation bar with size =60 .And In this navigation bar i add my custom image to this.
I write this code..
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"1.png"] forBarMetrics:UIBarMetricsDefault];
help me thanks in advance

hope this will help you
ok , you want to customize the height of navigation bar(according apple designing guildelines you should ignored it but there are many beautiful apps in app store with customize navigation bar height).
the one way i got to know to customize the navigation bar by subclassing UINavigationBar and and overvide the following method
- (CGSize)sizeThatFits:(CGSize)size
{
CGSize customizeSize = CGSizeMake(customizeWidth , customizeWidthHeight);
return customizeSize;
}

UIImageView *imgView = [[UIImageView alloc] initWithFrame:SOMEFRAME];
imgView.image = SOMEIMAGE;
self.navigationItem.titleView = imgView;
navigation bar has the property title view. U can add any Elements u want on it.

Related

Resizing UISearchBar in UINavigation Bar?

I have added UISearchBar to UINavigationBar and this works really well, Except the left padding of UISerachBar here.
On Debug I came to know that here back button frame is too large that's why UINavigation bar title view shifted from left too much.
How to deal with that?
I want to achieve something like twitter app where the spacing between back button and search bar is not much.
Thanks in advance!
You can add a custom back button in your navigation bar to override the default back button (in this case the left button):
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"my-back-button-image.png"]];
imageView.frame = CGRectMake(0, 0, 43, 30);
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:imageView];
self.navigationItem.leftBarButtonItem = barButtonItem;
The space between left button and search controller decreases.
As mentioned earlier by Michele Broggi you can try that.
Else
You can also set the search Bar in place of navigation Title view so that it would be centre aligned to the screen (If you want that design)

How to deal with default paddings while customise navigation bar with title view?

I am trying to customise the navigation bar with title view.
But it seems setting title view comes with its own left and right and top paddings.I was expecting the title view to cover the whole navigation bar according to the frames given.
Is it expected behaviour and if YES than how to deal with that?
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 44)];
view.backgroundColor = [UIColor greenColor];
//Navigation Bar
self.navigationItem.titleView = view;
If you just want the navigation bar to be green use [self.navigationController.navigationBar setBarTintColor:[UIColor greenColor]]; in iOS 7+ or [self.navigationController.navigationBar setTintColor:[UIColor greenColor]]; in iOS 6-
Yes, it seems it is not possible to remove that left/right padding. This is the screenshot form debugging views in Xcode after running your code
The outside view that is gray is the navigationBar and the green is obviously the titleView. No matter the frame for the titleView, it is clipped.
Apple documentation says just this:
Custom title views are centered on the navigation bar and may be resized to fit.
I think the only solution would be to subclass the navigationBar, so that you override the titleView frame.

Can't set my navigation bar background image

I want to change my table view navigation bar background image to some png, i created the nav bar png with the exact sizes of a default nav bar (I checked in interface builder).
This is my code (in my table view controller class, viewDidLoad method):
UINavigationBar *navBar = self.navigationController.navigationBar;
UIImage *image = [UIImage imageNamed:#"nav.png"];
[navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
Shouldn't this work..?
this is my table view settings in the interface builder if this help:
the top one is what i suppose to get and the bottom one is what im getting:
thanks!
UIImageView *backImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"nav.png"]];
[self.navigationController.navigationBar addSubview:backImgView];
Try to add Image in custom view
Why not use the existing nav bar? Don t think you need to create a new one... This code is working for me :
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"nav.png"] forBarMetrics:UIBarMetricsDefault];
Your TableViewController must be "embadded" in a navigation controller a.k.a connected to other ViewControllers as push segue... Then you get your nav bar automatically instead of creating one... I mean you create a nav bar and never add it to the view isnt it?

How to Customize navigation bar in XCode5 iOS7 in AppDelegate file?

I am trying to customise the navigation bar of my iPhone app. I want to add an image in the left side of the navigation bar title.
I tried the following code from the didFinishLaunchingWithOptions of the AppDelegate.m file:
UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(170, 10, 20, 20)];
UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"logo.png"]];
[myView addSubview:image];
[self.navigationController.navigationBar addSubview:myView];
self.navigationController.navigationItem.titleView=myView;
but it doesn't work. When I try it from the viewDidLoad of one of my ViewControllers it works. But i want to inherit this behaviour in all my ViewControllers
UINavigationItem has a property, titleView. You need to set that to your custom view, and then that view will replace the standard title (so you need to have your image and a label for the title in your custom view).
self.navigationItem.titleView = myView;

How to design custom navigation bar

I have searched lots for how to design navigation like given in the image.
I have created category for UINavigationBar like given in the following solution
Custom nav bar styling - iOS
Able to increase the size of NavigationBar using below code.
CGFloat navBarHeight = 84.0f;
CGRect frame = CGRectMake(0.0f, 0.0f, 320.0f, navBarHeight);
[self.navigationController.navigationBar setFrame:frame];
But all content like (left button, right button, title) moves to bottom of bar.
When attach segment control to titleView like below
self.navigationItem.titleView = self.segmentedControl;
All views( left button, title, right button) as liner.
I want segment control bottom of the navigation bar as shown in the image.
#Shankar Shinde why are you adding segment controller as navigation title view you can add below the navigation bar using storyboard: check below screen shot
I think you need to hide the navigation bar using following code and create view like attached screenshot let me know it's help you or not ?
[self.navigationController.navigationBar setHidden:YES];
Please check it out sample:
https://github.com/boctor/idev-recipes/tree/master/WoodUINavigation
It may help you :)
You can use Prompt provided for navigationItem
First give prompt to your navigationItem
self.navigationItem.prompt = #"";
Create a segmentcontroll
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(35, 200, 250, 50);
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
segmentedControl.selectedSegmentIndex = 1;
[self.navigationController.navigationBar addSubview:segmentedControl];

Resources