如何在Swift中以编程方式创建“Back”UIBarButton项?

时间:2015-12-15 01:00:35

标签: ios swift uibarbuttonitem

我能够创建一个UIBarButton项目,可以使用以下代码以编程方式返回:

    func backAction() -> Void {        
        self.navigationController?.popViewControllerAnimated(true)
    }
override func viewDidLoad() {
        super.viewDidLoad()
    let backButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Plain, target: self, action: "backAction")

        self.navigationItem.leftBarButtonItem = backButton
    }

问题是后退按钮没有左箭头: enter image description here 有没有办法让它看起来像带有箭头的常规后退按钮:enter image description here

我还想知道是否有办法将按钮标题名称作为前一个视图控制器的标题,如果可能的话。

由于

7 个答案:

答案 0 :(得分:34)

以下是使用UIButton和图像的代码,您可以将其添加为UIBarButtonItem的customView

$('.pov_icon_small').on('click', function (e) {
    $(this).toggleClass("pov_icon_large");
});

BackButton.png Download Link

要使用上一个视图控制器标题设置后退按钮的标题,您必须将标题作为字符串传递,同时显示控制器,将上述代码更改为

override func viewDidLoad() {
    super.viewDidLoad()
    var backbutton = UIButton(type: .Custom)
    backbutton.setImage(UIImage(named: "BackButton.png"), forState: .Normal) // Image can be downloaded from here below link 
    backbutton.setTitle("Back", forState: .Normal)
    backbutton.setTitleColor(backbutton.tintColor, forState: .Normal) // You can change the TitleColor
    backbutton.addTarget(self, action: "backAction", forControlEvents: .TouchUpInside)

    self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backbutton)
}

func backAction() -> Void {        
   self.navigationController?.popViewControllerAnimated(true)
}

这可能会有所帮助。

Swift 3

var titleStrFromPreviousController: String // This value has to be set from previous controller while presenting modal controller
backbutton.setTitle(titleStrFromPreviousController, forState: .Normal)

答案 1 :(得分:4)

您可以将视图嵌入导航控制器中。 以下是显示如何执行此操作的图像:navigationController

希望它有所帮助:D

答案 2 :(得分:3)

已针对Swift 4.2进行了更新-感谢sam bing和silentbeep

对某些颜色和动作的选择器进行了一些修改。

['{{ var2 }}']
['{{ var1 }}', '{{ var2 }}']

答案 3 :(得分:3)

对于以后的搜索,我想补充一下,您现在可以通过以下代码使用默认图标:

override public func viewDidLoad() {
    // create chevron image
    let config = UIImage.SymbolConfiguration(pointSize: 25.0, weight: .medium, scale: .medium)
    let image = UIImage(systemName: "chevron.left", withConfiguration: config)

    // create back button
    let backButton = UIButton(type: .custom)
    backButton.addTarget(self, action: #selector(self.close(_:)), for: .touchUpInside)
    backButton.setImage(image, for: .normal)
    backButton.setTitle("Back", for: .normal)
    backButton.setTitleColor(backButton.tintColor, for: .normal)
    self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backButton)
}

@IBAction func close(_ sender: UIButton) {
    self.navigationController?.dismiss(animated: true, completion: nil)
}

答案 4 :(得分:1)

第一个答案效果很好,但图像有点太大,所以使用预览并将其缩小到宽度:13和高度:22,还将其渲染模式设置为.alwaysTemplate并更改{{1将色调变为白色,同时在字符串前添加两个空格:"回到&#34 ;.这将导致类似于导航栏后退按钮的静音,图像在尺寸和位置方面可能更好。

编辑代码:

UIButton

答案 5 :(得分:0)

我从所选答案中更改了最后一行代码,它对我有用。

override func viewDidLoad() {
    super.viewDidLoad()

    addBackButton()
}

func addBackButton() {
    let backButton = UIButton(type: .custom)
    backButton.setImage(UIImage(named: "BackButton.png"), for: .normal) // Image can be downloaded from here below link
    backButton.setTitle("Back", for: .normal)
    backButton.setTitleColor(backButton.tintColor, for: .normal) // You can change the TitleColor
    backButton.addTarget(self, action: #selector(self.backAction(_:)), for: .touchUpInside)

    self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backButton)
}

@IBAction func backAction(_ sender: UIButton) {
   let _ = self.dismiss(animated: true, completion: nil)
}

答案 6 :(得分:0)

Swift 5

override func viewDidLoad() {
            super.viewDidLoad()
            let backbutton = UIButton(type: .custom)
            backbutton.setImage(UIImage(named: "BackButton.png"), for: .normal) // Image can be downloaded from here below link
            backbutton.setTitle("Back", for: .normal)
            backbutton.setTitleColor(backbutton.tintColor, for: .normal) // You can change the TitleColor
            backbutton.addTarget(self, action: Selector(("backAction")), for: .touchUpInside)
    
            self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backbutton)
        }
    
        func backAction() -> Void {
            self.navigationController?.popViewController(animated: true)
        }