【问题标题】:Make text as UINavigationController back button将文本设为 UINavigationController 后退按钮
【发布时间】:2021-06-03 13:43:58
【问题描述】:

我需要做这种导航栏

但我不知道如何将 backBarButton 设置为文本。我应该创建将替换我的导航栏的 xib 还是我可以通过编程来完成它

【问题讨论】:

    标签: swift uinavigationbar


    【解决方案1】:

    在 viewDidLoad 中设置您的自定义按钮导航栏:

    let button = UIButton(type: .custom)
        //Set the image
        button.setImage(UIImage(systemName: "chevron.backward"), for: .normal)
        //Set the title
        button.setTitle("Yourtitle", for: .normal)
        //Add target
        button.addTarget(self, action: #selector(callMethod), for: .touchUpInside) //call button tap action
        // Add insets padding
        let spacing: CGFloat = -10 // the amount of spacing to appear between image and title
        button.imageEdgeInsets = UIEdgeInsets(top: 0, left: spacing, bottom: 0, right: 0)
        button.titleEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) // customize your button titleEdgeInsets if you want
        //Create bar button
        let barButton = UIBarButtonItem(customView: button)
        navigationItem.leftBarButtonItem = barButton
    

    然后创建你的按钮点击功能:

    @objc fileprivate func callMethod() {
        print("Your code here")
    }
    

    【讨论】:

    • 赞成,但有些代码可能不需要。特别是与图像和框架相关的东西。诀窍 - 这是您的代码中 is 需要的部分 - 是(1)创建一个看起来像 UIBarButton 所需的 UIButton,然后(2)使用它创建一个条形按钮UIBarButtonItem(customView:) 最后 (3) 使用 navigationItem.leftBarButtonItem 使其成为导航栏的左栏按钮。
    • @dfd 我添加了所有参数以显示如何操作,因此您可以根据需要自定义它以使用它。如果你看一下 button.titleEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0),所有变量都是 0.... 通常你是对的 :)
    猜你喜欢
    • 2010-11-29
    • 2011-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多