Right navigation button moves when warning is disabled

I am adapting an iOS 6 application to iOS 7, and I am experiencing a strange โ€œbugโ€. There is a rightBarButtonItem on the screen with a simple image that is shown in its place. But, if the application shows a warning, the image moves down (50 pixels or so) when I click the OK button of the warning (the only button in this warning). There is no action associated with this warning, it is only informative.

In addition, if I change the image (setImage) of the button, this image will be inappropriate.

+4
source share
3 answers

Well, finally, I found a solution:

I had UIBarButtonItemwith UIBarButtonItemStylePlainand the image configured using setImageon UIBarButtonItem.

To solve the problem, I created UIButtonwith the image (setting its frame to CGRectMake), and then created UIBarButtonItemwith initWithCustomViewand using UIButtonas CustomView. Thus, the image will always be 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;
+6
source

Thanks for this ral, I translated it to Swift for Swift users:

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
0
source

, , "title" "". , . YMMV.

I'm not sure why this matters, but he fixed my problem when a panel button element got an offset when firing uialertcontroller.

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

0
source

Source: https://habr.com/ru/post/1526311/


All Articles