如何在导航控制器上的UIBarButtonItem上添加图像和文本,就像在照片,ebay等现代应用程序上一样(请参阅工具栏(
我知道如果您手动将工具栏添加到视图,这在界面生成器中是可能的,但如果使用导航控制器,这是不可能的。
如果您在UINavigationController
中有UIViewController
,则可以使用
// System item
self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(action))
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(action))
// Tittle
self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Left Button", style: .plain, target: self, action: #selector(action))
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Right Button", style: .plain, target: self, action: #selector(action))
// Image
self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: image, style: .plain, target: self, action: #selector(action))
self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: image, style: .plain, target: self, action: #selector(action))
// Custom view
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: view)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: view)
这是我到目前为止所得到的。但是,这仅显示图像右侧的文本:
//Create a UIButton with an image on the left, and text to the right
var buttonImage = #imageLiteral(resourceName: "Lab")
var button : UIButton = UIButton(type: UIButtonType.custom)
button.setImage(buttonImage, for: UIControlState.normal);
button.setTitle("Caption", for: UIControlState.normal);