【问题标题】:Set image and title for bar button item?为栏按钮项目设置图像和标题?
【发布时间】:2016-08-12 08:43:42
【问题描述】:

我目前有一个自定义导航控制器,其中包含简单的文本按钮的条形按钮项。是否可以保留栏按钮项目的标题,但也可以将它们设置为图像(图标图像+标题下方)。

class NavigationController: UINavigationController
{
    var mode: NavigationMode = .Swipe {
        didSet {
            self.setButtonAttributes()
        }
    }

    private var leftBarButton: UIBarButtonItem!
    private var middleBarButton: UIBarButtonItem!
    private var rightBarButton: UIBarButtonItem!
    private var rightBarButton2: UIBarButtonItem!

    override func viewDidLoad()
    {
        super.viewDidLoad()
    }

    func configureNavigationItem(navigationItem: UINavigationItem)
    {
        //Configure the bar buttons text and actions


        if (self.leftBarButton == nil) {
            self.leftBarButton = UIBarButtonItem(title: "Menu1", style: .Plain,target: self, action: "menu1Pressed:")
        }
        if (self.middleBarButton == nil) {
            self.middleBarButton = UIBarButtonItem(title: "Games", style: .Plain, target: self, action: "gamesPressed:")
        }
        if (self.rightBarButton == nil) {
            self.rightBarButton = UIBarButtonItem(title: "Menu3", style: .Plain, target: self, action: "menu3Pressed:")
        }
        if (self.rightBarButton2 == nil) {
            self.rightBarButton2 = UIBarButtonItem(title: "Settings", style: .Plain, target: self, action: "settingsPressed:")
        }

        self.setButtonAttributes()

        navigationItem.leftBarButtonItems = [self.leftBarButton, self.middleBarButton, self.rightBarButton, self.rightBarButton2]

    }

更新:

  let button = UIButton(type: .System)
        button.setImage(UIImage(named: "play"), forState: .Normal)
        button.setTitle("Play", forState: .Normal)
        button.sizeToFit()

        leftBarButton = UIBarButtonItem(customView: button)

        if (self.leftBarButton == nil) {
            self.leftBarButton = UIBarButtonItem(title: "Play", style: .Plain,target: self, action: "Pressed:")
        }

【问题讨论】:

    标签: ios swift uinavigationcontroller swift2


    【解决方案1】:

    您可以创建 UIButton 实例,为其设置图像和标题,然后使用它创建您的 UIBarButtonItem:

        let button = UIButton(type: .System)
        button.setImage(UIImage(named: "YourImage"), forState: .Normal)
        button.setTitle("YourTitle", forState: .Normal)
        button.sizeToFit()
        self.leftBarButton = UIBarButtonItem(customView: button)
    

    添加动作:

        button.addTarget(self, action: #selector(self.someAction), forControlEvents: .TouchUpInside)
    

    self.someAction 在哪里

    func someAction() {
    
    }
    

    【讨论】:

    • 谢谢!我更新了我的问题。如何也将操作分配给它?
    • 效果很好。谢谢!
    • 你知道有没有办法将按钮标题放在按钮图片下方?
    • @OlgaNesterenko 当我使用您的解决方案时,图像是蓝色的。如何使图像按原样显示而不变成蓝色?
    • @ShinehahGnolaum 尝试将第二行更改为button.setImage(UIImage(named: "YourImage").withRenderingMode(.alwaysOriginal), forState: .Normal)
    【解决方案2】:

    斯威夫特 3:

        let button = UIButton(type: .system)
        button.setImage(UIImage(named: "categories_icon"), for: .normal)
        button.setTitle("Categories", for: .normal)
        button.addTarget(self, action: #selector(showCategories), for: .touchUpInside)
        button.sizeToFit()
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: button)
    

    【讨论】:

      【解决方案3】:

      创建一个 UIButton,为其设置图像和标题,并将其用作自定义图像来初始化您的UIBarButtonItem(customView:)

      如果你想让图片在按钮的右侧,你可以将按钮的semanticContentAttribute设置为.forceRightToLeft

      Swift 4 示例:

      let view = UIView()
      let button = UIButton(type: .system)
      button.semanticContentAttribute = .forceRightToLeft
      button.setImage(UIImage(named: "DocumentsIcon"), for: .normal)
      button.setTitle("Documents", for: .normal)
      button.addTarget(self, action: #selector(openDocuments), for: .touchUpInside)
      button.sizeToFit()
      view.addSubview(button)
      view.frame = button.bounds
      navigationItem.rightBarButtonItem = UIBarButtonItem(customView: view)
      

      【讨论】:

      • 请解释您的代码行,以便其他用户了解其功能。谢谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-26
      • 2014-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多