【问题标题】:How can I add title and image on a bar buttonitem for a toolbar of navigation controller如何在导航控制器工具栏的栏按钮项上添加标题和图像
【发布时间】:2018-09-20 08:49:55
【问题描述】:

如何在导航控制器上的 UIBarButtonItem 上添加图像和文本,就像在照片、ebay 等现代应用程序上一样(请参阅Toolbar

我知道,如果您手动将工具栏添加到视图中,这在 Interface Builder 中是可能的,但如果使用导航控制器,则这是不可能的。

【问题讨论】:

标签: ios swift uinavigationcontroller uibarbuttonitem


【解决方案1】:

这是我目前为止。但是,这仅在图像右侧显示文本:

   //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);

【讨论】:

    【解决方案2】:

    如果你在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)
    

    【讨论】:

    • 很有趣,但我想要的是同时拥有一个项目和图像。如果我在 UIBarButtonItem 上设置图像,则不会显示标题。我试图使用自定义视图并使用垂直堆栈容器,但由于某种原因它根本不会显示。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多