[英]How to align 2 buttons to left & right border of a UIToolbar
I have been experimenting a bit with Cocoa and Swift (I am a noob) and ran into a problem I cannot seem to solve: 我一直在尝试使用Cocoa和Swift(我是一个菜鸟)并遇到了一个我似乎无法解决的问题:
Let's assume I create a UIToolbar and add two buttons to it: 'Cancel' and 'Accept'. 我们假设我创建了一个UIToolbar并为其添加了两个按钮:'Cancel'和'Accept'。 I would like to align the Cancel button to the left edge of the UIToolbar (that works, of course, by default) and the Accept button to the right edge.
我想将“取消”按钮对齐到UIToolbar的左边缘(当然,默认情况下可以正常工作),将“接受”按钮对齐到右边缘。 Is there some easy way how to achieve the right alignment?
有一些简单的方法如何实现正确的对齐? This is what I have currently:
这就是我目前所拥有的:
let toolbar = UIToolbar(frame: CGRectMake(0, 0, w, 35))
toolbar.backgroundColor = UIColor(red: 0.75, green: 0.75, blue: 0.75, alpha: 0.95)
let acceptButton = UIBarButtonItem(title: "Accept", style: UIBarButtonItemStyle.Done, target: self, action: "buttonPressed:")
let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: "buttonPressed:")
let space = UIBarButtonItem(customView: UIView(frame: CGRectMake(0, 0, 50, 10))) // <- MEH! this is not dynamic!
toolbar.items = [acceptButton, space, cancelButton]
view.addSubview(toolbar)
I am using another UIBarButtonItem to create the space but this would not be dynamic (and many answers here on SO suggest this solution - no good). 我正在使用另一个UIBarButtonItem来创建空间,但这不是动态的(这里的许多答案都提出了这个解决方案 - 没有好处)。 So I was expecting I would get the UIBarButtonItems' frames but I can't seem to make this work either, they have no
.frame
property. 所以我期待我会得到UIBarButtonItems的框架,但我似乎无法使这项工作,他们没有
.frame
属性。 So in another SO answer this was the suggestion: 所以在另一个SO答案中这是建议:
UIView *view= (UIView *)[self.toolbar.subviews objectAtIndex:0]; // 0 for the first item
This doesn't work either, seems like no direct subview-child is a UIBarButtonItem (and so it returns the width as 375). 这也不起作用,似乎没有直接的subview-child是UIBarButtonItem(因此它将宽度返回为375)。
Setting tags is a no go...: 设置标签是不行的...:
let cancelButton.tag = 1
let acceptButton.tag = 0
let acceptButtonFrame = toolbar.viewWithTag(0)!.frame
let cancelButtonFrame = toolbar.viewWithTag(1)!.frame
println(acceptButtonFrame.width)
println(cancelButtonFrame.width)
Yields in: 产量:
375.0
375.0
fatal error: unexpectedly found nil while unwrapping an Optional value
致命错误:在展开Optional值时意外发现nil
(lldb)
(LLDB)
Is there some way? 有什么办法吗? :-/ Any help appreciated.
: - /任何帮助表示感谢。
Yes, just add a flexible space in between the two of them. 是的,只需在两者之间添加一个灵活的空间即可。
let toolbar = UIToolbar(frame: CGRectMake(0, 0, w, 35))
toolbar.backgroundColor = UIColor(red: 0.75, green: 0.75, blue: 0.75, alpha: 0.95)
let acceptButton = UIBarButtonItem(title: "Accept", style: UIBarButtonItemStyle.Done, target: self, action: "buttonPressed:")
let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: "buttonPressed:")
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil);
toolbar.items = [acceptButton, flexibleSpace, cancelButton]
Not sure if that is the correct syntax
, but the class is still UIBarButtonItem .
不确定这是否是正确的语法
,但该类仍然是UIBarButtonItem 。 The only difference is the systemItem is a UIBarButtonSytemItem.FlexibleSpace
. 唯一的区别是systemItem是一个
UIBarButtonSytemItem.FlexibleSpace
。
Comments: 评论:
So in another SO answer this was the suggestion:
所以在另一个SO答案中这是建议:
UIView *view= (UIView *)[self.toolbar.subviews objectAtIndex:0]; // 0 for the first item
This doesn't work either, seems like no direct subview-child is a UIBarButtonItem (and so it returns the width as 375).
这也不起作用,似乎没有直接的subview-child是UIBarButtonItem(因此它将宽度返回为375)。
The reason that doesn't work is because [self.toolbar.subviews objectAtIndex:0]
is Objective-C code, not Swift code. 不起作用的原因是因为
[self.toolbar.subviews objectAtIndex:0]
是Objective-C代码,而不是Swift代码。
You have to use the FlexibleSpace buttonSystem as a separator 您必须使用FlexibleSpace buttonSystem作为分隔符
let space = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace,
target: nil,
action: nil);
This will scale automatically 这将自动缩放
Cheers, 干杯,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.