Zeeshan Anjum
Zeeshan Anjum

Reputation: 148

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

I am trying to push a view controller and set its navigation bar's title but it is not centered due to long title being used i guess. Along with that, scope of the back button is increased till the title view, i.e if I tap with with Milestone's "M", it gets back though it frame is the same.

enter image description here

Bounds of back button are the same but its click impact is elongated.

enter image description here

Below is the code for how I am pushing the view controller.

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

and in the MilestoneDetailsViewController, i set the title as following:

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

Upvotes: 0

Views: 622

Answers (3)

Himanshu Moradiya
Himanshu Moradiya

Reputation: 4815

Try like this

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

Upvotes: 0

Umair Afzal
Umair Afzal

Reputation: 5049

Back button is picking its size according to the title of previous viewController. you can change it to a empty String for example, in your previous viewController from where you are pushing write this code.

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

Upvotes: 2

Koushik
Koushik

Reputation: 1250

Try this

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

Upvotes: 0

Related Questions