Can I change the position of navigationbar item? - iphone

Here is the snapshot:
And the code is here:
UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
leftButton.frame = CGRectMake(0.0f, 0.0f, 46.0f, 32.0f);
[leftButton setBackgroundImage:[UIImage imageNamed:#"navi_register_up.png"] forState:UIControlStateNormal];
[leftButton addTarget:self action:#selector(doRegister) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
self.navigationItem.leftBarButtonItem = leftBarItem;
[leftBarItem release];
Is there any way to move the button up some px?
Thank you:)

You need to create a UIBarButtonItem with system item UIBarButtonSystemItemFixedSpace with -5 width which will remove 5 px padding. Then set leftBarButtonItems with both fixed space item and your back bar item like this -
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[negativeSpacer setWidth:-5];
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacer,leftBarItem,nil];

You can try this,
leftButton.imageEdgeInsets = UIEdgeInsetsMake(-2, 0, 0, 0);

Add a UIBarButtonSystemItemFlexibleSpace item in between the items you want to separate.

Here's a Swift 3 version of the most voted answer:
let soundButton = UIBarButtonItem(image:image , style: UIBarButtonItemStyle.plain, target: self, action: #selector(self.soundPressed))
let negativeSpacer = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
negativeSpacer.width = -5
self.navigationItem.rightBarButtonItems = [negativeSpacer,soundButton]

The height of a standard bar button is 29pt. Do yourself a favor and just shrink leftbutton's height and navi_register_up.png's height instead.

You can use this code,I think this will be helpful to you.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0,0,50,32);
[button setBackgroundImage:[UIImage imageNamed:#"btn_back.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(BackBtnClicked) forControlEvents:UIControlEventTouchUpInside];
UIView *backButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 32)];
backButtonView.bounds = CGRectOffset(backButtonView.bounds, -1, -4);
[backButtonView addSubview:button];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButtonView];
//barButtonItem.imageInsets = UIEdgeInsetsMake(-6, 20, 30, 0);
[self.navigationItem setLeftBarButtonItem:barButtonItem];

I had a similar issue and found an option that sufficed in the storyboard. Just click on the navbar item and you should see the x/y position options.

Related

Custom button Image not visible on navigation bar iOS 7.1

I had added custom button on my navigation bar. here is the code now my problem is that in iOS7 I am able to see back Button image with text, while in iOS 7.1 the image is not displaying only text is displaying.
-(void)addBackButton{
self.navigationItem.hidesBackButton = YES;
[backButtonView removeFromSuperview];
if (backButtonView) {
[backButtonView release];
backButtonView = nil;
}
backButtonView = [[UIView alloc] initWithFrame:CGRectMake(0,6,70,30)];
NSString *strText = #"Back";
btnBack = [[UIButton alloc] initWithFrame:CGRectMake(2,0, 70, 31)];
[btnBack setTitle:strText forState:UIControlStateNormal];
btnBack.titleLabel.font = [UIFont fontWithName:#"HelveticaNeue-Medium" size:17];
btnBack.titleLabel.textColor = IOS7ColorBtnBackText;
[btnBack setImage:[UIImage imageNamed:#"Left_Arrow.png"] forState:UIControlStateNormal];
[btnBack addTarget:self action:#selector(btnBack:) forControlEvents:UIControlEventTouchUpInside];
[backButtonView addSubview:btnBack];
[btnBack release];
[self.navigationController.navigationBar addSubview:backButtonView];
}
Instead of adding it as subview in navigationView you can assign custom button to left or right button and this too works
UIButton *backbtn=[[UIButton alloc]initWithFrame:CGRectMake(20,5,42, 42)];
[backbtn setImage:[UIImage imageNamed:#"arrow.png"] forState:UIControlStateNormal];
[backbtn setTitle:#"Login" forState:UIControlStateNormal];
[backbtn addTarget:self action:#selector(didTapBackButton:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barbtn=[[UIBarButtonItem alloc]initWithCustomView:backbtn];
self.navigationItem.leftBarButtonItem=barbtn;
[self.navigationItem setHidesBackButton:YES];
This do work, but you made mistake here
[self.navigationController.navigationBar addSubview:backButtonView];
///// Your code to create custom button goes here
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]
initWithCustomView:BackButton];
Create custom button and add it in your navigation left bar button item as a custom view as follows:
let btnShowMenu = UIButton()
let image: UIImage = UIImage(named: "requiredImageName")!
btnShowMenu.setImage(image, for: UIControlState.normal)
btnShowMenu.frame = CGRect(x: 0, y: 0, width: 15, height: 15)
btnShowMenu.addTarget(self, action: #selector(addAnAction(_:)), for: UIControlEvents.touchUpInside)
let customBarItem = UIBarButtonItem(customView: btnShowMenu)
self.navigationItem.leftBarButtonItem = customBarItem;

Can't adjust spacing of UIBarButtonItem(s) when assigned to navigationItem.rightBarButtonItems

This is a weird problem.
The situation is that I want to adjust the spacing between my UIBarButtonItem(s) so that they are just 2 pixels apart.
I am able to do this quite easily with UIToolbar:
// Make bottom button bar buttons
NSMutableArray *bottomButtons = [[NSMutableArray alloc] initWithCapacity:3];
// Create spacer between buttons
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[bottomButtons addObject:spacer];
UIBarButtonItem* noSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
noSpace.width = -10;
// Add button 1
self.addAlbumButton = [UIGlossyButton buttonWithType:UIButtonTypeCustom];
[self.addAlbumButton setTitle:#"Button 1" forState:UIControlStateNormal];
[self.addAlbumButton addTarget:self action:#selector(addAlbum:) forControlEvents:UIControlEventTouchUpInside];
self.addAlbumBarButton = [[UIBarButtonItem alloc] initWithCustomView:self.addAlbumButton];
[bottomButtons addObject:self.addAlbumBarButton];
[bottomButtons addObject:noSpace];
// Add button2
self.downloadAllItemsButton = [UIGlossyButton buttonWithType:UIButtonTypeCustom];
[self.downloadAllItemsButton setTitle:#"Button 2" forState:UIControlStateNormal];
[self.downloadAllItemsButton addTarget:self action:#selector(downloadAllItemsAction:) forControlEvents:UIControlEventTouchUpInside];
self.downloadAllItemsBarButton = [[UIBarButtonItem alloc] initWithCustomView:self.downloadAllItemsButton];
[bottomButtons addObject:self.downloadAllItemsBarButton];
// add all button to bottom toolbar
[self.bottomToolbar setItems:bottomButtons];
The problem is when I try to do this with the navigationbar. For some reason, when I insert a fixed length button (with a negative value), it won't shrink the space between the buttons. I know the fixed length button is there and working because if I change the width to a positive number, the spacing between the buttons increases.
The code is basically the same except that instead of adding the buttons to self.bottomToolbar, I'm calling the following code:
self.navigationItem.rightBarButtonItems = bottomButtons;
I have found the same issue with the MasterViewController. I'm using a splitviewcontroller and the bottom toolbar works fine but the top toolbar has the same spacing problem. That problem, is I can't make the space between buttons less than the default.
It seems like navigationItem.rightBarButtonItems works differently than all other toolbars.
after adding all navigationButtonItem in to an array you can do it like the following way. i guess this will help you also coz this works for me and if you want to change space between button you can change it with help of macro
#define ONE_BUTTON_WIDTH 30.0f
#define SPACE_BETWEEN_BUTTONS 12.0f
#define ONE_BUTTON_TOTAL_WIDTH (ONE_BUTTON_WIDTH + SPACE_BETWEEN_BUTTONS)
#define kBookmarksImage [UIImage imageNamed:#"bookmarks.png"]
/* CREATE BOOKMARKS BUTTON */
UIButton *bookmarksButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, kBookmarksImage.size.width, kBookmarksImage.size.height)];
[bookmarksButton setImage:kBookmarksImage forState:UIControlStateNormal];
[bookmarksButton addTarget:target action:#selector(toolbarButtonTapped:) forControlEvents:UIControlEventTouchDown];
bookmarksButton.tag = kBookmarksButtonTag;
UIBarButtonItem *bookmarksButtonItem = [[UIBarButtonItem alloc] initWithCustomView:bookmarksButton];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
spacer.width = SPACE_BETWEEN_BUTTONS;
[buttons addObjectsFromArray:#[bookmarksButtonItem,spacer]];
/* ADD ALL THESE BUTTONS TO CUSTOM TOOLBAR AND TOOLBAR TO NAVIGATION BAR */
UIToolbar *customToolbar = [[UIToolbar alloc]
initWithFrame:CGRectMake(0.0f, 0.0f, ([bottomButtons count]/2*ONE_BUTTON_TOTAL_WIDTH), 44.01f)]; // 44.01 shifts it up 1px for some reason
customToolbar.clearsContextBeforeDrawing = NO;
customToolbar.clipsToBounds = NO;
customToolbar.tintColor = [UIColor colorWithWhite:0.305f alpha:0.0f]; // closest I could get by eye to black, translucent style.
customToolbar.barStyle = -1; // clear background
[customToolbar setItems: bottomButtons animated:NO];
UIBarButtonItem *customUIBarButtonitem = [[UIBarButtonItem alloc] initWithCustomView:customToolbar];
self.navigationItem.rightBarButtonItem = customUIBarButtonitem;
Easiest way just create a method to set up UIButton for each UIBarButtonItem like so:
- (UIBarButtonItem*)createAddAlbumBarButtonWithImage:(NSString*)imageName
{
UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
[button addTarget:self action:#selector(addAlbum:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem* barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
return barButton;
}

Make custom back button's clickable area smaller in Navigation controller

I've created a custom back button with the code below, however the clickable area is very big and goes well beyond the icon itself. Does anyone know how to set the clickable area, or make it the same size as the image?
Thanks
UIImage *buttonImage = [UIImage imageNamed:#"prefs"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self action: #selector(handleBackButton)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
The clickable area is shown in red.
Thanks!
If you want to prevent the click other than the button then add the custom button to UIView then set that view as custom view to the barbuttonItem
Your code will become like this :
UIImage *buttonImage = [UIImage imageNamed:#"prefs"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self action: #selector(handleBackButton)
forControlEvents:UIControlEventTouchUpInside];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height)];
[view addSubview:button];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:view];
self.navigationItem.leftBarButtonItem = customBarItem;
This should work as it worked for me.
#Prasad Devediga, swift version works greatly:
let btnName = UIButton()
btnName.setImage(UIImage(named: "settings_filled_25"), forState: .Normal)
btnName.frame = CGRectMake(0, 0, 30, 30)
btnName.addTarget(self, action: Selector("toggleRight"), forControlEvents: .TouchUpInside)
var rightView = UIView()
rightView.frame = CGRectMake(0, 0, 30, 30)
rightView.addSubview(btnName)
let rightBarButton = UIBarButtonItem()
rightBarButton.customView = rightView
self.navigationItem.rightBarButtonItem = rightBarButton

Adding a Custom Button to a UINavigationItem LeftBarButtonItem

Could anyone tell me why this code does not working?
self.backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.backButton setImage:[UIImage imageNamed:#"back_arrow.png"]
forState:UIControlStateNormal];
self.backButton.contentMode = UIViewContentModeCenter;
[self.backButton addTarget:self
action:#selector(backButtonAction:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.backButton];
[navigationItem setLeftBarButtonItem:backButtonItem animated:NO];
navigationItem.hidesBackButton = YES;
Edit:
Nothing appears on the leftBarButtonItem. That's the problem.
From the docs:
"When creating a custom button—that is a button with the type UIButtonTypeCustom—the frame of the button is set to (0, 0, 0, 0) initially. Before adding the button to your interface, you should update the frame to a more appropriate value."
So you should see something if you set the frame in line 2, eg:
self.backButton.frame = CGRectMake(0, 0, 40, 20);
This should work
CGRect rect = CGRectMake(10, 0, 30, 30);
self.backButton = [[UIButton alloc] initWithFrame:rect];
[self.backButton setImage:[UIImage imageNamed:#"back_arrow.png"]
forState:UIControlStateNormal];
self.backButton.contentMode = UIViewContentModeCenter;
[self.backButton addTarget:self
action:#selector(backButtonAction:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.backButton];
self.navigationItem.leftBarButtonItem = backButtonItem;
self.navigationItem.hidesBackButton = YES;

How can i offset UIButton image inside UIBarButtonItem on navBar?

Here is the code i am using to insert a custom UIBarButtonItem as the leftButton on my navbar. The issue is that the button is too close to the left edge and i cant figure out how to indent it a little without using another image with padding on the left?
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
btn.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
[btn setBackgroundImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
self.myBtn = btn;
[btn release];
UIBarButtonItem *barBtn = [[UIBarButtonItem alloc] initWithCustomView:self.myBtn];
self.myBarBtn = barBtn;
self.myBarBtn.imageInsets = UIEdgeInsetsMake(0, 5, 0, 0);
[self.navigationItem setLeftBarButtonItem:self.myBarBtn animated:YES];
[barBtn release];
I've tried adjusting the frame, edgeInsets, all without any luck. The barButtonItem is still too close to the left edge. Is there any way to offset the image for the button?
Thx
UIImage *buttonImage = [UIImage imageNamed:#"Close.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
button.imageEdgeInsets = UIEdgeInsetsMake(0, -40, 0, 0);
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self action:#selector(donePressed:) forControlEvents:UIControlEventTouchUpInside];
//create a UIBarButtonItem with the button as a custom view
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
there is the custom left or right button with swift language
let buttonEdit: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
buttonEdit.frame = CGRectMake(0, 0, 40, 40)
buttonEdit.setImage(UIImage(named:"nav-right-Y.png"), forState: UIControlState.Normal)
buttonEdit.addTarget(self, action: "rightNavItemEditClick", forControlEvents: UIControlEvents.TouchUpInside)
var rightBarButtonItemEdit: UIBarButtonItem = UIBarButtonItem(customView: buttonEdit)
self.navigationItem.rightBarButtonItem = rightBarButtonItemEdit