How to change the background image of tab bar in Objective-C? - ios

I am develop in objective-C. I want to change the tab bar background like the following picture:
And the code is like the following:
UIImage *tabBarBackground = [UIImage imageNamed:#"tabbaritem_background.png"];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
But after setting the background image , the background is not at the correct place like the following:
The background image should be place at the bottom like the background in above picture.
Did I missing something ? Can someone help me ?
Thanks in advance.

I think is somewhere you go wrong, check if is this steps:
In the storyboard change the ViewController's background color for test.
Embed the ViewController in Tab Bar Controller
In the ViewController.m you can set the tabbar bacground color:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[UITabBar appearance] setBackgroundColor:[UIColor grayColor]]; // Here you can set the converted color form image, make sure the imageSize fit.
}
The result is below:

I think the method - (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode you can try, because your backgroudimage's size is not equal to tabbar'size.

Try this method to change your image to a ScaleImage.
+(UIImage *)getScaleImageNamed:(NSString *)name{
UIImage *nomalImage = [UIImage imageNamed:name];
CGFloat hInset = floorf(nomalImage.size.width / 2);
CGFloat vInset = floorf(nomalImage.size.height / 2);
UIImage *res = [nomalImage resizableImageWithCapInsets:UIEdgeInsetsMake(vInset, hInset, vInset, hInset)];
return res;
}

Steps you may miss, hope that can help.
Make sure you have imported the background image (e.g. in Assets.xcassets)
Use resizableImageWithCapInsets: to resize the background image
Put the UIAppearance settings in AppDelegate.m:
[[UITabBar appearance] setBackgroundImage:[[UIImage imageNamed:#"tabbaritem_background.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)]];

Related

How to customize my UITabBar. White icons on gradient background

iOS 7.0.
I need to make your design UITabBar. The picture (this link) shows how it looks now and how it should look. The difference is that:
1) The images on the buttons of some dirty color, and should be white
2) The selected item is highlighted in a little darker background color. Now he does not highlighted at all.
Here's how I define design in AppDelegate.m in application:didFinishLaunchingWithOptions
// Background image for TabBar
UIImage *backgroundDownImage = [UIImage imageNamed:#"background_320_49.png"];
// Mode UIImageResizingModeStretch
backgroundDownImage = [backgroundDownImage resizableImageWithCapInsets:UIEdgeInsetsZero resizingMode:UIImageResizingModeStretch];
// Set background image for all UITabBar in application
[[UITabBar appearance] setBackgroundImage:backgroundDownImage];
// White color for TabBar buttons
[[UITabBar appearance] setTintColor:[UIColor whiteColor]];
// Special font for all UITabBarItem
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:#"Xnjslkpqqovrwqxfnmropbgqtzp" size:0], UITextAttributeFont, nil] forState:UIControlStateNormal];
I tried the 4 variants:
1) In the method - (id)initWithNibName old code setFinishedSelectedImage + withFinishedUnselectedImage
2) In the method - (id)initWithNibName new code setImage + SetSelectedImage
3) In the method - (void)viewDidLoad old code
4) In the method - (void)viewDidLoad new code
- (void)viewDidLoad
{
[super viewDidLoad];
// Variant 1
[self.tabBarItem setImage:[[UIImage imageNamed:#"Live.png"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal]];
[self.tabBarItem setSelectedImage:[[UIImage imageNamed:#"Live.png"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal]];
// Variant 2
//[self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:#"Live.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"Live.png"]];
[CommonUse showLiveVideoFromController:self];
}
In all cases, icons will be displayed. Method initWithNibName (options 1 and 2) no action at all on the color icon has not. Method viewDidLoad work both old and new code - but in a strange way. When you first run the application, only the first icon is white, and all the other gray. If you click on any other icon and then go back to the first, this icon becomes white (but only a picture! Text is still gray).
It turns out that the color of icons affects only method viewDidLoad but not entirely - something else is missing.
Maybe draw two sets of icons for Selected and NoSelected. That was in the icon and image and text description as graphic. But still remain two points: 1) When you first start the application, all the icons will be gray, and only the first will be white. 2) When you change the width of the screen, do not disperse if the buttons.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:#"partners_hover.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"partners_small.png"]];
}
return self;
}
To change a color for unselected items (icons) you can use UITabBar property unselectedItemTintColor
[UITabBar appearance].unselectedItemTintColor = [UIColor anyColor];

UIbarbutton blue instead of background image

i'm trying to add a back button using a custom png file for the background, but every time i add the background using the storyboard it just become blue like this:
How can i add a background image on a UIbarbutton?
the back button look like this:
This is the standard behavior in iOS 7 for an image in a button. The image is rendered as a template image, with opaque areas colored the current tint color, and transparent areas, transparent. If you want to see the image, you need to create the image with imageWithRenderingMode: and pass UIImageRenderingModeAlwaysOriginal as the argument.
You will need to do it grammatically.
I have tried doing it in storyboard, and it looks like there is a really strange bug, that causes the developer to decide - either use text or use an image, not both....
- (void)viewDidLoad
{
[super viewDidLoad];
[self addButtonsToNavigationBar];
}
- (void)addButtonsToNavigationBar
{
UIButton *regularButton = [[UIButton alloc] initWithFrame: CGRectMake(0, 0, 100.0f, 30.0f)];
UIImage *historyButtonImage = [UIImage imageNamed:#"image_name.png"];
// can set the background color instead of setting an image.
[regularButton setBackgroundImage:historyButtonImage forState:UIControlStateNormal];
[regularButton setTitle:#"Some button name" forState:UIControlStateNormal];
[regularButton addTarget:self action:#selector(historyButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *navigationBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:regularButton];
self.navigationItem.leftBarButtonItem = navigationBarButtonItem;
}

View background as the current set background image?

I'm developing an app which takes a common background for most of the views displayed on the screen, so let's say I have this background image for my app:
Now imagine I add a table view and I want my table view section view background to be the app background image specific part it should match in its current position. How do I achieve that effect? I tried setting alpha to 0 but obviously this is what happens when I scroll my table:
What I want it to look like always:
What happens when I scroll:
I also tried setting background image layer's mask property but since it only works for one view layer it's not the right thing to do in this case.
I know I'm not explaining myself really well, but I hope you understand at least what I want to do.
You can do this by cutting out the part of the image you need then setting it has the background for the nav bar.
Here is a category to cut out part of the image:
.h
#import <UIKit/UIKit.h>
#interface UIImage (Crop)
-(UIImage*)cropFromRect:(CGRect)fromRect;
#end
.m
#import "UIImage+Crop.h"
#implementation UIImage (Crop)
-(UIImage*)cropFromRect:(CGRect)fromRect
{
fromRect = CGRectMake(fromRect.origin.x * self.scale,
fromRect.origin.y * self.scale,
fromRect.size.width * self.scale,
fromRect.size.height * self.scale);
CGImageRef imageRef = CGImageCreateWithImageInRect(self.CGImage, fromRect);
UIImage* crop = [UIImage imageWithCGImage:imageRef scale:self.scale orientation:self.imageOrientation];
CGImageRelease(imageRef);
return crop;
}
#end
Then in your viewDidLoad:
//Cut out part of image where nav bar sits
UIImage *navBG = [[UIImage imageNamed:#"cVyuX.png"] cropFromRect:CGRectMake(0, 20, 320, 44)];
//Set nav bar image
[[UINavigationBar appearance] setBackgroundImage:navBG forBarMetrics:UIBarMetricsDefault];
//Remove gray seperator
[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
I was able to produce this:
Set your tableView background to be clearColor and do the same for your tableViewCells. Then you must hide the separator lines.
tableView.backgroundColor = [UIColor clearColor];
//in your tableView cellForRowPath method
cell.backgroundColor = [UIColor clearColor];
cell.separatorStyle = UITableViewCellSeparatorStyleNone;

How do I an image at a specific location in a navigation bar?

Im customizing my navbars so they all have a logo in them. I want to add the logo right around here:
I know navbars have a background image property but that would stretch it to the whole navbar. I just want it there! :)
So far Ive tried this:
UIImage *gradientImage44 = [[UIImage imageNamed:#"NavBar25High"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
UIImage *gradientImage32 = [[UIImage imageNamed:#"NavBar25High"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
// Set the background image for *all* UINavigationBars
[[UINavigationBar appearance] setBackgroundImage:gradientImage44 forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:gradientImage32 forBarMetrics:UIBarMetricsLandscapePhone];
But thats for gradients because as I mentioned, it uses the image as a background image :(
I would allocate an ImageView using CGRectMake to the specific location you want it and add that image view to the view of the UINavigationController.
Like this:
Declare the image
UIImage *myImage =[UIImage imageNamed:#"header.png"];
Allocate the image view
myImageView_tools = [[UIImageView alloc] initWithFrame:CGRectMake(97.5, 20, 125, 45)];
Set the image as the image of the imageview
myImageView_tools.image = myImage;
Add this to your navbar.
[nameofyournavbar.view addSubview:myImageView_tools];
In my code I happened to add it to the RootViewController instead of the NavigationBar but I would imagine it would work just the same.
How about adding the image as the custom view of a UIBarButtonItem. Maybe using a UIBarButtonSystemItemFixedSpace item with a specified width.

Adding a background image to UINavigationBar covers the title

I have successfully added a background image to my UINavigationBar with the following code:
UIImageView *barView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"subHeader_lg.png"]];
[calculatorBar addSubview:barView];
[barView release];
However, with this method, the title of the view is covered by the image.
If I navigate further into the app, and then back, the title appears on top of the background image like so:
Any ideas how I can get the title to appear on top from the beginning?
I have tried pushing the barView to the back, but that makes it hidden behind everything else.
EDIT:
It seems that the custom draw function is the accepted answer, but I am unable to get the draw function to be called. I have this code at the bottom of my appdelegate.m file
#implementation UINavigationBar (UINavigationBarCustomDraw)
- (void)drawRect:(CGRect)rect
{
UIImage *image = [UIImage imageNamed: #"subHeader_lg.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
When you call "addSubview" it adds it above any view that have already been added, thus covering the title.
What you want is
[calculatorBar insertSubview:barView atIndex:0];
However, this won't make it "stick" on subsequent pushes, so use the methods described at http://www.developers-life.com/custom-uinavigationbar-with-image-and-back-button.html for a better solution.
Also in iOS 5, Apple has added a built in way to customize the nav bar see http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIAppearance_Protocol/Reference/Reference.html#//apple_ref/doc/uid/TP40010906
To set the background image on your navigation bar, use the following.
On iOS 5.x, use
[calculatorBar setBackgroundImage: barView.image forBarMetrics: 0];
On iOS 4.x use
calculatorBar.layer.contents = (id)barView.image.CGImage;

Resources