修改两个右UIBarButtonItems之间的间距

时间:2023-02-01 08:42:52

I want to place two UIBarButtonItems on the right side of my navigation bar. The main problem I'm facing is the spacing between the buttons (the spacing between the right most button and the border of the navigation view has been solved thanks to this post). This is the code I'm using

我想在导航栏的右边放置两个UIBarButtonItems。我所面临的主要问题是按钮之间的间隔(由于这篇文章,右边的大多数按钮和导航视图的边框之间的间距已经得到了解决)。这是我正在使用的代码

// 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)

If I try to place an additional spacer between the two button (like shown here) there is no apparent effect. I need to get the buttons a bit closer, how can I do that?

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

1 个解决方案

#1


2  

You don't really get any control over the spacing between UIBarButtonItems. You can change a UIBarButtonItem's width, but that's all. A UIButtonItem is not a UIView; it has no frame, so you can't say anything about its position. If you want finer control over the look of the interface, you'll have to use a UIBarButtonItem which itself consists of a custom view (init(customView:)). Now you're in the UIView world and you can set the frames of its subviews, which can be real buttons or whatever.

你无法控制UIBarButtonItems之间的间隔。你可以改变UIBarButtonItem的宽度,但仅此而已。UIButtonItem不是UIView;它没有坐标系,所以你不能说它的位置。如果您想要更好地控制界面的外观,您必须使用一个UIBarButtonItem,它本身包含一个自定义视图(init(customView:))。现在你在UIView中你可以设置它的子视图的框架,它可以是真正的按钮或者别的什么。

#1


2  

You don't really get any control over the spacing between UIBarButtonItems. You can change a UIBarButtonItem's width, but that's all. A UIButtonItem is not a UIView; it has no frame, so you can't say anything about its position. If you want finer control over the look of the interface, you'll have to use a UIBarButtonItem which itself consists of a custom view (init(customView:)). Now you're in the UIView world and you can set the frames of its subviews, which can be real buttons or whatever.

你无法控制UIBarButtonItems之间的间隔。你可以改变UIBarButtonItem的宽度,但仅此而已。UIButtonItem不是UIView;它没有坐标系,所以你不能说它的位置。如果您想要更好地控制界面的外观,您必须使用一个UIBarButtonItem,它本身包含一个自定义视图(init(customView:))。现在你在UIView中你可以设置它的子视图的框架,它可以是真正的按钮或者别的什么。