Swift:工具栏项目出现在随机位置

问题描述 投票:0回答:1

我目前正在使用自定义工具栏实现应用。我将以编程方式添加工具栏。我希望工具栏显示图像和文本,如下所示:Image with text below

很遗憾,我编写的工具栏如下所示:enter image description here

而且我不太确定为什么它不起作用。我编写的代码如下:

  self.navigationController?.isToolbarHidden = false


    let customButton : UIButton = UIButton(type: .custom)
    customButton.setImage(UIImage(named: "Home"), for: .normal)
    customButton.imageView?.contentMode = .scaleAspectFit

    let customButton1 : UIButton = UIButton(type: .custom)
    customButton1.setImage(UIImage(named: "Account"), for: .normal)
    customButton1.imageView?.contentMode = .scaleAspectFit

    let customButton2 : UIButton = UIButton(type: .custom)
    customButton2.setImage((UIImage(named: "Ball")), for: .normal)
    customButton2.imageView?.contentMode = .scaleAspectFit


    customButton.imageEdgeInsets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
    customButton1.imageEdgeInsets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
    customButton2.imageEdgeInsets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)

    let customBarButtonItem = UIBarButtonItem(customView: customButton as UIView)
    let customBarButtonItem1 = UIBarButtonItem(customView: customButton1 as UIView)
    let customBarButtonItem2 = UIBarButtonItem(customView: customButton2 as UIView)

    var items = [UIBarButtonItem]()
    items.append( UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil))
    items.append( customBarButtonItem)
    items.append( UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil) )
    items.append( customBarButtonItem1)
    items.append( UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil) )
    items.append( customBarButtonItem2)



    self.toolbarItems = items

我知道代码看起来很糟糕,但我只想使其正常工作,但我发现没有找到适合我的解决方案。

[也许有人知道如何以编程方式构建工具栏的很好的教程,我将不胜感激。 :)

swift toolbar items
1个回答
1
投票

此自定义PagerController可能会有所帮助,我已使用它在视图顶部显示仅带有标题的选项卡,但也可以选择在ViewController的底部添加图标。

https://github.com/StepicOrg/stepik-ios/blob/master/PagerController.swift

© www.soinside.com 2019 - 2024. All rights reserved.