iOS 7自定义UINavigationBar TitleView在推送或弹出新的View Controller时移动 [英] iOS 7 Custom UINavigationBar TitleView moves when Pushing or Popping new View Controller
问题描述
我正在使用UINavigationBar的自定义标题视图,其代码如下:
I am using a custom title view for a UINavigationBar with the following code:
// Set a label to the nav bar
THLabel *titleLabel = [[THLabel alloc] init];
titleLabel.text = @"Test";
titleLabel.font = [UIFont fontWithName:APP_FONT size:22.0];
titleLabel.frame = CGRectMake(0, 0, 100, 30);
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.textColor = CUSTOM_LIGHT_BLUE;
titleLabel.strokeColor = kStrokeColor;
titleLabel.strokeSize = kStrokeSize;
self.navigationItem.titleView = titleLabel;
问题在于,当呈现新的viewcontroller然后返回到原始视图控制器时,此自定义视图会发生变化然后重新集中自己。请参阅视频以了解相关信息。
The problem is that when presenting a new viewcontroller and then returning to the original view controller this custom view shifts and then re-centers itself. Please see the video for a demonstration of that.
请在此处观看视频:
https://www.youtube.com/watch?v=961CCVQmpJM&feature=youtu.be
我已使用故事板和每个视图控制器的代码禁用导航控制器的每个子视图的自动调整:
I have disabled autoresizing of every subview for the navigation controller with both the storyboard and in code for each view controller:
// Set the navigation bar hidded on the log in view
UINavigationController* mainViewController = (UINavigationController*)self.appDelegate.window.rootViewController;
[mainViewController setNavigationBarHidden:YES];
[[mainViewController navigationBar] setAutoresizesSubviews:NO];
然而它仍然会调整大小!我怎么能阻止这个 - 我做错了什么?谢谢!
However it still resizes! How can I stop this - what am I doing wrong? Thanks!
推荐答案
只有在 viewWillAppear中设置 titleView 代码时,它才能重现即可。将其移至 viewDidLoad 可修复问题
It's reproducible for me only if I place setting titleView code in viewWillAppear. Moving it to viewDidLoad fixes the issue
这篇关于iOS 7自定义UINavigationBar TitleView在推送或弹出新的View Controller时移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!