我正在将 iOS 6 应用程序调整为 iOS 7,但遇到了一个奇怪的“错误”。在屏幕中,有一个 rightBarButtonItem,其中显示了一个简单的图像。但是,如果应用程序显示警报 View ,则当我点击警报 View 的“确定”按钮(此警报中的唯一按钮)时,图像会向下移动(50 像素左右)。没有任何操作链接到此警报 View ,它只是提供信息。
此外,如果我更改按钮的图像(setImage),该图像将出现不合适的位置。
最佳答案
好吧,我终于找到了解决方案:
我有一个带有 UIBarButtonItemStylePlain
的 UIBarButtonItem
以及在 UIBarButtonItem
上使用 setImage
设置的图像。
为了解决这个问题,我创建了一个带有图像的 UIButton
(使用 CGRectMake
设置其框架),然后创建了 UIBarButtonItem
与 initWithCustomView
并使用 UIButton
作为 CustomView
。这样图像始终位于应有的位置。
编辑:
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;
关于cocoa - 当警报 View 关闭时,右侧导航栏按钮会移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21697868/