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

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.

Related

Resizing UISearchBar in UINavigation Bar?

I have added UISearchBar to UINavigationBar and this works really well, Except the left padding of UISerachBar here.
On Debug I came to know that here back button frame is too large that's why UINavigation bar title view shifted from left too much.
How to deal with that?
I want to achieve something like twitter app where the spacing between back button and search bar is not much.
Thanks in advance!
You can add a custom back button in your navigation bar to override the default back button (in this case the left button):
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"my-back-button-image.png"]];
imageView.frame = CGRectMake(0, 0, 43, 30);
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:imageView];
self.navigationItem.leftBarButtonItem = barButtonItem;
The space between left button and search controller decreases.
As mentioned earlier by Michele Broggi you can try that.
Else
You can also set the search Bar in place of navigation Title view so that it would be centre aligned to the screen (If you want that design)

Can't set image in UINavigationBar to the center

I'm using two images in my navigaton controller. One for the rightBarButtonItem and one for the titleView. I created both in code. So my problem is that the title view is not in the center, it's a little bit closer to the left side. It's because the right bar button, it's sure, I tested it and found some related questions, but still couldn't solve the problem.
I tried [[UIBarButtonItem appearance] setTitlePositionAdjustment:UIOffsetMake(0.0f, 0.0f) forBarMetrics:UIBarMetricsDefault]; but doesn't helped me out. I would really appreciate any suggestions.
- (void)setupLogo {
[self setNeedsStatusBarAppearanceUpdate];
CGRect myImage = CGRectMake(0, 0, 41, 41);
UIImageView *myLogo = [[UIImageView alloc] initWithFrame:myImage];
[myLogo setImage:[UIImage imageNamed:#"logo.png"]];
myLogo.contentMode = UIViewContentModeScaleAspectFit;
self.navigationItem.titleView = myLogo;
[[UIBarButtonItem appearance] setTitlePositionAdjustment:UIOffsetMake(0.0f, 0.0f) forBarMetrics:UIBarMetricsDefault];
}
The titleView is centered between the left and right items. It's not centered on the nav bar.
One possible solution would be to add a dummy left bar button item of the same size as your right bar button item. This will force the titleView to the center.

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.

UIToolbar in iPad

I wanted to have two buttons on the both end of Navigation Bar (in iPad's Detail View Controller).
So I created two UIToolbars and I set the them as Left&RightBarButtonItems.
But, There is some color variation in the NavigationBar.
Attached images for your understanding.
the code I used ,
UIToolbar *leftToolbar =[[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 200, 45)];
NSMutableArray *lItems = [[NSMutableArray alloc] initWithArray:[leftToolbar items]];
UIBarButtonItem *lb1 =[[UIBarButtonItem alloc]initWithTitle:#"Home"style:UIBarButtonItemStyleBordered target:self action:#selector(home:) ];
UIBarButtonItem *lb2 =[[UIBarButtonItem alloc]initWithTitle:#"New Document"style:UIBarButtonItemStyleBordered target:self action:#selector(newDoc:) ];
[lItems insertObject:lb1 atIndex:0];
[lItems insertObject:lb2 atIndex:1];
[leftToolbar setItems:lItems animated:YES];
[lItems release];
leftToolbar.barStyle =UIBarStyleBlackTranslucent;
leftToolbar.tintColor=[UIColor clearColor];
self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:leftToolbar];
Can you help me to avoid this color variation?
Is there any other way to have buttons like this, without using UIToolbar?
Thanks ,
Gopi.
just remove navigation bar and add tool bar, why you adding toolbar to navigation bar?
To achieve the same, use segment controll, set it in Left or right barbutton's view, once you select a segmet, deselect it after few seconds, say 0.3secs, it looks good, no color vairations, it looks like a part of Navigation bar
Found the solution !
code is correct, but one small bug. Have to set the height to 44, not 45. I did this and it seems to fit over the existing NavigationBar.
UIToolbar *leftToolbar =[[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
Works for me . . Anyway I moved to the Single tool bar method.
Hope this helps some one.!!
Have a great day!!
Gopi.

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

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...

Resources