Misaligned title in UINavigationBar since iOS6

后端 未结 2 1849
既然无缘
既然无缘 2021-01-13 15:25

Ever since iOS 6 I have several problems with using custom styling in my application. I use a custom font and several UIAppearance proxies. A problem I can\'t g

相关标签:
2条回答
  • 2021-01-13 15:43

    If it helps, you can adjust (at least) the vertical position of the title:

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

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

    0 讨论(0)
  • 2021-01-13 16:02

    Same issue here, but I noticed it doesn't happen when the navigation bar has either a back button or a right bar button (table edit). The alignment on the title is also fixed when navigating to a new view controller and then going back to the first one...

    What I think that is happening is that iOS calculates the position of the frame of the title based on the default font, because the font I use is a bit smaller the title misaligns a bit to the left of the center.

    My current fix is calling setNeedsLayout in viewWillAppear. Seems to be working.

    - (void) viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
    
        if (self.navigationItem.hidesBackButton || self.navigationItem.rightBarButtonItem == nil) {
            [self.navigationController.navigationBar setNeedsLayout];
        }
    }
    
    0 讨论(0)
提交回复
热议问题