I’m trying to customize my UINavigationBar font, using the following code for iOS 5 in my app delegate’s application:didFinishLaunchingWithOptions:
if ([[UINavigationBar class] respondsToSelector:@selector(appearance)])
{
[[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
[UIColor blackColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(1, 0)], UITextAttributeTextShadowOffset,
[UIFont fontWithName:kDefaultFont size:0.0], UITextAttributeFont,
nil]];
}
It works fine and the navigation bar is rendered using my font. Great.
The references I’ve found suggest that you can use the font size of zero and it will resize the font to fit your navigation bar (using a slightly smaller font for the shorter navigation bar for the landscape layout). And it does choose a font size that fits nicely to the height of the navigation bar. But it looks like if you go from portrait to landscape and back, the width of the navigation bar’s title label gets screwed up, so what shows up as a title of, for example, “Long Titlebar”, looks fine when you first view it in portrait orientation, looks fine when you view it in landscape (with the appropriately smaller font), but when I come back to portrait, the font correctly reverts to the larger font, but the title text itself is truncated, becoming “Long…” even though there’s plenty of space for the full title. Has anyone else seen this behavior when using a font size of 0.0?
Clearly, I can just specify an actual font size (in which case I don’t see this truncating behavior), but then I’m manually figuring out what size to use. And worse, the font size is the same for landscape and portrait, so right now I’m using a font size that fits in the shorter landscape navigation bar title and the title is smaller than it needs to be in the taller portrait navigation bar.
Has anyone out there had experience with using setTitleTextAttributes to change the font of the [UINavigationBar appearance] in such a way that the font size changes between portrait and landscape, but not having this truncation of the title when you return back to portrait after going to landscape? I’m about to pursue various kludgy workarounds, but if you have any experience in this issue, let me know.
Update:
In the process of submitting this bug to Apple, I decided to demonstrate how to reproduce the problem:
-
Create new iOS Master-Detail Application in Xcode 4.3.2.
-
Put the above
setTitleTextAttributescode in the app delegate’sapplication:didFinishLaunchingWithOptions(I used the font @”GillSans”). -
Go to MasterViewController and add line that says
self.title = @"Long Title"; -
Comment out the
UIBarButtonItem *addButtoncode. -
Run the program. Note the title correctly says “Long Title”. Rotate to landscape. Still looks good. Rotate back to portrait and the title now says “Long…” even though there’s plenty of space.
-
Curiously, if you restore the
UIBarButtonItem *addButtoncode, the title works as it should. But if you either eliminate the UIBarButton item, or replace it with a button that usesinitWithTitlerather thaninitWithBarButtonSystemItem, you get the problem with the navigation bar title after rotating from portrait to landscape and then back to portrait.
the following is my workaround for this problem, and here’s the comment that appears next to my workaround implementation in each case as a reminder to myself as to why i implemented this bit of code:
so, for the cases where i’m ok with using the 0.0 value for
UITextAttributeFontbut have to continue supporting iOS5, i use solution (1):and in the couple of cases of legacy code where i wanted to support iOS 6 and fix the glitch when the view first appears without having to re-write the MyAppAppearance class methods i have that set non-0.0 values for my
[UINavigationBar appearance] titleTextAttributes, i found it easier to implement the solution (2) thus:(and the
__IPHONE_OS_VERSION_MIN_REQUIREDbracket just helps remind me which code can eventually go away if desired in the future, and which may have to stay.)to see a little more exactly what happens here, particularly in the case of the rotation, run the simulator with slow animations toggled.