Search code examples
iosobjective-ciphoneuinavigationbaruinavigationitem

Navigation bar title is not centred in iPhone's portrait mode


I have created a ViewController Using Xib file of size wAny and hAny and i have used autolayaout and constraint to fit UI all devices and all orientation.

Everything is working fine but when i'm running app on iPhone portrait mode title of navigation bar is not centred where as when rotating it to landscape mode title is centred.

I have used following code for setting title

self.title = @"Download Schedule";

enter image description here

enter image description here

Already tried: I have tried to set custom UILabel of lesser font size to Navigation bar's title view but still i'm getting it on left side .

Here is code of my right button

 //add right bar button for more options
    UIView *navBarButtonsView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    UIBarButtonItem *item0 = [UIBarButtonItem negativeSpacerWithWidth:5];
    UIButton *moreButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    [moreButton setBackgroundImage:[UIImage imageNamed:@"ic_web"] forState:UIControlStateNormal];
    [moreButton addTarget:self action:@selector(addNewSchedule) forControlEvents:UIControlEventTouchDown];
    [navBarButtonsView addSubview:moreButton];

    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:navBarButtonsView];
    self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:item0, backButton, nil];

Solution

  • There is something else going on - something you have not told us about. I suspect that there is something wrong with how you are setting your rightButtonItems, such that there are invisible buttons taking up the space on the right. I can reproduce this issue only by deliberately making the right button really wide:

    enter image description here

    So I suspect you have applied a large width to the right button, or there are extra invisible right button items taking up the space on the right.