How to place UINavigationBarButton in custom location - ios

My current navigation bar looks like this:
As you can see I need to move it to the right. Not sure that I'm setting this up correctly, any suggestions?
Here is the code:
-(void)customizeNavigationBar{
//*** Nav bar Background ***
//Image being used
UIImage *backgroundImage = [UIImage imageNamed:#"navBarBackground"];
//Set navigation bar image
[self.navigationController.navigationBar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];
//*** Search button**
//Get Image
UIImage* searchImage = [UIImage imageNamed:#"navSearchButton"];
//Set frame
CGRect frame = CGRectMake(0, 0, searchImage.size.width, searchImage.size.height);
//Create button
UIButton *searchButton = [[UIButton alloc] initWithFrame:frame];
//Set background images (selected and not)
[searchButton setBackgroundImage:searchImage forState:UIControlStateNormal];
[searchButton setBackgroundImage:[UIImage imageNamed:#"navSearchButtonSelected"] forState:UIControlStateSelected];
//Add action
[searchButton addTarget:self action:#selector(search)
forControlEvents:UIControlEventTouchUpInside];
//Set highlight on
[searchButton setShowsTouchWhenHighlighted:YES];
//Create nav buttons
UIBarButtonItem *navSearchBtn =[[UIBarButtonItem alloc] initWithCustomView:searchButton];
//Add to nav bar
[self.navigationItem setLeftBarButtonItem:navSearchBtn];
}

Use setImage:forState: for setting background image to UIButton (instead of setBackgroundImage:)
Set UIButton's frame a bit larger (size.width)

Related

It's hard to click on Bar button Item

I've created custom back bar button. And I added an UIview because I have to change position of it within bar.
UIImage *backButtonImage = [UIImage imageNamed:#"backBtnWhite"];
_backButton = [UIButton buttonWithType:UIButtonTypeCustom];
_backButton.exclusiveTouch = YES;
[_backButton setImage:backButtonImage forState:UIControlStateNormal];
[_backButton setImage:backButtonImage forState:UIControlStateHighlighted];
[_backButton addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
_backButton.frame = CGRectMake(-4.0, 7.0, 40, 40);
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(-4.0, 7.0, backButtonImage.size.width, backButtonImage.size.height)];
[view addSubview:_backButton];
UIBarButtonItem* barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:view];
self.navigationItem.rightBarButtonItem = barButtonItem;
But after that I've added UIView it became difficult to click on it.
I've tried to change button area, but it didn't give results.
It seems like you specify incorrect frame for _button or/and view and this changes the tap region. Try to set different background color for _backButton and view. This will show you if frames are in positions that you expect.
If you try to adjust image position by specifying these frames - its more correct to use properties of UIButton like imageEdgeInsets and titleEdgeInsets.

Remove text from iOS navigation back bar button.

I want to remove the text from the navigation bar left button but I've looked everywhere and can't find anything. I want the button to just display an image and it does that but with the text over laying it. How can I remove the text?
UIImage* image = [UIImage imageNamed:#"yourImage"];
CGRect frameimg = CGRectMake(0, 0, image.size.width, image.size.height);
UIButton *yourButton = [[UIButton alloc] initWithFrame:frameimg];
[yourButton setBackgroundImage:image forState:UIControlStateNormal];
UIBarButtonItem *button =[[UIBarButtonItem alloc] initWithCustomView:yourButton];
self.navigationItem.rightBarButtonItem=button;
just set
self.navigationItem.title = #"";
in the previous viewcontroller

Custom navigation bar back button with only image

I want to show custom navigation bar back button. I want to show only image on it. I want to use this button in whole app also dont want to create this button in each file. How can i??
Here is my code:
// Set the custom back button
UIImage *buttonImage = [UIImage imageNamed:#"back_arrow.png"];
//create the button and assign the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
//set the frame of the button to the size of the image (see note below)
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self action:#selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
//create a UIBarButtonItem with the button as a custom view
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
but this is showing on current page only.
Another code:
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[UIImage imageNamed:#"back_arrow.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
Using this code back button image is showing in whole app. But the text "Back" is also showing. How can i solve this problem.
Thanks in advance.
I've used a category in the past to do this.
Create a category on UIBarButtonItem called +projectButtons (or something).
Then you can have a function like...
+ (UIBarButtonItem)backArrowButtonWithTarget:(id)target action:(SEL)action
{
UIImage *buttonImage = [UIImage imageNamed:#"back_arrow.png"];
//create the button and assign the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
//set the frame of the button to the size of the image (see note below)
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
//create a UIBarButtonItem with the button as a custom view
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
return customBarItem;
}
Then add it to you navigation bar like so...
self.navigationItem.leftBarButtonItem = [UIBarButtonItem backArrowButtonWithTarget:self action:#selector(popViewControllerAnimated:)];
This means you only need one line of code to create it but you still get the customisation of the target and action in each VC that you use it in. If you decide to change the look of the button then it's all done in one place.

Custom UIBarButtonItem

I am trying to create a custom UIBarbuttonItem that uses just a png with transparency so that I only have an icon as button. When I try to set the button image, set the background as white, and set the style to Plain I still get an inner shadow and black border around it.
What gives?
I have tried the below code and it still puts the black border around it.
UIImage *background = [UIImage imageNamed:#"Dismiss_normal.png"];
UIImage *backgroundSelected = [UIImage imageNamed:#"Dismiss_selected.png"];
self.closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.closeButton addTarget:self action:#selector(closeButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; //adding action
[self.closeButton setBackgroundImage:background forState:UIControlStateNormal];
[self.closeButton setBackgroundImage:backgroundSelected forState:UIControlStateSelected];
self.closeButton.frame = CGRectMake(0 ,0,background.size.width, background.size.height);
self.closeButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.closeButton];
self.navigationItem.leftBarButtonItem = self.closeButtonItem;
What I noticed is if I do a modal segue the button, with the code above still has a black border around it, but if I do a push segue it doesn't? WTF?
You must set the button type to Custom and the icon image to the button background.
example code:
UIImage *background = [UIImage imageNamed:#"icon.png"];
UIImage *backgroundSelected = [UIImage imageNamed:#"icon_selected.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:#selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside]; //adding action
[button setBackgroundImage:background forState:UIControlStateNormal];
[button setBackgroundImage:backgroundSelected forState:UIControlStateSelected];
button.frame = CGRectMake(0 ,0,35,35);
Then set this button to your BarButtonItem like this:
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = barButton;
I understood your problem.
When you actually push a UIViewController it will be a part of existing UINavigationController. So the UINavigationBar retains. But when you use ModalViewController its just another UIViewController kind of "added" to your existing UIViewController. So the UINavigationBar won't be there.
But you can add another UINavigationBar from xib and add a UIButton. But don't directly add the UIButton, if you do so it will automatically convert to UIBarButton making it bordered.
So first drag a normal UIButton to your UIView(not navigation bar). Change it to custom and add UIImage to it using Attribute Inspector. Then drag that image to your custom created UINavigationBar. It will work as I tried it just now. You can see the screen shot attached below.
This will update ALL back buttons and navigation bars throughout the app.
I have used the Following code to set Custom Navigation back button
UIImage *imgBack = [[UIImage imageNamed:#"nav_back.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(-6, 22, -6, 0)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:imgBack forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
and the Following code for Setting background image on navigation bar.
UIImage *navBackgroundImage = [UIImage imageNamed:#"nav_bar.png"];
[[UINavigationBar appearance] setBackgroundImage:navBackgroundImage forBarMetrics:UIBarMetricsDefault];
Try use [UIBarButtonItem initWithCustomView]
UIImage *menuButtonImage = [UIImage imageNamed:#"list.png"];// set your image Name here
UIButton *btnToggle = [UIButton buttonWithType:UIButtonTypeCustom];
[btnToggle setImage:menuButtonImage forState:UIControlStateNormal];
btnToggle.frame = CGRectMake(0, 0, menuButtonImage.size.width, menuButtonImage.size.height);
UIBarButtonItem *menuBarButton = [[UIBarButtonItem alloc] initWithCustomView:btnToggle];
self.navigationItem.leftBarButtonItem = menuBarButton;
I personally like FlatUIKit's approach by just flattening out the entire UI through UIAppearance
Essentially, you'd do something like:
UIImage *backButtonPortraitImage = [UIImage backButtonImageWithColor:color barMetrics:UIBarMetricsDefault cornerRadius:cornerRadius];
UIImage *highlightedBackButtonPortraitImage = [UIImage backButtonImageWithColor:highlightedColor barMetrics:UIBarMetricsDefault cornerRadius:cornerRadius];
[appearance setBackButtonBackgroundImage:backButtonPortraitImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[appearance setBackButtonBackgroundImage:highlightedBackButtonPortraitImage forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
UIImage *buttonImageNormal = [UIImage imageWithColor:color cornerRadius:cornerRadius];
UIImage *buttonImageHightlighted = [UIImage imageWithColor:highlightedColor cornerRadius:cornerRadius];
[appearance setBackgroundImage:buttonImageNormal forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[appearance setBackgroundImage:buttonImageHightlighted forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
Try this code..
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *btnImage = [UIImage imageNamed:#"bg"];
[button setImage:btnImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, btnImage.size.width, btnImage.size.height);
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
UIImage *icon = [UIImage imageNamed:#"icon"]
UIImageView *iconView = [[UIImageView alloc] initWithImage:icon];
iconView.frame = CGRectMake(0, 0, icon.size.width, icon.size.height);
[button addSubview:iconView];
iconView..userInteractionEnabled=NO;
[iconView centerBothDirections];
[iconView release];
UIBarButtonItem *barbtn = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
self.navigationItem.leftBarButtonItem =barbtn

Can set background image for UIBarButtonItem in nav bar, but not bottom toolbar

So this works when adding the item to the nav bar, but when i add it to the toolbar set in the bottom bar via interface builder, the background image doesn't show up.
UIBarButtonItem *resetButtonItem = [[UIBarButtonItem alloc]initWithTitle:#"Reset" style:UIBarButtonItemStylePlain target:self action:#selector(resetCriteria:)];
UIImage *img = [UIImage imageNamed:#"someImage.png"];
img = [img stretchableImageWithLeftCapWidth:5 topCapHeight:20];
[resetButtonItem setBackgroundImage:img forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
self.toolbarItems = [NSArray arrayWithObjects: resetButtonItem, nil];
Not only does the background not appear, none of the other behaviors work as well (but they work fine when adding these barbutton items to the nav bar)
You should try the following method to do the same.
Setting custom view in UIBarButtonItem.
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0, 0, 30, 30);
[btn setImage:[UIImage imageNamed:#"someImage.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(resetCriteria:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *Item = [[UIBarButtonItem alloc] initWithCustomView:btn];
self.toolbarItems = [NSArray arrayWithObject:Item];
[Item release];
We can set any view in UIBarButtonItem with CustomView init method.
We could set the ImageView. But ImageView doesnot handle UIControl Events.

Resources