ios - swift3 中带有自定义标题的自定义导航后退按钮

标签 ios swift uinavigationbar

我需要实现带有自定义背景图像和自定义标题的导航栏。例如:如果我在“使用 facebook 登录”页面,我需要在自定义背景图片之外显示“使用 facebook 登录”。为此,我遵循这样的代码:

let yourBackImage = UIImage(named: "back_button_image")
self.navigationController?.navigationBar.backIndicatorImage = yourBackImage
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = yourBackImage
self.navigationController?.navigationBar.backItem?.title = "Custom"

但是我得到的输出是这样的:

enter image description here

注意:标题是当前页面的标题。

如何实现?

最佳答案

如果后退标题太长,iOS 会将其更改为“后退”。

但是,如果您添加左栏按钮项目 - 您可以将其设置为更长的长度。

long back

来自 Apple 文档:https://developer.apple.com/documentation/uikit/uinavigationcontroller

In cases where the title of a back button is too long to fit in the available space, the navigation bar may substitute the string “Back” for the actual button title. The navigation bar does this only if the back button is provided by the previous view controller. If the new top-level view controller has a custom left bar button item—an object in the leftBarButtonItem or leftBarButtonItems property of its navigation item—the navigation bar does not change the button title.

要执行此操作,请转到 Storyboard,在 Interface Builder 中,将一个 Bar Button Item 拖到导航栏中并设置其标题。

*请注意,您需要将它连接到您的 View Controller 以弹出它。

 @IBAction func pop(_ sender: UIBarButtonItem) {
    _ = navigationController?.popViewController(animated: true)
}

关于ios - swift3 中带有自定义标题的自定义导航后退按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45019274/

相关文章:

ios - Firebase 和 Alamofire Url TableViewCell 图像加载问题 Swift 4

swift - 无法在 SwiftUI 中使用自定义 View

ios - UIViewController pushViewController,显示navigationBar

iphone - 导航栏标题过长如何处理?

ios - 改变ios导航栏的高度

ios - 是否可以安全访问存储在 AWS 上的图像?

ios - 在快速存储 NSDictionary 时出错,其中包含一些值,例如 userDefaults 中的 "<null>"

swift - UITableViewDiffableDataSource 未 deinit

ios - 给定一个自定义形状,如何增加尺寸以适应 SwiftUI 中的框架?

ios - 利用加速度计检测敲击压力强度