Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7804327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:52:43+00:00 2026-06-02T01:52:43+00:00

I have a UINavigationBar and have the leftBarButtonItem and rightBarButtonItem set. The leftbar is

  • 0

I have a UINavigationBar and have the leftBarButtonItem and rightBarButtonItem set. The leftbar is set as:

UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [backButton setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
        UIBarButtonItem *myButton = [[[UIBarButtonItem alloc] initWithCustomView:backButton] autorelease];
        self.navigationItem.leftBarButtonItem = myButton;

and the rightbarbutton is set as:

UIButton *doneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
 UIImage *doneButtonImage = nil;
        doneButtonImage = [UIImage imageNamed:@"done.png"];
        [doneBtn setImage:doneButtonImage forState:UIControlStateNormal];
  doneBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:doneBtn];
    self.navigationItem.rightBarButtonItem = doneBarButtonItem;

the width of back is 23px and done is 72px, so they’re not equal. Now the issue is I have a label that I want to be in the center all the time.. and I want to clip the text if the text in the label is too long that it interferes with the right bar button. here’s how I set it up:

titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    titleLabel.text = self.pageTitle; // Can't change
    titleLabel.adjustsFontSizeToFitWidth = YES;
    titleLabel.clipsToBounds = YES;
    titleLabel.numberOfLines = 1;
    titleLabel.font = [UIFont fontWithName:@"PTSans-Narrow" size:30.0]; 
    titleLabel.textColor = [UIColor colorWithWhite:0.6 alpha:1.0];
    titleLabel.backgroundColor = [UIColor yellowColor];
    titleLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    titleLabel.textAlignment = UITextAlignmentCenter;
    [titleLabel sizeToFit];
    titleLabel.frameHeight = self.navigationController.navigationBar.frameHeight;


 self.navigationItem.titleView = titleLabel;

However this doesn’t center the title.. How can I do so?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T01:52:44+00:00Added an answer on June 2, 2026 at 1:52 am

    I suggest first count the width of self.pageTitle
    then accordingly set the position of titleLabel.I prefer this:-

    titleLabel = [[UILabel alloc] init];
    titleLabel.text = self.pageTitle; // Can't change
    
    **CGSize maximumTitleLabelSize = CGSizeMake(200,40);//Set maximum width that an self.pageTitle can have.
    CGSize expectedLabelSize = [titleLabel.text titleLabel.font 
                                                constrainedToSize:maximumTitleLabelSize
                                                    lineBreakMode:UILineBreakModeWordWrap];
    //adjust the label the the new width.
    CGRect newFrame = titleLabel.frame;
    newFrame.size.width = expectedLabelSize.width;
    titleLabel.size.width = newFrame.size.width;**    
    if(titleLabel.size.width==100)
    {
    titleLabel.frame = cgRectMake(85,5,titleLabel.size.width,30);
    }
    else if(titleLabel.size.width==200)
    {
    titleLabel.frame = cgRectMake(36,5,titleLabel.size.width,30);
    }
    
    titleLabel.adjustsFontSizeToFitWidth = YES;
    titleLabel.clipsToBounds = YES;
    titleLabel.numberOfLines = 1;
    titleLabel.font = [UIFont fontWithName:@"PTSans-Narrow" size:30.0]; 
    titleLabel.textColor = [UIColor colorWithWhite:0.6 alpha:1.0];
    titleLabel.backgroundColor = [UIColor yellowColor];
    titleLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    titleLabel.textAlignment = UITextAlignmentCenter;
    [titleLabel sizeToFit];
    titleLabel.frameHeight = self.navigationController.navigationBar.frameHeight;
    
    self.navigationItem.titleView = titleLabel;
    

    Try this.It will help you.Thanks 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have had the following code: [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@navbarBackBlack.png] forBarMetrics:UIBarMetricsDefault]; [[UINavigationBar appearance] setBackgroundImage:[UIImage
I have this code that changes the back button of my UINavigationBar // Set
I'm customizing UINavigationBar and have to set a custom background image to the navigation
I have set a custom tint color for a UINavigationBar (within a UINavigationController )
I have a UInavigationBar that is set to hidden in the view did load.
I have set UINavigationBar titleView as given below: UIView *labelView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 255,
I have a custom UINavigationBar title and a custom back button. My problem is
I have a UINavigationBar based application - only a one navigation bar that is
I have subclass of UINavigationBar . @interface MyNavigationBar : UINavigationBar Made some changes and
I have tried this approach/hack: http://blog.blackwhale.at/2009/06/uibuttons-in-uinavigationbar/ The problem is this leaves a faint seam.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.