UINavigationBar header is truncated when viewControllers change

When I pop the new tableViewController from the initial screen of the iOS application (I click on the settings screen), the title in the UINavigationController is trimmed to the end of the animation:

enter image description here

This is the navigation bar in the middle of the animation, and just before the animation finishes, it looks like this:

enter image description here

After a moment, the title will correctly change to "Settings". It doesn’t matter, but you can imagine how much this worries a programmer who is a little prone to OCD! :)

Here is the code in the ViewController table where I set the title, nothing special:

- (id)initWithStyle:(UITableViewStyle)style { self = [super initWithStyle:style]; if (self) { self.title = @"Settings"; // Hide tabBar when pushed so you cannot switch from the Settings self.hidesBottomBarWhenPushed = YES; self.tableView.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"bg.png"]]; } return self; } 
+6
source share
3 answers

I was a bit late with the answer, but I figured out the problem on iOS 5. When you use the UIAppearance proxy on the UINavigationBar, you need to explicitly specify the font size, instead of using 0.0, to allow it to be automatically set based on the orientation.

I was able to fix this by running a subclass of UINavigationController and entering the following code:

 - (void)viewWillLayoutSubviews { [super viewWillLayoutSubviews]; // You should include a conditional here to check for iOS 5, so iOS 6 doesn't have to do any additional work self.navigationBar.titleTextAttributes = @{ UITextAttributeFont:[UIFont boldSystemFontOfSize:UIInterfaceOrientationIsPortrait(self.interfaceOrientation) || IS_IPAD ? 20.0f : 16.0f], UITextAttributeTextColor:[UIColor whiteColor], UITextAttributeTextShadowColor:[UIColor colorWithWhite:0.0f alpha:0.5f], UITextAttributeTextShadowOffset:[NSValue valueWithUIOffset:UIOffsetMake(0.0f, -1.0f)] }; } 
+1
source

try to install

 self.navigationItem.title = self.title; 

in viewWillAppear method

0
source

try

 - (void)viewWillAppear:(BOOL)animated { self.title = @"Settings"; } 
0
source

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


All Articles