我希望能够不仅在水平轴上移动2个维度的按钮,而且只需要一个解决方案,我们将非常感激。
我已经看到了很多这个问题的答案,但没有一个完全奏效,至少在ios 7+上没有。 使用UIView并将按钮放在里面会改变触摸区域:
var rightMoreBarButtonItem = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
var rightMoreBarButtonImage = UIImage(named: "ic_more_22px")
rightMoreBarButtonItem.setBackgroundImage(rightMoreBarButtonImage, forState: UIControlState.Normal)
rightMoreBarButtonItem.addTarget(self, action: "moreTapped", forControlEvents: UIControlEvents.TouchUpInside)
rightMoreBarButtonItem.frame = CGRectMake(0, 0, 22, 22)
var barButton1View: UIView = UIView(frame: CGRectMake(0, 0, 22, 22))
barButton1View.bounds = CGRectOffset(barButton1View.bounds, -14, -7)
barButton1View.addSubview(rightMoreBarButtonItem)
var RightMoreBarButtonItem: UIBarButtonItem = UIBarButtonItem(customView: barButton1View)
self.navigationItem.rightBarButtonItem = RightMoreBarButtonItem
使用fixedSpace仅适用于rightBarButtonItem的第一个空格:
var space1:UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FixedSpace, target: nil, action: nil)
space1.width = -16.0
var space2:UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FixedSpace, target: nil, action: nil)
space2.width = 0.0
self.navigationItem.setRightBarButtonItems([space1, rightMoreBarButtonItem, space2, rightAddBarButtonItem ], animated: true)
我尝试过更改space1,space2的值而没有运气。我花了很多时间试图解决这个问题,但我是Swift / Obj-C的新手,而且我没有解决方案。
答案 0 :(得分:0)
您应该使用" SPACERS"
(是的,这是ObjC,我将尝试提出一个Swift解决方案,但你要做的唯一改变就是使用这个" UIBarButtonSystemItemFlexibleSpace" 创建UIBarButtonItems p>
例如:
_invitePlaceHolderForExampleBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"some_image"] style:UIBarButtonItemStylePlain target:self action:@selector(doSomething)];
_homePlaceHolderForExampleBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"some_image"] style:UIBarButtonItemStylePlain target:self action:@selector(doSomethingMore)];
[[self navigationItem] setRightBarButtonItems:[[NSArray alloc] initWithObjects:_invitePlaceHolderForExampleBarButtonItem, _homePlaceHolderForExampleBarButtonItem, nil]];
制作此内容:
那很明显,对吗?
现在,请看1个SPACER:
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
_invitePlaceHolderForExampleBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"some_image"] style:UIBarButtonItemStylePlain target:self action:@selector(doSomething)];
_homePlaceHolderForExampleBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"some_image"] style:UIBarButtonItemStylePlain target:self action:@selector(doSomethingMore)];
[[self navigationItem] setRightBarButtonItems:[[NSArray alloc] initWithObjects:_invitePlaceHolderForExampleBarButtonItem, spacer,_homePlaceHolderForExampleBarButtonItem, nil]];
制作此内容:
并且,它产生了这个HIT区域:
你会注意到,非常细的线条显示了UIBarButtonItems的命中区域,它们就像它们应该一样
现在,请考虑使用3个SPACERS:
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
_invitePlaceHolderForExampleBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"some_image"] style:UIBarButtonItemStylePlain target:self action:@selector(doSomething)];
_homePlaceHolderForExampleBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"some_image"] style:UIBarButtonItemStylePlain target:self action:@selector(doSomethingMore)];
[[self navigationItem] setRightBarButtonItems:[[NSArray alloc] initWithObjects:spacer, _invitePlaceHolderForExampleBarButtonItem, spacer,_homePlaceHolderForExampleBarButtonItem, spacer, nil]];
制作此内容:
并且,它产生了这个HIT区域:
只需使用各种间隔物即可,并尝试强制UIView进入导航栏。几乎所有看起来像苹果公司都无法做到的东西都是苹果公司已经做过的事情。这是一个文档问题,Apple可以更好地将这样的事情指向开发人员而不是开发人员试图破解解决方案,但我离题了,这将有效,只需使用你已经拥有的东西你。
这有效:
import UIKit
class DetailViewController: UIViewController {
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.title = "Example"
var img = UIImage(named: "thw")
var space1:UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: self, action: nil)
space1.width = 50
var b = UIBarButtonItem(image: img, style: .Plain, target: self, action: "sayHello")
var a = UIBarButtonItem(image: img, style: .Plain, target: self, action: "sayHello")
self.navigationItem.setRightBarButtonItems([b, space1, a], animated: true)
}
func sayHello() {
println("got here")
}
override func viewDidLoad() {
super.viewDidLoad()
self.view?.backgroundColor = UIColor .redColor()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
产生此输出,没有子类化:
你将不得不与间隔物混在一起使这项工作,但这是有效的。我没有为你提供更多的答案,你只需要在这里和那里进行调整,但是从我之前向你展示的例子中,你正在使用"修正宽度"间隔器,我没有使用固定宽度,即使我刚刚将swift示例中的那些定义为灵活,这是否会产生影响?我不知道,但它没有改变标题。