cain
cain

Reputation: 1048

Items in UINavigationBar not filling width of screen

When I first load my view controller, I set up some buttons and a title for my UINavigationBar. If I load the screen in portrait, everything looks great. However, if I load the screen in landscape, the buttons only take up part of the space provided. When I rotate to portrait and back to landscape again, the items are now correctly expanding to fill the space on the NavBar.

The NavBar itself is added manually in storyboard. It is set to autosize and stretch and Autoresize Subviews is checked.

What can I do to make it always correctly display the navigation bar?

Relative setup code is displayed below.

UIBarButtonItem * saveBtn = [[UIBarButtonItem alloc] initWithTitle:@"Set" style:UIBarButtonItemStyleBordered target:self action:@selector(setField)];
UIBarButtonItem * cancelBtn = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelClicked:)];

UINavigationItem * buttonsItem = [[UINavigationItem alloc] init];


NSString * title = [self.data objectForKey:@"title"];
if(title){
    buttonsItem.title = title;
} else {
    buttonsItem.title = @"Default Title";
}

[buttonsItem setLeftBarButtonItem:cancelBtn animated:YES];
[buttonsItem setRightBarButtonItem:saveBtn animated:YES];
[navBar setItems:[[NSArray alloc] initWithObjects: buttonsItem, nil] animated:YES];

ADDITIONAL INFO: Some additional info that I had previously forgot to mention.

I am not loading this screen through a segue, instead I am doing this:

DiaryCalendarPickerViewController *navigationController = [self.storyboard instantiateViewControllerWithIdentifier:@"diaryCalendarForm"];
 ...           
[self presentViewController:navigationController animated:YES completion:nil];

Initial load into landscape

Initial load in landscape

Rotate to portrait

Rotate to portrait

Rotate back to landscape

Rotate back to landscape

Upvotes: 1

Views: 966

Answers (2)

Hussain1982
Hussain1982

Reputation: 301

I have same issue exactly , and your comment cain is the right answer , for whom have the same issue , to fix it but the code for right bar button or right navigation bar item into ViewDidAppear and it will work like charm !

Upvotes: 0

zimmryan
zimmryan

Reputation: 1099

After you layout your view but before setting nav items try calling

[buttonsItem.titleView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];

Upvotes: 1

Related Questions