Invalid name in UINavigationBar with iOS6

With iOS 6, I have several problems using custom style in my application. I use my own font and several UIAppearance . The problem that I cannot figure out is the misalignment of the name in my UINavigationBar. In iOS 5, everything worked fine and was properly aligned.

Since iOS6 is released and user style is not uncommon, I assume that this is not a mistake, but my misunderstanding of some new change for iOS6.

misalignment

I was looking for documentation for the text alignment method to call the UIAppearance proxy, but I could not find such a method.

I use the following lines of code to style my UINavigationBar throughout my application:

 [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"NavigationBarBackground"] forBarMetrics:UIBarMetricsDefault]; [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"NavigationBarBackground"] forBarMetrics:UIBarMetricsLandscapePhone]; [[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor whiteColor], UITextAttributeTextColor, [UIFont fontWithName:@"Corbel-Bold" size:14.0], UITextAttributeFont, nil]]; [[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTintColor:[UIColor whiteColor]]; [[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor ceBlueColor], UITextAttributeTextColor, [UIColor whiteColor], UITextAttributeTextShadowColor, [NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset, [UIFont fontWithName:@"Corbel" size:0.0], UITextAttributeFont, nil] forState:UIControlStateNormal]; [[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -3) forBarMetrics:UIBarMetricsDefault]; 
+4
source share
2 answers

The same problem, but I noticed that this does not happen when there is either a "Back" button on the navigation panel or a right panel button (editing the table). Heading alignment is also fixed when navigating to the new view controller, and then returns to the first ...

What I think this is happening is that iOS calculates the position of the header frame based on the default font, because the font I use is a little smaller, the name is slightly shifted to the left of the center.

My current fix is ​​calling setNeedsLayout in viewWillAppear. Seems to work.

 - (void) viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; if (self.navigationItem.hidesBackButton || self.navigationItem.rightBarButtonItem == nil) { [self.navigationController.navigationBar setNeedsLayout]; } } 
+12
source

If this helps, you can adjust (at least) the vertical position of the header:

 [[UINavigationBar appearance] setTitleVerticalPositionAdjustment:4 forBarMetrics:UIBarMetricsDefault]; 

(This question helped me find a solution to this problem: UINavigationBar custom header position )

+5
source

Source: https://habr.com/ru/post/1438618/


All Articles