简体   繁体   中英

set the alignment of title of navigation bar

I'm trying to align the title of the navigation bar to the center in my app but it seems the title is staying on the right side (please find the screen shot). I'm using the code below:

-(void)viewDidLoad {
    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:@"Home" style:UIBarButtonItemStylePlain target:self action:@selector(goToHomePage:)];
    [self.navigationController.navigationItem.rightBarButtonItem setTarget:self];
    self.navigationItem.rightBarButtonItem = addButton;
    [addButton release];
}

in viewWillAppear() as below

[self.navigationItem setTitle:offertitle];

and tried this too

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(40, 0, 120, 40)];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:14.0];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = UITextAlignmentLeft;
label.textColor =[UIColor whiteColor];
label.text=offertit;
self.navigationItem.titleView = label;

the title displays center aligned title when I hide the back button. Is there any method to set the appearance attributes of back button?

image link here

Can anyone guide me where i may have gone wrong.

You don't have to use UILabel. You can directly use this code:

[self.navigationItem setTitle:@"Your Title"];

and you're good to go! This code will automatically lay its self to the center of the navigation bar.

Or if you actually have an instance of UINavigationController, use:

[self setTitle:@"Your Title"];

You need to set self.title = "" in the viewWillDisappear method in the previous viewcontroller.

Eg. ViewControllerB is opening from ViewControllerA then set self.title = "" in ViewControllerA's "viewWillDisappear" method

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        // this will appear as the title in the navigation bar
        UILabel *label = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
        label.backgroundColor = [UIColor clearColor];
        label.font = [UIFont boldSystemFontOfSize:20.0];
        label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
        label.textAlignment = UITextAlignmentCenter;
        label.textColor = [UIColor yellowColor]; // change this color
        self.navigationItem.titleView = label;
        label.text = NSLocalizedString(@"PageThreeTitle", @"");
        [label sizeToFit];
    }

    return self;
}

Just call this in ViewDidLoad

- (void)prepareNavigationTitle
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(-50, 0.0, 240.0, 40.0)];
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont boldSystemFontOfSize:14.0];
    label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
    label.textAlignment = UITextAlignmentLeft;
    label.textColor =[UIColor whiteColor];
    label.text=offertit;
    self.navigationItem.titleView = label;
}

如果你有一个导航控制器,那么只需添加:self.title = @“你的标题”;

Try using self.navigationItem.title = @"YourTitle"; this code will place the title in the center of the navigation bar .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM