Swift 如何在屏幕中央对UITabButtonItem进行编程? 我试着把中间的纽扣移动一下。

Swift 如何在屏幕中央对UITabButtonItem进行编程? 我试着把中间的纽扣移动一下。 ,swift,uibarbuttonitem,Swift,Uibarbuttonitem,最初,它位于左侧: let doneButton = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(ViewController.dismissKeyboard)) toolBar.setItems([doneButton], animated: false) toolBar.isUserInteractionEnabled = true 您是否知道如何通过编程将其移动

最初,它位于左侧:

let doneButton = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(ViewController.dismissKeyboard))

    toolBar.setItems([doneButton], animated: false)
    toolBar.isUserInteractionEnabled = true

您是否知道如何通过编程将其移动到屏幕中间?我试图添加灵活的空间,但它不起作用。

必须使用SystemItem:.flexibleSpace创建UIBarButtonim 并将第一个和最后一个项目添加到工具栏项目:

toolBar.setItems([UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil), doneButton, UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)], animated: false)