Drawing in custom UINavigationBar, attached to top - ios

I have a subclass of UINavigationBar. It's barPosition is UIBarPositionTopAttached.
Than I override -(void)drawRect:(CGRect)rect in my subclass. The rect, that comes as parameter, has always 44 px height, and I can draw only inside this rect. So, I can't perform drawing over the status bar and it has default look. If I comment -drawRect out, than it looks as expected, navigation bar and status bar look as a whole and have 64 px height. Is there a way to achieve this effect with having overrided -drawRect: in subclass of UINavigationBar?

I had a the issue of a custom UINavigationBAr with a background image which would not appear under the translucent Status Bar on iOS 7 when I was adding the image in drawRect.
After moving the background image to the ViewControllers viewDidLoad Method, the background image then appeared under the Status Bar
-(void)viewDidLoad
{
if (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPad)
{
UIImage *image = [UIImage imageNamed:#"nav-bg-ipad.png"];
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}else{
UIImage *image = [UIImage imageNamed:#"nav-bg-iphone.png"];
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}
// do your other stuff now …
Hopefully this helps

Related

Remove status bar without making Navigation Bar shorter Objective-C

I'm making an app in which I have a navigation bar that contains a custom background image. It looks like this.
I want to remove the status bar (as it covers part of the Nav Bar image), however when I do it shortens the NavBar, like so:
I want to find a way to get rid of the status bar without shortening the nav bar and causing the image in the navBar to come all out of proportion.
Here is the code I use to set the background image of the NavBar.
[[UINavigationBar appearance] setBackgroundImage:[[UIImage imageNamed:#"navigationBar"] stretchableImageWithLeftCapWidth:0 topCapHeight:0] forBarMetrics:UIBarMetricsDefault];
Thanks!
Edit: Please note that I attempted to just change the height of the Navigation Bar, however the background image remained out of proportion.
UIImageView *logoImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourLogoImage"]];
[logoImageView setFrame:CGRectMake(0.0f, 0.0f, 200.0f, 44.0f)];
logiImageView.contentMode = UIViewContentModeCenter;
self.navigationItem.titleView = logoImageView ;
Will fix your UIImage stretching.
EDIT:
or
self.navigationController.navigationItem.titleView = logoImageView;

Transparent Navigation Bar & Tabbar in iOS

I want to have a navigation bar and tabbar transparent to view background (reference image ==> https://www.dropbox.com/s/3vn8ef0p5lv1cw7/Required.png?dl=0). I am already setting background to view. But, I couldn't have Navigation Controller & Tabbar Controller transparent (reference image ==> https://www.dropbox.com/s/vfewqpwve78l4dq/Current.png?dl=0).
This is what I have tried before :
1. Setting background color of Navigation Bar and Tabbar to null/clear color.
2. BarTintColor works for Navigation Bar but it only allows me to set color, cannot have clearcolor (makes it black when I tried)
3. Translucent property !
Please help. Thanks in advance.
try to set transparent background images for navigation bar and tab bar. This will definitely solve your issue.
UIImage *image = [UIImage imageNamed:#"NavigationBar.png"];
[navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
Updated:
[self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.navigationBar.shadowImage = [UIImage new];
self.navigationBar.translucent = YES;

UI navigation bar custom background iPhone SDK

I have customized the UINavigationBar of my app with a texture. Here is my code:
if ([[UINavigationBar class]respondsToSelector:#selector(appearance)]) {
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"MYIMAGE.png"] forBarMetrics:UIBarMetricsDefault];
}
The code works but I want to reduce the size of my texture how to do that?
I recommend that you resize or crop the image before you set it as appearance to the UINavigationBar. Here are two SO links to do that
Crop UIImage
Resize UIImage

How do I edit the background color of backBarButtonItem (UINavigationItem) ? iOS 4.3 issue

I am trying to edit the background behind the backBarButtonItem as it insists on remaining white. I have edited the background color of the entire navigation bar but to no avail.
It only seems to affect iOS4 however, seeing as the:
[self.navigationController.navigationBar setBackgroundImage:navBarBackground forBarMetrics:UIBarMetricsDefault];
code works on iOS5. I have also tried to edit the navigation bar using this method also:
#implementation UINavigationBar (UINavigationBarCategory)
-(void)drawRect:(CGRect)rect{
UIImage *img = [UIImage imageNamed:#"bg_actionbar_edit.png"];
[img drawInRect:rect];
}
#end;
however this code results in all of my navigation bars to change, which is something that I do not want. Is there any simple code that I could use to simply change the BG colour of a buttonItem based on the navigation bar?
Set the tint of your navigation bar to change the color of the navigation bar button items:
self.navigationController.navigationBar.tintColor = [UIColor grayColor];

Remove Background Image on UINavigationBar

My app uses a navigation controller with an image on in the navigation bar of the home page. The background image is done by putting
[navigationController.navigationBar setBackgroundImage:navImage forBarMetrics:UIBarMetricsDefault];
[_window addSubview:navigationController.view];
in the app delegate. The nav bar displays this image fine.
Every other view will not have an image in the navigation bar. Therefore, I need to remove this background image when any other view controller is pushed. Does anyone know how this can be done?
There are many answers online about adding or changing a nav bar image, but this question is a little bit different. Thank you in advance.
To remove UINavigationBar background in iOS 5 or later, you should do this:
[navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
Hope it helps
I believe this to be the first actual answer to this for iOS5, the main problem being the "removal" of the background image once you are done. Well, just retain the existing image and put it back when you are done.
#implementation MyViewController {
UIImage *_defaultImage;
}
- (void)viewWillAppear:(BOOL)animated {
_defaultImage = [self.navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"bar.png"] forBarMetrics:UIBarMetricsDefault];
}
- (void)viewWillDisappear:(BOOL)animated {
[self.navigationController.navigationBar setBackgroundImage:_defaultImage forBarMetrics:UIBarMetricsDefault];
}
You can set background image with blank image created in runtime.
UIGraphicsBeginImageContextWithOptions(CGSizeMake(36, 36), NO, 0.0);
UIImage *blank = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[[UINavigationBar appearance] setBackgroundImage:blank forBarMetrics:UIBarMetricsDefault];
Creating blank image in runtime:
How to create a blank transparent png in Objective-C?
Nothing here have worked for me. After searching for hours, the only thing that worked was following a source code example provided by Apple.
I placed the following code and it worked and removed the navigation bar image from other view controllers finally!!
// Reset everything about navigation bar. (Other flows' background image used to show up on another view controller)
self.navigationController!.navigationBar.setBackgroundImage(nil, for: .default)
self.navigationController!.navigationBar.setBackgroundImage(nil, for: .compact)
self.navigationController!.navigationBar.barTintColor = nil
self.navigationController!.toolbar.barTintColor = nil
self.navigationController!.toolbar.isTranslucent = true
Hope this helps someone.
Cheers
It looks like the easiest way to do this is to:
1) set the alpha of the navigation bar on the home page to 0.000001
2) put the background image in a UIImageView "behind" the navigation bar (which is now clear)
3) use viewWillAppear / viewWillDisappear to set the navigation bar's opacity to either 1 or 0.00001 when views are pushed / popped.

Resources