How to add Background image for Custom UIToolBar - ios

i am using below code to add background image to the Custom UItoolbar where it has 4 Custom UIBarButton it in.I want to add background image for the ToolBar i searched many things and tried but nothing is working. can anyone please tell me how to add background image for Custome UIToolBar for the Frame which i have specified.
UIToolBar *toolBar =[[UIToolBar alloc]init];
-(void)layoutSubviews{
CGRect frame;
frame = CGRectMake(20 ,90, 150, 30);
toolBar.frame = frame;
UIImage* toolbarImage = [UIImage imageNamed: #"toolbar_background.png"];
[[UIToolbar appearance]
setBackgroundImage: toolbarImage
forToolbarPosition: UIToolbarPositionAny
barMetrics: UIBarMetricsDefault];
}
The Above code is not working For me as mine is Custom Toolbar adding progrmatically and also i am adding it as per requirment.

I had the same problem and now, it is working fine. You can try it, if its works fine,
1.First i set the tool bar hidden to NO.
[self.navigationController setToolbarHidden:NO animated:YES];
2.addin Image to UItoolbar
UIImage *toolbarImage = [[UIImage imageNamed:#"search_BottomBar1.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UIToolbar appearance]setBackgroundImage:toolbarImage forToolbarPosition:UIToolbarPositionBottom barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance]setTintColor:[UIColor grayColor]];
i added the bar Button to it.
UIBarButtonItem *button2 = [[UIBarButtonItem alloc]initWithTitle:#"My Place"
style:UIBarButtonItemStyleBordered target:self action:#selector(clickedButton1)];
NSArray *itemsN = [NSArray arrayWithObjects:button0,button1,button2, nil];
[self setToolbarItems:itemsN animated:NO];
Try this, Hopefully it will works..

Related

controller navigationItem.leftBarButtonItem changes color in IOS7

I have an image that is assigned to the navigationItem.leftBarButtonItem on IOS7. When I get it from the designer it looks fine on the navigation bar's background. But when I implement it in IOS7 it becomes pale and almost disappears.
This is how it's being set up:
UIBarButtonItem *button = [[barButtonItemClass alloc] initWithImage:[UIImage imageNamed:#"nav_menu_icon.png"] style:UIBarButtonItemStyleBordered target:self action:#selector(showLeft:)];
viewController.navigationItem.leftBarButtonItem = button;
This is how it should look (image came from my designer):
And this is how it looks when implemented (on the simulator or a phone):
What's the solution?
You should set the tint color of the navigation bar to tint all items in the bar.
A nice solution is to use the UIAppearanceprotocol introduced in iOS 5.
In you AppDelegate's applicationDidFinishLaunching method put the following code:
[[UINavigationBar appearance] setTintColor: [UIColor whiteColor]];
Note, I am currently not able to try the code.
Cheers!
this works fine for me :
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithImage:backButtonImage style:UIBarButtonItemStyleBordered target:self action:#selector(backBtnAction)];
customItem.imageInsets = UIEdgeInsetsMake(0.0, -10, 0, 0);// For adjusting the image
[customItem setBackgroundVerticalPositionAdjustment:+3.0f forBarMetrics:UIBarMetricsDefault];
[self.navigationItem setHidesBackButton:YES];// Hide the Default back button before set custom
[self.navigationItem setLeftBarButtonItem: customItem];
Hopefully, causes might be with the Tint
Try this, may help you ;
// Solve your frame issue.
UIImage * bgImg = [UIImage imageNamed:#"nav_menu_icon.png"];
UIButton *leftButton = [[UIButton alloc] initWithFrame: CGRectMake(x, y, bgImg.size.width, bgImg.size.width)];
viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
Also change the tintColour to clear colour.

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];

My app displaying 2 toolbars instead of one

When i testing my app through xcode in multiple devices, my app shows one toolbar as expected. After that i have uploaded the update of my app, in app store. But suddenly, I realized that in some iphones (ios 7.0.4), after app's update, on launching it cames with 2 toolbars.
I am adding the toolbar programmatically in viewDidLoad function of this controller.
UIImage* leftImg = [UIImage imageNamed:#"left.png"];
UIImage* rightImg = [UIImage imageNamed:#"right.png"];
CGRect frame = CGRectMake(0, 0, leftImg.size.width, leftImg.size.height);
UIButton* lefButton = [[UIButton alloc] initWithFrame:frame];
UIButton* rigButton = [[UIButton alloc] initWithFrame:frame];
[lefButton setTitle:#"" forState:UIControlStateNormal & UIControlStateHighlighted];
[rigButton setTitle:#"" forState:UIControlStateNormal & UIControlStateHighlighted];
[lefButton setImage:leftImg forState:UIControlStateNormal];
[lefButton setImage:leftImg forState:UIControlStateSelected];
[rigButton setImage:rightImg forState:UIControlStateNormal];
[rigButton setImage:rightImg forState:UIControlStateSelected];
[lefButton addTarget:self action:#selector(loadPrevChapter:) forControlEvents:UIControlEventTouchUpInside];
[rigButton addTarget:self action:#selector(loadNextChapter:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *itemLeft = [[UIBarButtonItem alloc] initWithCustomView:lefButton];
UIBarButtonItem *itemRight = [[UIBarButtonItem alloc] initWithCustomView:rigButton];
// In case i want to add Space between barbuttonitems
UIBarButtonItem *flexiableItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
// add items to toolbar
NSArray *items = [NSArray arrayWithObjects:itemLeft, flexiableItem, itemRight, nil];
self.toolbarItems = items;
[self.navigationController setToolbarHidden:NO animated:NO];
UIImage *toolbarBgImage = [UIImage imageNamed:tlbImg];
UIImage *navbarBgImage = ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0f) ? [UIImage imageNamed:navImgIos7] : [UIImage imageNamed:navImg];
[[UINavigationBar appearance] setBackgroundImage:navbarBgImage forBarMetrics:UIBarMetricsDefault];
[[UIToolbar appearance] setBackgroundImage:toolbarBgImage forToolbarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
But i haven't added at navigation controller the toolbar this:
[[self navigationController].view addSubview:_toolbar];
as #Xeieshan said below.
Has anyone notice something like this before or does anyone know why this happening?
Screenshot of my app running on my iPhone 5 (v7.0.4)
Screenshot of my app, installed through appstore, after update, running on an iPhone 5 (v7.0.4)
[[self navigationController].view addSubview:toolbar]; This is how to add UIToolbar in UINavigationController but i cannot see your code where you do it?
I think you are adding UIToolbar on both UIViewController and UINavigationController.
I'd advise against adding the toolbar to UINavigationController.view, as it breaks encapsulation (even though that's not enforced very much in UIKit).
Instead, add a custom root view controller containing the toolbar and a contained UINavigationController. This also allows you to properly layout the toolbar so that it doesn't cover the views in the navigation controller.

how to remove uibaritem background image in IOS?

Hi i used this code to set the background image for my bar button. I put it in "AppDelegate.h"
//-- set bar button image
UIImage *barButtonImage = [[UIImage imageNamed:#"bar-button"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 6, 0, 6)];
[[UIBarButtonItem appearance] setBackgroundImage:barButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
It worked fine but in my Search Controller I dont want to use it, because it make my "search" button look very ugly. Is there anyway to remove the background image in search controller only ? Plz helpe me. Thanks!
In your SearchController.m try
[[UIBarButtonItem appearance] setBackgroundImage:nil forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
if you want to remove background image of Search controller
[[searchBar.subviews objectAtIndex:0] removeFromSuperview];
and you can add clear background image to it.
UIImageView *searchImage=[[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 319.0, 44.0)];
[searchImage setImage:[UIImage imageNamed:#"searchbg.png"]];
[searchBar insertSubview:searchImage atIndex:1];
You can set where you want the customization, for instance in tis snippet I only want it in UINavigationBar.
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil]setBackgroundImage:barButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
Not checked in xcode but the concept is OK. Here the doc + (instancetype)appearanceWhenContainedIn:(Class )ContainerClass,.... Great guide also here: NSHipster.
As you can see is a variadic arguments API, so you can set more classes
PS: I don't know if it is a wanted effect, but the kind of reflection is due to wrong insets in the image.
[searchBarObject setImage:nil forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
This work for me.. I hope it will be helpful for you..
Updated full code..
.m file contents in viewDidLoad
//Table initialization..........................................................................
my_table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height)];
//SearchBar initialization......................................................................
UISearchBar *search = [UISearchBar new];
[search sizeToFit];
search.delegate = self;
//Make the image in size 20x20................................................................
UIImage *img = [UIImage imageNamed:#"apple.jpg"];
UIGraphicsBeginImageContextWithOptions(CGSizeMake(20, 20), YES, 0);
[img drawInRect:CGRectMake(0, 0, 20, 20)];
UIImage *img2 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//Set the image for the UISearchbarIconSearch...................................................
[search setImage:img2 forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
search.barStyle = UIBarStyleBlackTranslucent;
[my_table setTableHeaderView:search];
[my_table reloadData];
//SearchDisplay initialization..................................................................
UISearchDisplayController *display = [[UISearchDisplayController alloc]initWithSearchBar:search contentsController:self];
sbc = display; //
display.delegate = self;
display.searchResultsDataSource = self;
display.searchResultsDelegate = self;
my_table.dataSource = self;
my_table.delegate = self;
[self.view addSubview:my_table];
.h file.
#interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchDisplayDelegate>
{
NSArray *table_array;
UITableView *my_table;
UISearchDisplayController *sbc;
NSArray *filtered;
}
#end
This is my code which change the search icon image...

Trouble adding custom button to navigation controller's toolbar

I have a tableview controller that has a navigation controller. I have added a toolbar to the bottom of the view and now I need to add a custom button. I have the following code (from viewDidLoad), which is producing strange results. When the view loads, I see the custom button for a brief second, then an empty toolbar. At the moment I am only trying to get one custom button to load.
self.navigationController.toolbar.barStyle = UIBarStyleBlackTranslucent;
[self.navigationController.toolbar sizeToFit];
CGFloat toolbarHeight = [self.navigationController.toolbar frame].size.height + 20;
[self.navigationController.toolbar setFrame:CGRectMake(CGRectGetMinX(self.view.bounds),
CGRectGetMaxY(self.view.bounds) - toolbarHeight,
CGRectGetWidth(self.view.bounds),
toolbarHeight)];
UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithImage:[UIImage
imageNamed:#"stub_fav.png"]
style:UIBarButtonItemStylePlain
target:self action:#selector(addFavorite)];
[btn setTintColor:[UIColor whiteColor]];
[btn setImage:[UIImage imageNamed:#"stub_fav.png"]];
NSArray *toolbarItems = [[NSArray alloc] initWithObjects:btn, nil];
[self.navigationController.toolbar setItems:toolbarItems];
Any ideas? Thanks!
Im not sure what btnAddFave is for but it's not the subview of anything. I assume btw and btnAddFave should have the same background image, if so you spelled stub_fav.png wrong. shouldn't it be stub_fave.png

Resources