iOS: UIToolbar setBackgroundImage not working - ios

I can not change the uitoolbar background image. gives error.
This navigationBar code is working.
UIImage *navBackgroundImage = [UIImage imageNamed:#"bartop.png"];
[[UINavigationBar appearance] setBackgroundImage:navBackgroundImage forBarMetrics:UIBarMetricsDefault];
But this toolbar code is not working and gives error.
UIImage *navBackgroundImage = [UIImage imageNamed:#"bartop.png"];
[[UIToolbar appearance] setBackgroundImage:navBackgroundImage forBarMetrics:UIBarMetricsDefault];
Where is my wrong?
UPDATE:
This code is working:
UIImage *testimage = [[UIImage imageNamed:#"bartop"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UIToolbar appearance] setBackgroundImage:testimage forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
[[UIToolbar appearance] setBackgroundImage:testimage forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsLandscapePhone];

According to the documentation, the method you need to use with UIToolbar is
- setBackgroundImage:(UIImage *)backgroundImage forToolbarPosition:(UIToolbarPosition)topOrBottom barMetrics:(UIBarMetrics)barMetrics
You are using
- setBackgroundImage:(UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics
Try using that and see if it works for you.
More info here.

Related

iOS 11 UINavigationBar Back button image color issue

I am facing issue in iOS 11 with custom BackButton Image color.
BackButton Image works correct in versions lower the iOS 11.
I have customized the Back Button Image of UINavigationBar by using following code.
[[UINavigationBar appearance] setBackgroundColor:[UIColor blackColor]];
[[UINavigationBar appearance] setBarTintColor:[UIColor blackColor]];
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
[[UINavigationBar appearance] setTranslucent:false];
UIEdgeInsets insets = UIEdgeInsetsMake(0, 0, 5.0f, 0);
UIImage *backArrowImage = [[UIImage imageNamed:#"icon_nav_back"] imageWithAlignmentRectInsets:insets];
[[UINavigationBar appearance] setBackIndicatorImage:backArrowImage];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:backArrowImage];
It displays a proper image with default color of image in versions lower than iOS11
But, In Version iOS 11 its color and size have been improper.
Please provide proper solution to resolve this issue.
I have tried Tint Color also, but it doesn't work.
// untested code
// try to see if you can access the back button directly
NSArray *leftBarButtonItems = self.navigationController.navigationBar.items.firstObject.leftBarButtonItems;
for (id barButtonItem in leftBarButtonItems) {
UIBarButtonItem *item = (UIBarButtonItem*)barButtonItem;
if (item) {
item.tintColor = [UIColor whiteColor];
}
}
Follow this link: Swift how to change tintColor of backIndicatorImage
set image on this method setBackIndicatorTransitionMaskImage Might be help!
[UINavigationBar appearance].translucent = NO;
[[UINavigationBar appearance] setBackIndicatorImage:backArrowImage];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:backArrowImage];
Updated use following code it's definitely set back image
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[backBtn setBackgroundImage:backArrowImage forState:UIControlStateNormal];
[backBtn addTarget:self action:#selector(goback) forControlEvents:UIControlEventTouchUpInside];
backBtn.frame = CGRectMake(0, 0, 54, 30);
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn] ;
self.navigationItem.leftBarButtonItem = backButton;

ios7 setBackButtonBackgroundImage image is still stretched

Using the following code to set a global navigation back button image:
float imageSize = 21.5; // #2x is 43px width
UIImage *barBackBtnImg = [[UIImage imageNamed:#"arrow-back-white"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, imageSize, 0, 0)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:barBackBtnImg
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)
forBarMetrics:UIBarMetricsDefault];
Original image: http://cl.ly/XJgD and
in-app (being stretched): http://cl.ly/XKc0
Any ideas why the image is still being stretched?
If anyone is interested, this was my fix:
[[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:#"arrow-back-white"]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:#"arrow-back-white"]];
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];

How to setup a navigation bar with image?

Something weird and frustrating is happening with the UINavigationBar on iOS7. I'm trying to add an image like this:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"Logo_Small.png"] forBarMetrics:UIBarMetricsDefault];
The result is that the lettering of my header -- the Hebrew letters -- repeat all over the navigation bar.
I tried all four options for the UIBarMetrics flag, but with the other three the logo just disappears. I also set the image size to 320x64, as per the documentation. But nothing seems to do the right thing, and I'm on the verge of giving up using a logo and just have a controller.title.
Do the below:
UIImage *gradientImage = [[UIImage imageNamed:#"Logo_Small.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(-4, 300, 10, 300)];// play here
[[UINavigationBar appearance] setBackgroundImage:gradientImage forBarMetrics:UIBarMetricsDefault];
you can do like this
UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,44)];
iv.image = [UIImage imageNamed:#"Logo_Small.png"];
self.navigationItem.titleView = iv;
Use this,
UIImage *navBarImage64 = [[UIImage imageNamed:#"Logo_Small.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UINavigationBar appearance] setBackgroundImage:navBarImage64 forBarMetrics:UIBarMetricsDefault];
If above is not working then add imageview in your NavigationBar as below code. it work.
UIImage *image = [UIImage imageNamed: #"NavigationBar#2x.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
imageView.frame = CGRectMake(0, 0, 320, 44);
[self.navigationController.navigationBar addSubview:imageView];
You code is actually works. The problem is that you are probably calling it after UINavigationBar was added to screen.
To verify that your code is working you can try to put into AppDelegate's didiFinishedLaunchingWithOptions.
To customize navigation bar with Image use this,
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"ImageName.png"] forBarMetrics:UIBarMetricsDefault];

How to apply a background image to navigation bar

I want to insert an image to the navigation bar.
My code is
UINavigationBar *navBar = self.navigationController.navigationBar;
UIImage *image = [UIImage imageNamed:#"bodyBg.png"];
[navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
I have attached the screenshot of the current output. Is this because of the large image size?
But my desired output is
First, make your navigation bar image size 1024x44 pixels nad for retina display 2048x88 pixels.
If you have the same image for UINavigationBar on every view controller, put this to AppDelegate in method didFinishLaunchingWithOptions:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"nav-background.png"] forBarMetrics:UIBarMetricsDefault];
// This will remove shadow in iOS6
if ([[UINavigationBar class] instancesRespondToSelector:#selector(shadowImage)]) {
[[UINavigationBar appearance] setShadowImage:[[[UIImage alloc] init] autorelease]];
}
And also I see you need custom back button, also put this in AppDelegate:
UIImage *backButtonNormal = [UIImage imageNamed:#"nav-back.png"];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonNormal forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
you can set the color of navigation bar same as your image use below code and you can put image name which you want to show in navigation bar
self.navigationController.navigationBar.tintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"bodyBg.png"]];
try it out with this code..
if ([navBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)])
{
[navBar setBackgroundImage:[UIImage imageNamed:#"Nav.png"] forBarMetrics:UIBarMetricsDefault];
}
else
{
UIImageView *imageView = (UIImageView *)[navBar viewWithTag:1];//any tag
if (imageView == nil)
{
imageView = [[UIImageView alloc] initWithImage:
[UIImage imageNamed:#"Nav.png"]];
[navBar insertSubview:imageView atIndex:0];
[imageView release];
}
}
see my full answer from this link setting-title-for-uinavigation-bar-in-iphone
self.navigationController.navigationBar.tintColor = [UIColor clearColor];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"navBar"] forBarMetrics:UIBarMetricsDefault];
I hope , it will work for u
Use UINavigationBar's setBackgroundImage: forBarMetrics: method :
[self.navigationController.navigationBar setBackgroundImage:yourImageHere forBarMetrics:UIBarMetricsDefault];
your UIImage should be of appropriate height and width. For example iphone 4 retina size would be 640*88 for portrait.
EDIT : Point here is if UIImage height is bigger say 150 pixel height it will display that much height and our UINavigationBar height is 44 pixel.
check this link for add background image in navigationbar and first you your navigationbar
image size set as navigationbar.
check this link here
i hope this code useful for you.
[myNavbar setBackgroundImage:[UIImage imageNamed: #"UINavigationBarBackground.png"]
forBarMetrics:UIBarMetricsDefault];
and also this link
Try this code it works for me:
UIView* navigationBGView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UIImage * targetImage = [UIImage imageNamed:#"Header_top.png"];
UIGraphicsBeginImageContextWithOptions(navigationBGView.frame.size, NO, 0.f);
[targetImage drawInRect:CGRectMake(0.f, 0.f, navigationBGView.frame.size.width, navigationBGView.frame.size.height)];
UIImage * resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
navigationBGView.backgroundColor = [UIColor colorWithPatternImage:resultImage];
[self.navigationController.navigationBar addSubview:navigationBGView];
[self.navigationController.navigationBar setTintColor:[UIColor clearColor]];

IOS Custom UIBarbuttonItem change position in detail view

I'm trying to customize my navigationbar's UIBarbuttonItem so that I'm using this im my Appdelegate.m
// didFinishLaunchingWithOptions:
{
UIImage *navBarImage = [UIImage imageNamed:#"nav-bar.png"];
[[UINavigationBar appearance] setBackgroundImage:navBarImage
forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:5
forBarMetrics:UIBarMetricsDefault];
UIImage *barButtonSave = [[UIImage imageNamed:#"nav-bar-button.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 2, 0, 2)];
[[UIBarButtonItem appearance] setBackgroundImage:barButtonSave
forState:UIControlStateNormal
style:UIBarButtonItemStyleDone
barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackgroundVerticalPositionAdjustment:5 forBarMetrics:UIBarMetricsDefault];
return YES;
}
When I'm done with this I just place a UIBarbuttonItem with Storyboard and I'm done.
The point is I noticed that the "Save" button changes between my views!
It looks like is moving down few pixels in my DetailView:
Why is That?
I still don't know why. but I solved giving my custom UIbarbuttonItem image the 30px default height.
[see similar issue UIBarButtonItem shifting downwards after a UINavigationController push

Resources