1

I am setting a rightbarbuttonitem in from a viewController using the following code:

UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithTitle:@"Save" style:UIBarButtonItemStylePlain target:self action:@selector(saveDetails)];
self.navigationItem.rightBarButtonItem = item;

It is appearing as shown in the image:

navigationitem

As you can see, the button which appears is not vertically aligned with either th leftbuttonitem or the title.

I also tried adjusting the alignment using the code mentioned here:

[self.navigationItem.rightBarButtonItem setTitlePositionAdjustment:UIOffsetMake(0, -10) forBarMetrics:UIBarMetricsDefault];

But this removes the custom font and moves the button even higher.

navigation item with adjustment

How can I set a rightbarbuttonitem which is aligned AND maintains the custom font set using the UIAppearance proxy?

EDIT:

I even tried using

[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(saveDetails)];

but the result is exactly as shown in the first image.

UPDATE (Sep 20):

It seems that I will have to go with Alexander's answer, anybody with a cleaner solution?

Community
  • 1
  • 1
ZeMoon
  • 20,054
  • 5
  • 57
  • 98

2 Answers2

2

You can use the next variant:

UIButton *saveButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 20)];
[saveButton addTarget:self action:@selector(saveDetails) forControlEvents:UIControlEventTouchUpInside];
[saveButton setTitle: @"Save" forState:UIControlStateNormal];
[saveButton setTitleEdgeInsets:UIEdgeInsetsMake(5, 0, 0, 0)];
UIBarButtonItem *item =[[UIBarButtonItem alloc] initWithCustomView:saveButton];
self.navigationItem.rightBarButtonItem = item;
micnguyen
  • 1,419
  • 18
  • 25
Alexander Merchi
  • 1,495
  • 13
  • 11
  • I did, and the results are better. This will be my last resort, as it will take some time adjusting the button to it's right position. – ZeMoon Sep 12 '14 at 08:30
1

I tried the answer but didn't seem to work.

The solution for me was adding UIButton as UIBarButtonItem. If it sounds confusing, just drag a UIButton from Interface Builder to the position of Left or RightBarButtonItem on the navigationBar in your storyboard.

Hope it helps.

Ashik
  • 1,204
  • 12
  • 22