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

标题: ios - NavigationItem 的 titleView 动画不正确 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:44
标题: ios - NavigationItem 的 titleView 动画不正确

我已经查看了所有我能找到的类似问题,但我还没有找到解决这个问题的方法。

我在 viewDidLoad 中像这样设置 titleView 属性:

self.navigationItem.titleView = label

View Controller 是导航堆栈的一部分。当您点击前一个 View Controller 中的一行时,它会将这个 Controller 插入堆栈。完全正常的 UINavigationController 的东西。

当这个 View Controller 开始动画时,标 checkout 现在原点(左上角),然后一直停留在那里,直到 View Controller 完成动画,然后它跳转(没有动画)到导航中间的正确位置酒吧。

点击后退按钮后,标题 View 会正确显示动画,就像普通标题一样。

这是viewDidLoad中的代码:

let label = UILabel(frame: CGRect.zero)
label.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
label.backgroundColor = .red
label.text = "test"
label.sizeToFit()
self.navigationItem.titleView = label

我尝试过的事情:

这很容易通过 Xcode 中的 Master-Detail 项目模板重现。如果你想尝试一下,只需将上面的代码添加到 DetailViewController.swift 中的 configureView() 顶部。在该模板中,导航项的标题被硬编码到 Storyboard 中。您可以通过搜索“详细信息”轻松删除它。单击显示 Navigation Item: Title = "Detail" 的结果,然后从 Inspector Pane 中删除该字符串。


更新

@synndicate 的建议确实适用于上面的 UILabel 示例。但我真正的问题是 UIStackView。当我按照@synndicate 建议的方法使用堆栈 View 时,我遇到了另一个动画问题。这次标题 View 开始滑入,但动画一直到原点。动画完成后,它会捕捉到它应该在的位置。

这是 prepare(for:sender 中的代码,正如@synndicate 所建议的...

let label = UILabel(frame: CGRect.zero)
label.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
label.backgroundColor = .red
label.text = "test"
let stackView = UIStackView(frame: CGRect.zero)
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.addArrangedSubview(label)
stackView.sizeToFit()
controller.navigationItem.titleView = stackView

还有什么建议吗?

另外,这是 Xcode 8 和 iOS 10。

更新 2

我发现对于导航层次结构的根是 UINavigationControllerviewDidLoad 的 View Controller ,上面的堆栈 View 代码完美地动画(就像普通标题一样)。当导航层次结构的根是 UISplitViewController 时会出现问题。

所以我想我可以将我的问题更新为这个......

如何配置 UIStackView 将在 navigationItemtitleView 属性上设置,其中导航层次结构的根是UISplitViewController?



Best Answer-推荐答案


在将 View Controller 压入堆栈之前,您能否将代码移动到?

例如Master-Detail项目的MasterViewController.swift

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "showDetail" {
            if let indexPath = tableView.indexPathForSelectedRow {                    

                let object = objects[indexPath.row] as! NSDate
                let controller = (segue.destination as! UINavigationController).topViewController as! DetailViewController
                controller.detailItem = object
                controller.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem
                controller.navigationItem.leftItemsSupplementBackButton = true

                let label = UILabel()
                label.textAlignment = .center
                label.backgroundColor = .red
                label.text = "test"
                label.sizeToFit()

                controller.navigationItem.titleView = label

            }
        }
    }

关于ios - NavigationItem 的 titleView 动画不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44166476/






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