ios - 如何设置系统的后退按钮(backBarButtonItem)选择器?

标签 ios swift

Systems back button on the navigation bar.
我希望导航栏上的这个系统按钮有两个不同的操作。

1. navigationController?.popViewController 当设备方向为纵向时作为默认操作。
2. 当设备方向为横向时,将设备方向旋转为纵向。 UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")


我可以通过创建一个新的 UIBarButtonItem 并将其设置为 navigationItem.leftBarButtonItem 来实现。

但它没有系统的左箭头图像和本地化字符串。 我需要此按钮具有系统的左箭头图像和本地化字符串,如系统提供的。

我试着设置像这样的 Action

navigationController?.navigationBar.backItem?.backBarButtonItem?.action = #selector(self.backToPortrait)

那是行不通的。

那么如何为这个后退按钮设置 Action 呢?

最佳答案

您不能向该系统后退按钮添加操作。有两种选择。

  1. 创建您自己的后退按钮。 Here是一个例子

  2. 与其管理方向更改,不如将您以前的 View Controller 锁定为仅具有纵向模式。所以它可以为你自动旋转,比如 this

关于ios - 如何设置系统的后退按钮(backBarButtonItem)选择器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44792841/

相关文章:

objective-c - 带有 UILabel subview 缓存问题的 UITableViewCell

swift - 使用 NSUser Defaults 保存当前游戏分数(不是高分)?

ios - LayerKit iOS - newConversationWithParticipants 在 swift 2.0 中失败但 do/catch 没有从抛出的错误中捕捉到任何东西

ios - 当尝试快速使用 cocoapods 时出现错误 : bridging header does not exist?

ios - 计算 scrollView 中的精确缩放

iphone - 如何在 iOS 上的应用程序之间共享值

ios - 快速更改联系人添加按钮颜色

javascript - PhoneGap for iOS 中的通知

ios - 如何使用Unity在iPhone上加载XML文件?

ios - GCM 推送通知返回错误消息 "GCM registration is not ready with auth credentials...."