Right navbar button moves when alertview is dismissed
Asked Answered
U

3

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.

Unaunabated answered 11/2, 2014 at 9:29 Comment(3)
Are you using AutoLayout ?Shandy
Nope. The project was done by another one and he did not use autolayout at all.Unaunabated
And this happens too when I switch to another tab and come back to this tab.Unaunabated
U
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;
Unaunabated answered 13/2, 2014 at 13:25 Comment(4)
would you be willing to display your code that fixed it. I'm experiencing the same issue and want to compare my solution.Amidst
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;Unaunabated
Sorry, I can't format the comment... If it works, could you mark my answer as valid? ThanksUnaunabated
You could edit your solution and add it there, but I am good with your comment.Amidst
K
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
Kowalewski answered 26/4, 2016 at 2:4 Comment(0)
B
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

Billman answered 31/1, 2018 at 19:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.