MSU_Bulldog
MSU_Bulldog

Reputation: 3519

iOS: Navigation Controller title not centered

I have a navigation controller with a custom back button on the left. I do this programmatically so this is not an auto-layout issue. My problem is that the navigation controller title is not centered, it is going off the right side of the screen. I remember seeing a fix for this awhile back dealing with setting some type of fixed space as the right bar button item, but I cannot seem to find anything similar to this now.

Could someone tell me how to set the navigation controller title to centered and if the title is too big for its space, set nav bar title to fix its font size to fit the width of title space. This all needs to be done programmatically, thanks!

Upvotes: 5

Views: 10145

Answers (6)

Add following method in your Utilities 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;
}

Use across view controllers as follows

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

Upvotes: 2

Yongxiang Ruan
Yongxiang Ruan

Reputation: 189

In navigation controller, by default, the view controller A that pushes current view controller B. The title of A will show up in backBarButton in view controller B.

The navigation controller title not centered is also because the previous view controller title is too long.

To counter that, use this in viewWillAppear of view controller A:

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

Upvotes: 8

Rathna Kumaran
Rathna Kumaran

Reputation: 113

Add both leftbarbutton and Rightbarbutton . Usually back button exist . If you add an empty right bar button , title will get centered.

Upvotes: 1

MSU_Bulldog
MSU_Bulldog

Reputation: 3519

Thanks so much for the answers, they were useful in helping my find a solution. My solution was to use the titleView property of the navigation controller. It sets the title without having to go through the trouble of making a custom UIView and the setAdjustsFontSizeToFitWidth property solved my problem of the font size being too big. I've added a little extra formatting to make it look nice, but I won't post it since it doesn't have to do with my question. Here is the code I am using:

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;

Upvotes: 3

Gopal Devra
Gopal Devra

Reputation: 614

Try out this, it would help you and it is tested.

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

Upvotes: 2

Nikunj
Nikunj

Reputation: 675

You have to add custom view in the navigation

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

Upvotes: 2

Related Questions