iOS: 导航控制器标题未居中

iOS: Navigation Controller title not centered

我有一个导航控制器,左侧有一个自定义后退按钮。我以编程方式执行此操作,因此这不是 auto-layout 问题。我的问题是导航控制器标题没有居中,它超出了屏幕的右侧。我记得有一段时间看到一个修复程序,将某种类型的固定 space 设置为右栏按钮项目,但我现在似乎找不到与此类似的东西。

有人能告诉我如何将导航控制器标题设置为居中吗?如果标题对于它的 space 来说太大了,请设置导航栏标题以固定其字体大小以适应标题的宽度 space。这一切都需要以编程方式完成,谢谢!

您必须在导航中添加自定义视图

    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(10, 0, 150, 44)];

    UILabel *titleLabel=[[UILabel alloc] initWithFrame:CGRectMake(10, 165,370, 25)];
    titleLabel.text=@"Home";
    titleLabel.textColor=[UIColor whiteColor];
    [view addSubview:titleLabel];    
    [self.navigationController.navigationBar addSubview:view];

试试这个,它会帮助你,它已经过测试。

Objective C版本

UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
    [button addTarget:self action:@selector(yourSelector:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"<" forState:UIControlStateNormal];

    button.titleEdgeInsets = UIEdgeInsetsMake(-3, -15, 3, 15);
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = leftItem;

Swift版本

button = UIButton(frame: CGRectMake(0, 0,50, 50))
        button?.addTarget(self, action: "backButtonTapped:", forControlEvents: UIControlEvents.TouchUpInside)
        button?.setTitle("<", forState: .Normal)
        button?.titleLabel?.font = UIFont(name: kFontHelveticaNeueRegular, size: 30)
        button?.titleEdgeInsets = UIEdgeInsetsMake(-3, -15, 3, 15)
        button?.setTitleColor(UIColor.whiteColor(), forState: .Normal)

        var leftItem = UIBarButtonItem(customView: button!)
        controller.navigationItem.leftBarButtonItem = leftItem

非常感谢您的回答,它们对帮助我找到解决方案很有帮助。我的解决方案是使用导航控制器的 titleView 属性。它设置标题而无需经历制作自定义 UIView 的麻烦,setAdjustsFontSizeToFitWidth 属性 解决了我的字体大小太大的问题。我添加了一些额外的格式以使其看起来不错,但我不会 post 它,因为它与我的问题无关。这是我使用的代码:

UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, self.view.frame.size.width-60, self.navigationController.navigationBar.frame.size.height)];
titleLabel.text = @"This is my big, long title";
titleLabel.textColor = [UIColor blackColor];
titleLabel.textAlignment = UITextAlignmentCenter;
[titleLabel setAdjustsFontSizeToFitWidth:YES];
self.navigationItem.titleView = titleLabel;

同时添加 leftbarbutton 和 Rightbarbutton 。通常后退按钮存在。如果您添加一个空的右栏按钮,标题将居中。

在导航控制器中,默认情况下,视图控制器A推送当前视图控制器B。A的标题将显示在视图控制器B的backBarButton中。

导航控制器标题不居中也是因为之前的视图控制器标题太长。

为了解决这个问题,请在视图控制器 A 的 viewWillAppear 中使用它:

self.navigationItem.backBarButtonItem= UIBarButtonItem(title: "", 
    style: UIBarButtonItemStyle.Plain, target: nil, action: nil)
self.navigationItem.backBarButtonItem!.title = ""`

在您的实用程序中添加以下方法class

+ (void)setTitle:(NSString *)title forViewController:(UIViewController *)viewController {
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
    [label setTextAlignment:NSTextAlignmentCenter];
    [label setTextColor:[UIColor whiteColor]];
    [label setFont:[UIFont boldSystemFontOfSize:18]];
    [label setText:title];
    [label sizeToFit];

    viewController.navigationItem.titleView = label;
}

跨视图控制器使用如下

[Utilities setTitle:@"Title for View Controller" forViewController:self];