修改两个右UIBarButtonItem之间的间距

时间:2015-02-05 01:37:43

标签: swift ios8 uinavigationbar uibarbuttonitem spacing

我想在导航栏的右侧放置两个UIBarButtonItem。我面临的主要问题是按钮之间的间距(由于这个post,最右边的按钮和导航视图边框之间的间距已经解决了)。这是我正在使用的代码

// add buttons
        let buttonEdit: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
        buttonEdit.frame = CGRectMake(0, 0, 40, 40)
        buttonEdit.setImage(UIImage(named:"iconMap"), forState: UIControlState.Normal)
        buttonEdit.addTarget(self, action: "rightNavItemEditClick:", forControlEvents: UIControlEvents.TouchUpInside)
        var rightBarButtonItemEdit: UIBarButtonItem = UIBarButtonItem(customView: buttonEdit)


        let buttonDelete: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
        buttonDelete.frame = CGRectMake(0, 0, 40, 40)
        buttonDelete.setImage(UIImage(named:"iconMap"), forState: UIControlState.Normal)
        buttonDelete.addTarget(self, action: "rightNavItemDeleteClick:", forControlEvents: UIControlEvents.TouchUpInside)

        var rightBarButtonItemDelete: UIBarButtonItem = UIBarButtonItem(customView: buttonDelete)


        let spaceFix: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FixedSpace, target: nil, action: nil)
        spaceFix.width = -10 
 // add multiple right bar button items
 self.navigationItem.setRightBarButtonItems([spaceFix,rightBarButtonItemDelete,rightBarButtonItemEdit], animated: true)

如果我尝试在两个按钮之间放置一个额外的间隔(如图here所示),则没有明显效果。我需要让按钮靠近一点,我该怎么做?

1 个答案:

答案 0 :(得分:2)

您无法控制UIBarButtonItems之间的间距。您可以更改UIBarButtonItem width,但这就是全部。 UIButtonItem不是UIView;它没有框架,所以你不能对它的位置说些什么。如果想要更好地控制界面外观,则必须使用UIBarButtonItem,它本身由自定义视图(init(customView:))组成。现在你进入UIView世界,你可以设置其子视图的帧,可以是真正的按钮或其他。