5

I'm adapting an iOS 6 app to iOS 7 and I'm experiencing an strange "error". In a screen there's a rightBarButtonItem with a simple image that is showed in his place. But, if the app shows an alertview, the image moves down (50 px or so) when I tap the OK button of the alertview (the only button in this alert). There's no action linked to this alertview, it's only informational.

Also, if I change the image (setImage) of the button, this image will appear out of place.

raul
  • 309
  • 1
  • 3
  • 15

3 Answers3

7

Well, I finally found myself a solution:

I had a UIBarButtonItem with UIBarButtonItemStylePlain and an image setted with setImage on the UIBarButtonItem.

To solve the issue, I have created an UIButton with the image (setting its frame with an CGRectMake) , and then I have created the UIBarButtonItem with initWithCustomView and using the UIButton as the CustomView. This way the image is always where it should be.

Edit:

UIButton* aButton = [UIButton buttonWithType:UIButtonTypeCustom];
aButton.frame = CGRectMake(0.0, 40.0, 30.0, 30.0);
[aButton setBackgroundImage:[UIImage imageNamed:@"anImage.png"] forState:UIControlStateNormal];
[aButton addTarget:self action:@selector(aFunction:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *anUIBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:aButton]; 
self.navigationItem.rightBarButtonItem = anUIBarButtonItem;
Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
raul
  • 309
  • 1
  • 3
  • 15
  • would you be willing to display your code that fixed it. I'm experiencing the same issue and want to compare my solution. – Ben Mar 19 '14 at 00:54
  • 1
    UIButton* aButton = [UIButton buttonWithType:UIButtonTypeCustom]; aButton.frame = CGRectMake(0.0, 40.0, 30.0, 30.0); [aButton setBackgroundImage:[UIImage imageNamed:@"anImage.png"] forState:UIControlStateNormal]; [aButton addTarget:self action:@selector(aFunction:) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *anUIBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton]; self.navigationItem.rightBarButtonItem = anUIBarButtonItem; – raul Mar 26 '14 at 13:12
  • Sorry, I can't format the comment... If it works, could you mark my answer as valid? Thanks – raul Mar 26 '14 at 13:17
  • You could edit your solution and add it there, but I am good with your comment. – Ben Mar 26 '14 at 13:18
0

Thanks for this raul, I've translated it into Swift for the Swift users out there:

let a = UIButton(type: .Custom)
a.frame = CGRectMake(0.0, 40.0, 30.0, 30.0)
a.setBackgroundImage(UIImage(named: "Share")!, forState: .Normal)
a.addTarget(self, action: "shareThis:", forControlEvents: .TouchUpInside)
let uiItem = UIBarButtonItem(customView: a)
self.navigationItem.rightBarButtonItem = uiItem
Amir
  • 281
  • 5
  • 15
0

I had a similar issue with my right nav bar button and it went away when I removed the "title" value of the offending bar button item. In my case the title should never have been set because the button uses an image. YMMV.

I'm not sure why it matters but it fixed my issue with the bar button item getting offset on uialertcontroller dismissal.

I got my inspiration from this question: UIAlertController moves leftBarButtonItem down

moliveira
  • 798
  • 8
  • 17