我正在尝试做一件简单的事情:在我的navBar的右侧添加一个UIBarButtonItem。看起来像一个简单的任务:
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:@"Button" style:UIBarButtonItemStylePlain target:self action:@selector(buttonTapped)];
[self.navigationItem setRightBarButtonItem:button];
但是当调用该方法时,会发生一件奇怪的事情:按钮从条形图的左侧一直滑过。
右边的条形按钮显示在最左侧,而不是仅显示在右侧,快速滑过整个导航栏(位于中间标题下方),并在右侧位置减速停止。从那以后,它完全按预期工作。
虽然听起来很简单,但是我无法通过这样的方式停止滑动。我尝试将animated:NO
和animated:YES
添加到setRightBarButtonItem:
方法中,无论如何都没有效果。无论左侧是否存在UIBarButtonItem,都会发生这种情况。我尝试过使用setRightBarButtonItems:@[button]
,但幻灯片动画没有变化。
有没有人知道如何在我的navBar的右侧添加一个简单的UIBarButtonItem而不从侧面滑入?
你在某种动画中嵌入了setRightBarButtonItem
。
如果你仔细观察,你会注意到它实际上并不是从每个说法从左到右,而是从(0,0)大小为(0,0)到它的最终静止位置和尺寸。 navigationItem
在动画中被扣为人质。
为了说服自己,你可以运行这个简单的片段:
func buttonTapped() {
UIView.animateWithDuration(5) { () -> Void in
let button = UIBarButtonItem(title: "Button",
style: .Plain,
target: self,
action: "buttonTapped")
self.navigationItem .setRightBarButtonItem(button, animated: false)
}
}
演示
我遇到了同样的问题,在我的情况下,我在rightBarButtonItem上添加了一个“隐藏键盘”按钮。我在UIKeyboardWillShow通知的选择器中执行了此操作,并且可能从动画块中调用此选择器以显示键盘。我刚刚添加按钮时禁用了动画。
UIView.setAnimationsEnabled(false)
self.navigationItem.setRightBarButton(hideKeyboardButton, animated: true)
UIView.setAnimationsEnabled(true)