修改两个右 UIBarButtonItems 之间的间距
Modifying spacing between two right UIBarButtonItems
我想在导航栏的右侧放置两个 UIBarButtonItems。我面临的主要问题是按钮之间的间距(最右边的按钮和导航视图边框之间的间距已经解决,多亏了这个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),则没有明显效果。我需要让按钮靠近一点,我该怎么做?
您实际上无法控制 UIBarButtonItem 之间的间距。您可以更改 UIBarButtonItem 的 width
,仅此而已。 UIButtonItem 不是 UIView;它没有框架,所以你不能说它的位置。如果你想更好地控制界面的外观,你将不得不使用一个 UIBarButtonItem,它本身包含一个自定义视图 (init(customView:)
)。现在你在 UIView 的世界里,你可以设置它的子视图的框架,它可以是真正的按钮或任何东西。
我想在导航栏的右侧放置两个 UIBarButtonItems。我面临的主要问题是按钮之间的间距(最右边的按钮和导航视图边框之间的间距已经解决,多亏了这个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),则没有明显效果。我需要让按钮靠近一点,我该怎么做?
您实际上无法控制 UIBarButtonItem 之间的间距。您可以更改 UIBarButtonItem 的 width
,仅此而已。 UIButtonItem 不是 UIView;它没有框架,所以你不能说它的位置。如果你想更好地控制界面的外观,你将不得不使用一个 UIBarButtonItem,它本身包含一个自定义视图 (init(customView:)
)。现在你在 UIView 的世界里,你可以设置它的子视图的框架,它可以是真正的按钮或任何东西。