on iOS, how can I shift navigationItem.leftBarButtonItem horizontally right? - ios

A cusomized UINavigationBar requires me to present a customized "back" button, I use navigationItem.leftBarButtonItem = myCustomizedButton, but its position is fixed.
Would anyone be so kind to share how can I shift this button 40pixels to right?

You can create a containing view that is 40 pixels bigger than your image.
Add your image at 40 pixels offset.
Add the containing view as the leftBarButtonItem.
Code follows:
// Create a containing view to position the button
UIView *containingView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, barButtonImage.size.width + 40, barButtonImage.size.height)] autorelease];
// Create a custom button with the image
UIButton *barUIButton = [UIButton buttonWithType:UIButtonTypeCustom];
[barUIButton setImage:barButtonImage forState:UIControlStateNormal];
barUIButton.frame = CGRectMake(40, 0, barButtonImage.size.width, barButtonImage.size.height);
[barUIButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
[containingView addSubview:barUIButton];
// Create a container bar button
UIBarButtonItem *containingBarButton = [[[UIBarButtonItem alloc] initWithCustomView:containingView] autorelease];
// Add the container bar button
navigationItem.leftBarButtonItem = containingBarButton;

You can add a blank space to your picture that you display on the navBar. I've had the same problem, and it's the only one solution i've found for resolve it. A little bit tricky but it works...

Related

Navigation bar title is not centred in iPhone's portrait mode

I have created a ViewController Using Xib file of size wAny and hAny and i have used autolayaout and constraint to fit UI all devices and all orientation.
Everything is working fine but when i'm running app on iPhone portrait mode title of navigation bar is not centred where as when rotating it to landscape mode title is centred.
I have used following code for setting title
self.title = #"Download Schedule";
Already tried:
I have tried to set custom UILabel of lesser font size to Navigation bar's title view but still i'm getting it on left side .
Here is code of my right button
//add right bar button for more options
UIView *navBarButtonsView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
UIBarButtonItem *item0 = [UIBarButtonItem negativeSpacerWithWidth:5];
UIButton *moreButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
[moreButton setBackgroundImage:[UIImage imageNamed:#"ic_web"] forState:UIControlStateNormal];
[moreButton addTarget:self action:#selector(addNewSchedule) forControlEvents:UIControlEventTouchDown];
[navBarButtonsView addSubview:moreButton];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:navBarButtonsView];
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:item0, backButton, nil];
There is something else going on - something you have not told us about. I suspect that there is something wrong with how you are setting your rightButtonItems, such that there are invisible buttons taking up the space on the right. I can reproduce this issue only by deliberately making the right button really wide:
So I suspect you have applied a large width to the right button, or there are extra invisible right button items taking up the space on the right.

setting the frame of a customView in UIBarButtonItem

I am doing
mapButton *topMapButton = [[mapButton alloc] init];
[topMapButton setSize:CGSizeMake(70, 30)];
topMapButton.frame = CGRectMake(0, STATUS_HEIGHT +7, 70, 30);
UIBarButtonItem *barIcon = [[UIBarButtonItem alloc] initWithCustomView: topMapButton];
[topMapButton addTarget:self action:#selector(openMap:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = barIcon;
where mapButton is a UIButton subclass, with preset look, nothing else
No matter what frame I set for topMapButton, it keeps being in (0,0) and not in the rightBarButtonItem usual place.
Note that using autolayout programatically to set the place works for placing, but crashes when I push another controller in the stack...
Am I doing anything wrong?
Look at this answer
Shortly, you can set edge inset. override alignmentRectInsets for your uibutton subclass and change top and left insets.
- (UIEdgeInsets)alignmentRectInsets {
UIEdgeInsets insets = UIEdgeInsetsMake(top_value, left_value, bottom_value, right_value);
return insets;
}
OK found the problem..
It seems that UIBarButtonItem doesn't really care what frame the customView has, since it will set it anyway.
however, my custom button MUST have a custom type, otherwise it will mess the frame and keep it (0,0) of the application screen.
self = [UIButton buttonWithType:UIButtonTypeCustom];

Bar Button Item not correctly aligned

I have an issue with my navigation bar, the UIBarButtonItem is not correctly vertically aligned. I don't know why because I'm using a simple UIBarButtonItem and not a custom view. See my code below and thanks for your help!
UIBarButtonItem *newGameItem = [[UIBarButtonItem alloc] initWithTitle:#"New Game" style:UIBarButtonItemStyleDone target:self action:#selector(newGame)];
self.navigationItem.rightBarButtonItem = newGameItem;
Actually there are couple ways to align the bar buttons.
Try to adjust the button title position with this:
[self.navigationItem.rightBarButtonItem setTitlePositionAdjustment:UIOffsetMake(-10, -10)
forBarMetrics:UIBarMetricsDefault];
-10 -10 - just for example
OR
you can initialize your newGameItem with custom UIButton. The custom button is a subclass of UIButton with this method overridden
- (UIEdgeInsets)alignmentRectInsets {
return UIEdgeInsetsMake(0, -10, 0, 15.0f); // here you can play with values to align your button properly
}
then initialize your newGameItem
CustomBarButton *button = [CustomBarButton buttonWithType:UIButtonTypeCustom];
[button setTitle:#"New game" forState:UIControlStateNormal];
button.frame = CGRectMake (0, 0, 60, 40);
[button addTarget:self action:#selector(newGame) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *newGameItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.rightBarButtonItem = newGameItem;
I had a similar issue: We use custom fonts via UIAppearance for both navigation titles and bar button items, and in some cases, the right item was vertically misaligned.
I could easily fix the misalignment by using [self.navigationItem setRightBarButtonItem:rightItem animated:YES] instead of the property setter.
I had the same problem with a button on the left, I was able to fix it in Xcode 6.3.1: select the button (bar button item) right column tab "ruler" inset image Top value: -54 instead of 0.
Note that: my device is an iPhone4 with iOS 7, the button was perfectly in place in Xcode and misplaced on the device, now it is in place on the device and misplaced in Xcode
Unfortunately, there is not much you can do about UIBarButtonItem vertical spacing. I believe you'll have to either increase the font size of your title on the UINavigationBar (you can do this with UIAppearance also), or decrease the font size of your UIBarButtonItem.

Custom navigation bar not showing

Controller 1 displays an array of UIImages, Controller 2 manages the paging of those images once selected, and Controller 3 just displays the images for the pages. When I tap on a cell and load a photo into the photoViewController, the paging of images works fine, but my custom navigation bar is overridden with the default bar and I am unable to add any custom buttons to it or change any of its properties in code. The only thing I found that I am able to do is hide it. If I try to change the title of the back button or add my own title to the navigation bar nothing changes. Please help with anything you can.
i think you should try like this
self.navigationItem.leftBarButtonItem = [self leftMenuBarButtonItem];
and function could be
- (UIBarButtonItem *)leftMenuBarButtonItem {
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(50, 50, 50, 30)]; [backButton setImage:[UIImage imageNamed:#"bar_btn_bg.png"] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(leftDrawerButtonPress:) forControlEvents:UIControlEventTouchUpInside];
return [[UIBarButtonItem alloc] initWithCustomView:backButton];
}
if you fall in between let me know

Change navigation bar back button and remove title to make it appear like in ios7

I want to remove back button title from my navigation bar and put a custom back button to make it appear as in ios7 (just a left arrow). I could change change the text, but when i change the text to nil, the previous page title appears again. Also changing the background image is not working too, though i could change the image
try this code to hide back button:
self.navigationItem.hidesBackButton=YES;
self.navigationItem.leftBarButtonItem=nil;
create custom back button like this:
CGRect frameimgback1 = CGRectMake(0, 0, 60, 35);
UIButton *back = [[UIButton alloc] initWithFrame:frameimgback1];
[back setImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
[back addTarget:self action:#selector(back)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btnL = [[UIBarButtonItem alloc]initWithCustomView:back];
self.navigationItem.leftBarButtonItem = btnL;

Resources