gpt4 book ai didi

iphone - 如何手动将 'back' 按钮添加到 UINavigationBar?

转载 作者:行者123 更新时间:2023-12-03 20:17:31 25 4
gpt4 key购买 nike

我希望导航栏有一个后退按钮,可以转到最后一页,即使最后一页没有导航栏。我如何插入后退按钮,并控制用户按下它时会发生什么?

最佳答案

我想添加一个答案,但不幸的是我只有 Swift 中的正确答案。我将在这里为任何遇到这个问题并在 Swift 中寻找答案的人提供它。假设您想在导航栏中添加一个“后退按钮”以转到最后一页。执行以下操作:

    let button = UIBarButtonItem.init()
button.title = "BACK"
let attributes : [NSAttributedStringKey : Any] = [NSAttributedStringKey(rawValue: NSAttributedStringKey.font.rawValue): UIFont.systemFont(ofSize: 15.0)]
button.setTitleTextAttributes(attributes, for: .normal)
button.target = self
button.action = #selector(self.goBack)
self.navigationItem.setLeftBarButton(button, animated: false)

然后,定义一个函数(用于上面按钮操作的选择器):

@objc func goBack() {
self.performSegue(withIdentifier: "myUnwindSegue", sender: AnyObject.self)
}

请注意,这里我使用了展开转场。要使用展开转场,请参阅 here如果您有任何疑问,请在下面评论。当您第一次使用 Unwind Segue 时,它​​们可能会很棘手,但之后它们就很棒了。另请注意,当您说“最后一页”时,我假设您指的是已添加到 View 层次结构中的 View 。

关于iphone - 如何手动将 'back' 按钮添加到 UINavigationBar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5227200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com