【问题标题】:UIBarButtonItem falsely animating along the bar?UIBarButtonItem 错误地沿栏设置动画?
【发布时间】:2016-01-26 22:38:43
【问题描述】:

我正在尝试做一件简单的事情:在导航栏的右侧添加一个 UIBarButtonItem。看起来很简单:

UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:@"Button" style:UIBarButtonItemStylePlain target:self action:@selector(buttonTapped)];
[self.navigationItem setRightBarButtonItem:button];

但是当调用该方法时,发生了一件奇怪的事情:按钮从栏的左侧一直滑过。

右栏按钮不仅仅出现在右侧,而是出现在最左侧,快速滑过整个导航栏(在中间的标题下方),然后在右侧位置减速停止。从那时起,它完全按预期工作。

听起来很简单,但我无法让右栏按钮停止像这样滑入。我尝试将animated:NOanimated:YES 添加到setRightBarButtonItem: 方法中,但都没有任何效果。无论左侧是否有 UIBarButtonItem,都会发生这种情况。我尝试过使用setRightBarButtonItems:@[button],但幻灯片动画没有改变。

有谁知道如何在导航栏的右侧添加一个简单的 UIBarButtonItem 而不会从侧面滑入?

【问题讨论】:

  • 你在哪个方法里面调用setRightBarButtonItem? (viewDidAppear:animated 或类似的?
  • 您是如何解决问题的?下面的答案是否没有描述您正在经历的事情?

标签: ios uibarbuttonitem uinavigationitem


【解决方案1】:

您以某种方式将setRightBarButtonItem 嵌入到某种动画中。
如果您非常仔细观察,您会注意到它实际上并不是按照说法从左到右,而是从 (0, 0)(大小为 (0, 0))到其最终静止位置和尺寸。 navigationItem 在动画中被扣为人质。

为了说服自己,可以运行这个简单的sn-p:

func buttonTapped() {
    UIView.animateWithDuration(5) { () -> Void in
        let button = UIBarButtonItem(title: "Button",
                                     style: .Plain,
                                    target: self,
                                    action: "buttonTapped")
        self.navigationItem .setRightBarButtonItem(button, animated: false)
    }
}

演示

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,在我的情况下,我在 rightBarButtonItem 上添加了一个“隐藏键盘”按钮。我在 UIKeyboardWillShow 通知的选择器中执行了此操作,并且可能从动画块中调用此选择器以显示键盘。我只是在添加按钮时禁用了动画。

    UIView.setAnimationsEnabled(false)
    self.navigationItem.setRightBarButton(hideKeyboardButton, animated: true)
    UIView.setAnimationsEnabled(true)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-16
      • 1970-01-01
      相关资源
      最近更新 更多