I have a navigationBar with both Left and Right bar buttons on each side. I have a customTitlelabel
which I set as the titleView of the UINavigationItem
.
[self.navigationItem setTitleView:customTitleLabel];
All is fine now. The problem, the size of the rightbarButton is dynamic based on the input I get in one of the text fields.
Therefore the title is automatically centered based on the available space between the buttons.
how can i set the title to a fixed position?
I had similar problem. My solution is do hide the original back button, add add your own implementation. Since the system will reserve space for the left items.
and the selector is simple
now it looks like this:
oh...and don't forget to use autolayout in your custom title view if you have dynamic length content like label in it. I add an additional layout in the customview to give it like "wrap_content" in Android by setting it centered to parent , and leading and trailing space ">=" 0
You can't do what you want directly -- the position of your title view is out of your control (when managed by
UINavigationBar
).However, there are at least two strategies to get the effect you want:
1) Add the title view not as the 'proper' title view of the nav bar, but as a subview of the
UINavigationBar
. (Note: this is not 'officially' sanctioned, but I've seen it done, and work. Obviously you have to watch out for your title label overwriting bits of the buttons, and handle different size nav bars for different orientations, etc. -- a bit fiddly.)2) Make an intelligent UIView subclass that displays a given subview (which would be your UILabel) at a position calculated to effectively show the subview perfectly centered on the screen. In order to do this, your intelligent UIView subclass would respond to layout events (or
frame
property changes etc.) by changing the position (frame
) of the label subview.Personally, I like the idea of approach 2) the best.
I had a similar situation where a
titleView
should be centered in UINavigationBar. I like occulus's approach of subclassing a UIView and overridingsetFrame:
. Then, I can center the frame inside the dimensions of UINavigationBar.In the UIView subclass:
The UIView subclass can then be assigned normally to
titleView
for each navigationItem. The developer does not have to programmatically add and remove special subviews from UINavigationBar.I tried aopsfan's answer but it didn't work. A breakpoint revealed that the bar's center was "(480.0, 22.0)" (The X coordinate way off) .
So I changed it into this:
...and it works like a charm. The slide/fade effect when pushing view controllers is intact. (iOS 5.0)
Setting the titleView property of the nav bar works just fine - no need to subclass or alter any frames other than those of your custom view.
The trick to getting it centered relative to the overall width of UINavigationBar is to:
Here's some example code that creates a custom titleView with a label which remains centred in UINavigationBar irrespective of orientation, left or right barbutton width: