【问题标题】:How can i move a button in a UINavigation bar to the right如何将 UINavigationbar 中的按钮向右移动
【发布时间】:2017-07-17 21:14:07
【问题描述】:

我试图将导航栏中的右侧按钮向右移动,以使其与表格视图上的按钮对齐,这是目前的样子

我做了一个这样的按钮:

let allButton = UIButton(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
    allButton.layer.borderColor = Constants.AppColor.cgColor
    allButton.layer.borderWidth = 1
    allButton.layer.backgroundColor = UIColor.white.cgColor
    allButton.addTarget(self, action: #selector(selectAllContacts), for: .touchUpInside)
    allButton.layer.cornerRadius = 10

我将它添加到我的导航栏,如下所示:

        navigationItem.rightBarButtonItems = [UIBarButtonItem(customView: allButton), importButton]

我尝试添加图片插入、内容插入,但不起作用。

知道我还能尝试什么吗?

【问题讨论】:

    标签: swift swift3 uinavigationcontroller navigationbar


    【解决方案1】:

    这样可以吗?

    navigationItem.rightBarButtonItem = UIBarButtonItem( 标题:“菜单>”, 风格:.plain, 目标:自己, 动作:#selector(rightNavTap) )

    【讨论】:

    • 我认为这不能解决我的问题,我只需要将按钮向右移动几个像素
    【解决方案2】:

    我可以通过添加来解决这个问题

        let negativeSpacer:UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.fixedSpace, target: nil, action: nil)
        negativeSpacer.width = -10
    

    并将其附加到我的按钮数组中

            navigationItem.rightBarButtonItems = [negativeSpacer, UIBarButtonItem(customView: allButton), importButton]
    

    如果有人找到更好的解决方案,请告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-12
      • 1970-01-01
      相关资源
      最近更新 更多