iOS 7自定义UINavigationBar当推或popup新的View Controller时,TitleView会移动

我使用下面的代码使用UINavigationBar的自定义标题视图:

// 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; 

问题是,当呈现一个新的视图控制器,然后返回到原始视图控制器此自定义视图转移,然后重新居中。 请观看video以进行演示。

请点击此处查看video: https : //www.youtube.com/watch?v = 961CCVQmpJM&feature=youtu.be

我已禁用导航控制器的每个子视图的自动调整,包括故事板和每个视图控制器的代码:

  // Set the navigation bar hidded on the log in view UINavigationController* mainViewController = (UINavigationController*)self.appDelegate.window.rootViewController; [mainViewController setNavigationBarHidden:YES]; [[mainViewController navigationBar] setAutoresizesSubviews:NO]; 

但是它仍然调整! 我怎么能阻止这个 – 我做错了什么? 谢谢!

只有当我把viewView代码放在viewWillAppear中时,它才能被重现。 将它移到viewDidLoad可以解决这个问题

我将embeddedUIView内的标签。 Interface Builder不喜欢在titleView直接放置一个UILabel ,这可能与你的问题有关。

另外尝试将autoResizingMask设置为UIViewAutoresizingFlexibleTopMargin 。 根据我的经验,任何在酒吧中的自定义视图的行为performance更好。

这也发生在我身上。 你可以做两件事:

1)确保导航设置在viewDidLayoutSubviews或viewDidLoad中完成,如上面的答案中所述

2)我有左和右栏button项目为零,但只有在标题标签设置后才调用它们。 在将titlelabel设置为titleview之前,确保将右侧和左侧的barbutton项设置为零(如果您当前没有使用它们)。

在上面的@Alex Peda的回答扩展,我发现,在iOS7,viewDidLoad以外,似乎有一个自定义标题的最小标题宽度。 下面是我正在做的事情。 请注意,我的代码中有一些特定的方法。

 #define MAX_TITLE_WIDTH 400 #define MIN_TITLE_WIDTH 150 - (void) setNavBarTitle: (NSString *) newTitle; { if (!newTitle) newTitle = @""; NSMutableString *title = [newTitle mutableCopy]; if (![_titleView.text isEqualToString:title]) { NSAttributedString *attrTitle = [UIAppearance attributedString:title withFontType:FontTypeTitle | FontTypeBold | FontTypeItalic size: 40.0 andOtherAttributes:@{NSForegroundColorAttributeName: [UIColor blackColor]}]; _titleView.attributedText = attrTitle; [_titleView sizeToFit]; // In iOS7, if you set the nav bar title with a custom view outside of viewDidLoad, there appears to be a minimum title width. Narrower custom view titles are not centered properly. I'm working around this by centering the text in the label, and setting the width of the label to the minimum width. if ([Utilities ios7OrLater]) { if (_titleView.frameWidth < MIN_TITLE_WIDTH) { _titleView.textAlignment = NSTextAlignmentCenter; _titleView.frameWidth = MIN_TITLE_WIDTH; } } if (_titleView.frameWidth > MAX_TITLE_WIDTH) { _titleView.frameWidth = MAX_TITLE_WIDTH; } } self.navigationItem.titleView = _titleView; } 

在我的情况下,这是因为我在titleView之前设置UIBarButton。 设置titleView应该是第一个。 现在完美的工作。

什么对我来说是在视图控制器中创build一个variables保存所需的标题视图,然后在viewDidLoad初始化它。 然后,您可以在viewWillAppear将该视图设置为self.navigationItem.titleView ,并显示正确。 无需设置autoResizeMask或rightBarButtons等

例:

 class ViewController { var myTitleImage: UIImageView! override func viewDidLoad() { super.viewDidLoad() myTitleImage = UIImageView(frame: CGRect(x: 0, y: 0, width: 25, height: 25)) myTitleImage.contentMode = .scaleAspectFit myTitleImage.image = #imageLiteral(resourceName: "my_title_image") // etc... } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) self.navigationItem.titleView = self.myTitleImage } }