UIBarButtonItem flexibleSpace不起作用

时间:2017-10-26 09:01:23

标签: xcode swift4

从iOS 11开始,UIBarButtonSystemItem.flexibleSpace不起作用。

我的源代码:

let backupBtn = UIBarButtonItem(image: UIImage(named: "itemRate.png"),
                                    style: UIBarButtonItemStyle.plain,
                                    target: self,
                                    action: #selector(goToBackup))

let sortBtn = UIBarButtonItem(image: UIImage(named: "itemSize.png"),
                                  style: UIBarButtonItemStyle.plain,
                                  target: self,
                                  action: #selector(changeSort))

let sizeBtn = UIBarButtonItem(image: UIImage(named: "itemSize.png"),
                                  style: UIBarButtonItemStyle.plain,
                                  target: self,
                                  action: #selector(changeSize))

let space = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace,
                                target: nil,
                                action: nil)

self.navigationItem.setRightBarButtonItems([sizeBtn, space, sortBtn, space, backupBtn], animated: true)

如果我使用:

  • self.navigationItem.setRightBarButtonItems
  • self.navigationItem.setLeftBarButtonItems

按钮出现在左侧或右侧,但始终在一起!!!

1 个答案:

答案 0 :(得分:0)

蹩脚的解决方案!

您可以根据需要在左侧或右侧使用此解决方案:

let space = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.fixedSpace, target: nil, action: nil)

space.width = (self.view.frame.size.width/2) - (40*UIScreen.main.scale)

self.navigationItem.leftBarButtonItem = backupBtn

self.navigationItem.rightBarButtonItems = [sortBtn, space, sizeBtn]

可怕,我知道!