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

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;

Related

Common navbar for all viewController with same action

I am newbie in iOS.
I want to set navbar that display on all screens and managed by Navigation Controller and it calls the same action.
If you are talking about having some UIViewController's add the same UINavigationItem's, then I would create a super class that adds the UINavigationitem's and have the UIViewControllers's be a subclass it.
Use below code to set image in title.
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
imgView.image = [UIImage imageNamed:#"image_name.png"];
self.navigationItem.titleView = imgView;
Or create a custom method and invoke it in each controller in order to set different title logo in every controller.

UINavigation bar with custom image

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.

UIImageView over Navigation bar and main screen

I have an UImage that I need half on the navigation bar and half on the main screen. I have added a Navigation Controller on my project so now I have a nav bar on all screens. I need a square image to go half on the navigation controller and half on the main screen.
In XCODE I have successfully put an UImageView over the nav bar and the main screen but when i put an image on it i only appears in the part thats over the main screen and not over the navigation bar. I don't want to split the image so I can use Navigationbar.image control - is there another way ?
Instead of adding it to viewController, add it to window.
UIView *view =[[UIView alloc] initWithFrame:CGRectMake(40, 30,240,60)];
view.backgroundColor =[UIColor greenColor];
[[[UIApplication sharedApplication] delegate].window addSubview:view];
As you might have noticed the UINavigationBar appears on top of everything you have within your UIViewController's view.
One way of overcoming this is by adding your UIImageView to the keyWindow like so:
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
imageView.image = [UIImage imageNamed:#"myImage"];
[[UIApplication sharedApplication].keyWindow addSubview:imageView];

Why does adding imageview to navigation bar in storyboard remove navbar?

I have a working app which is a tabbarcontroller based app. The first viewcontroller is a uitableviewcontroller. All 3 tabs have a navigation bar on the top, that I added from the object library. This is what the app looks like:
Then I wanted to set the image on the navbar as a centered logo. So I looked around SO and found code that looks like this:
UIImage *image = [UIImage imageNamed:#"icon57.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
self.navigationItem.titleView = imageView;
or
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"icon57.png"] forBarMetrics:UIBarMetricsDefault];
But it didnt work. I just got an empty white nav bar. So I decided to add a UIImageView to the navbar by dragging it in from the object library but for some reason it makes the navbar disappear and I end up with this:
Why does this happen?
The way you are doing it is not supported in Interface Builder. I would encourage you to file a radar to support doing that. You can accomplish this via two different ways, one in code, the other in IB.
Through Interface Builder
You can drag a UIView instance from the object library and drop it into the center of the nav bar. This will set the view inside the title area of the bar. You may then take a UIImageView instance and add it as a subview of that the view you just added.
Through Code
You can set the UIImageView through code, using the titleView property of your view controller's navigation item:
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *logo = [UIImage imageNamed:#"logo.png"];
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:logo];
}
EDIT:
In order to set it on the left side, you'll have to wrap the image view in a UIBarButtonItem. You can do that in IB using the same procedure described above, or in code like the following:
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *logo = [UIImage imageNamed:#"logo.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:logo];
UIBarButtonItem *imageButton = [[UIBarButtonItem alloc] initWithCustomView:imageView];
self.navigationItem.leftBarButtonItem = imageButton;
}

How to also animate subview in navigation bar?

I have a subview in my navigation bar. I try to add it by this way:
UIView *customView =
[[UIView alloc] initWithFrame:
CGRectMake(0, 0, image.size.width + label.frame.size.width, 44)];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[customView addSubview:imageView];
[customView addSubview:label];
[self.navigationController.navigationBar addSubview:customView];
However, when I try to push the navigation bar, the customView stays in the place, not animating following the sliding navigation bar. How can I achieve the animated subview? Is it even possible? Thanks!
you should not add subview in that way
you have tospecify your view location in the left , right or title view
self.navigationItem.titleView = YOURVIEW;
or choose another location left or right items in this way the the title view will added to the current view if you want to remove it just set it to nil in the place you want and reload it subviews again,
self.navigationItem.titleView = nil;
As you are using
[self.navigationController.navigationBar addSubview:customView];
that means the navigation bar you have create is in App delegate and is common for all the viewControllers in your project,that is why once you add a view on to it you see it on every view you have. Remove your sub-view in
-(void)viewWillDisappear:(BOOL)animated{
[Your subview remove fromSuperView];
[super viewWillDisappear:YES];
}
and add that subview in
-(void)viewWillAppear:(BOOL)animated{
[self.navigationController.navigationBar addSubview:Your subview];
[super viewWillAppear:YES];
}
this will add the subview in that particular view only and remove it as soon as that view is popped or pushed.The code given is not correct to the syntax please give a check on that.

Resources