OGeek|极客世界-中国程序员成长平台

标题: ios - 设置 UIBarButtonItem 动画在 iOS 11 上不起作用 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:04
标题: ios - 设置 UIBarButtonItem 动画在 iOS 11 上不起作用

从 iOS 11 开始,animated 参数似乎在 setLeftBarButtonItems(_, animated: Bool), setRightBarButtonItems(_, animated 中被忽略了code>、setLeftBarButton(_, animatedsetRightBarButton(_, animated 方法。

我为 UIViewController 子类编写了这个简单的代码:

override func viewDidLoad() {
    super.viewDidLoad()

    let rightButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(changeRightBarButtonItem(_))
    navigationItem.rightBarButtonItem = rightButton
}

@objc
func changeRightBarButtonItem(_ sender: Any) {
    let newRightButton = UIBarButtonItem(title: "Cancel", style: .plain,
                                             target: self, action: #selector(changeRightBarButtonItem(_))
    navigationItem.setRightBarButtonItems([newRightButton], animated: true)
}

这在 iOS 10 上按预期运行,但在 iOS 11 上,animatedtrue 还是 false 没有区别。

它似乎是 iOS 11,但也许我错过了一些东西。任何提示让它工作?即使是解决方法也会很有趣。



Best Answer-推荐答案


这似乎是 iOS 11 导航栏实现中的一个错误。

引用:

这些 setLeftBarButtonItem:animated:setRightBarButtonItem:animated:setLeftBarButtonItems:animated:setRightBarButtonItems:animated: code> 根本不是真正的动画项目。

我认为这也是一个 iOS 11 实现的错误,因为在这些 mtehods 的调用链中,最终都会调用 updateTopNavigationItemAnimated:,但是,当前的实现是这样的。

-[_UINavigationBarVisualProviderModernIOS updateTopNavigationItemAnimated:]:
0000000000be5708         push       rbp
; Objective C Implementation defined at 0x13ff050 (instance method), DATA XREF=0x13ff050
0000000000be5709         mov        rbp, rsp
0000000000be570c         mov        rsi, qword [0x147f030]
; @selector(setupTopNavigationItem), argument "selector" for method _objc_msgSend
0000000000be5713         pop        rbp
0000000000be5714         jmp        qword [_objc_msgSend_11195c8]
; _objc_msgSend

显然,此方法忽略给定的动画参数,并简单地调用不带任何参数的 setupTopNavigationItem。因此,此时动画标志被忽略。

由于这种实现,在某些情况下,UIBarButton 似乎有时会保持按下状态。

来源:https://gist.github.com/niw/569b49648fcab22124e1d12c195fe595

关于ios - 设置 UIBarButtonItem 动画在 iOS 11 上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46920720/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4