无法在导航栏中心设置 titleView,后退按钮的范围增加

Can't set titleView in Navigation bar center and scope of back button increased

我正在尝试推送一个视图控制器并设置其导航栏的标题,但我猜是因为使用了很长的标题,所以它没有居中。与此同时,后退按钮的范围增加到标题视图,即如果我点击 Milestone 的 "M",它会返回,尽管它的框架是相同的。

后退按钮的边界是一样的,但它的点击效果被拉长了。

下面是我如何推送视图控制器的代码。

 MilestoneDetailsViewController *controller = [[MilestoneDetailsViewController alloc] initWithNibName:@"MilestoneDetailsViewController" bundle:nil];
    [self.navigationController pushViewController:controller animated:YES];

在 MilestoneDetailsViewController 中,我将标题设置如下:

self.title = NSLocalizedString(@"Milestone Details", nil);

这样试试

UILabel* lbNavTitle = [[UILabel alloc] initWithFrame:CGRectMake(0,40,320,40)];
lbNavTitle.textAlignment = UITextAlignmentCenter;
lbNavTitle.text = NSLocalizedString(@"Milestone Details!",@"");
self.title = lbNavTitle // here self.title is your navigation bar title

后退按钮正在根据上一个 viewController 的标题选择大小。例如,您可以将其更改为空字符串,在您之前的 viewController 从您推送的位置写入此代码。

self.navigationItem.backBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:@""
        style:UIBarButtonItemStylePlain
       target:nil
       action:nil];

试试这个

    UIView *tView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 160 ,44)];

UILabel *bartitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 4, tView.frame.size.width, 40)];
[bartitleLabel setBackgroundColor:[UIColor clearColor]];
[bartitleLabel setTextAlignment:NSTextAlignmentCenter];
[bartitleLabel setAdjustsFontSizeToFitWidth:YES];
[bartitleLabel setText:NSLocalizedString(@"Milestone Details!",@"")];
[self.navigationItem setTitle:@""];

[tView addSubview:bartitleLabel];
[tView setBackgroundColor:[UIColor clearColor]];

[self.navigationItem setTitleView:tView];