首页 文章

iOS:导航控制器 Headers 不居中

提问于
浏览
2

我有一个导航控制器,左侧是自定义后退按钮 . 我以编程方式执行此操作,因此这不是自动布局问题 . 我的问题是导航控制器 Headers 没有居中,它是在屏幕的右侧 . 我记得在这一段时间内看到了一个修复,将一些类型的固定空间设置为正确的条形按钮项目,但我现在似乎无法找到类似的东西 .

有人可以告诉我如何将导航控制器 Headers 设置为居中,如果 Headers 对于其空间太大,请设置导航栏 Headers 以修复其字体大小以适应 Headers 空间的宽度 . 这一切都需要以编程方式完成,谢谢!

6 回答

  • 7

    在导航控制器中,默认情况下,视图控制器A按下当前视图控制器B.A的 Headers 将显示在视图控制器B中的backBarButton中 .

    导航控制器 Headers 未居中也是因为先前的视图控制器 Headers 太长 .

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

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

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

    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];
    
  • 3

    非常感谢您的答案,它们有助于我找到解决方案 . 我的解决方案是使用导航控制器的titleView属性 . 它设置 Headers 而不必经历制作自定义UIView的麻烦,并且setAdjustsFontSizeToFitWidth属性解决了我的字体大小太大的问题 . 我添加了一些额外的格式以使其看起来不错,但我不会发布它,因为它与我的问题没有关系 . 这是我正在使用的代码:

    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;
    
  • 2

    尝试这个,它会帮助你,它经过测试 .

    Objective C version

    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 version

    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
    
  • 1

    添加leftbarbutton和Rightbarbutton . 通常后退按钮存在 . 如果添加一个空的右侧栏按钮, Headers 将居中 .

  • 1

    在Utilities类中添加以下方法

    + (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];
    

相关问题