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.
Related
I'm making an app in which I have a navigation bar that contains a custom background image. It looks like this.
I want to remove the status bar (as it covers part of the Nav Bar image), however when I do it shortens the NavBar, like so:
I want to find a way to get rid of the status bar without shortening the nav bar and causing the image in the navBar to come all out of proportion.
Here is the code I use to set the background image of the NavBar.
[[UINavigationBar appearance] setBackgroundImage:[[UIImage imageNamed:#"navigationBar"] stretchableImageWithLeftCapWidth:0 topCapHeight:0] forBarMetrics:UIBarMetricsDefault];
Thanks!
Edit: Please note that I attempted to just change the height of the Navigation Bar, however the background image remained out of proportion.
UIImageView *logoImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourLogoImage"]];
[logoImageView setFrame:CGRectMake(0.0f, 0.0f, 200.0f, 44.0f)];
logiImageView.contentMode = UIViewContentModeCenter;
self.navigationItem.titleView = logoImageView ;
Will fix your UIImage stretching.
EDIT:
or
self.navigationController.navigationItem.titleView = logoImageView;
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)
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.
I want to add an image in my navigation bar. To do this, i wrote that :
self.navigationItem.titleView = [[UIImageView alloc] initWithImage: [UIImage imageNamed:#"logo.png"]];
It's running, but the only problem is the image seems to be blurred.
When i look it on photoshop or other software, the image is perfect.
The size of this image is : 120 x 30px.
Someone to help me ? Thx
First of all, please, print self.navigationItem.titleView.frame after your viewDidAppear.
Seems to be, that in some case your titleView is placed with half-pixeled origin.x, or origin.y.
Try to play with:
[self.navigationController.navigationBar setTitleVerticalPositionAdjustment:<#(CGFloat)#> forBarMetrics:<#(UIBarMetrics)#>]
Try to set different values to the first param and observe the result.
You can change the navigation and status bar background only in iOS7. For this, you can use
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"yourImage.png"] forBarMetrics:UIBarMetricsDefault];
The size should be 320*64 px
and to change the Navigation Bar title
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourImage.png"]];
I have searched lots for how to design navigation like given in the image.
I have created category for UINavigationBar like given in the following solution
Custom nav bar styling - iOS
Able to increase the size of NavigationBar using below code.
CGFloat navBarHeight = 84.0f;
CGRect frame = CGRectMake(0.0f, 0.0f, 320.0f, navBarHeight);
[self.navigationController.navigationBar setFrame:frame];
But all content like (left button, right button, title) moves to bottom of bar.
When attach segment control to titleView like below
self.navigationItem.titleView = self.segmentedControl;
All views( left button, title, right button) as liner.
I want segment control bottom of the navigation bar as shown in the image.
#Shankar Shinde why are you adding segment controller as navigation title view you can add below the navigation bar using storyboard: check below screen shot
I think you need to hide the navigation bar using following code and create view like attached screenshot let me know it's help you or not ?
[self.navigationController.navigationBar setHidden:YES];
Please check it out sample:
https://github.com/boctor/idev-recipes/tree/master/WoodUINavigation
It may help you :)
You can use Prompt provided for navigationItem
First give prompt to your navigationItem
self.navigationItem.prompt = #"";
Create a segmentcontroll
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(35, 200, 250, 50);
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
segmentedControl.selectedSegmentIndex = 1;
[self.navigationController.navigationBar addSubview:segmentedControl];