如何将 2 个按钮对齐到 UIToolbar 的左右边框
How to align 2 buttons to left & right border of a UIToolbar
我已经用 Cocoa 和 Swift(我是菜鸟)和 运行 尝试了一些我似乎无法解决的问题:
假设我创建了一个 UIToolbar 并向其添加了两个按钮:'Cancel' 和 'Accept'。我想将取消按钮对齐到 UIToolbar 的左边缘(当然,默认情况下是有效的),并将接受按钮对齐到右边缘。有没有一些简单的方法可以实现正确的对齐?这是我目前拥有的:
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)
我正在使用另一个 UIBarButtonItem 来创建 space 但这不会是动态的(SO 上的许多答案都建议这个解决方案 - 不好)。所以我期待我会得到 UIBarButtonItems 的框架,但我似乎也无法完成这项工作,它们没有 .frame
属性。所以在另一个 SO 回答中,这是建议:
UIView *view= (UIView *)[self.toolbar.subviews objectAtIndex:0]; // 0 for the first item
这也不起作用,似乎没有直接的子视图子视图是 UIBarButtonItem(因此 returns 宽度为 375)。
设置标签是不行的...:[=15=]
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)
产量:
375.0
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)
有什么办法吗? :-/ 感谢任何帮助。
是的,只是在它们两者之间添加一个灵活的space。
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]
不确定语法是否正确,但 class 仍然是 UIBarButtonItem。唯一的区别是 systemItem 是 UIBarButtonSytemItem.FlexibleSpace
.
评论:
So in another SO answer this was the suggestion:
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).
不起作用的原因是因为 [self.toolbar.subviews objectAtIndex:0]
是 Objective-C 代码,而不是 Swift 代码。
您必须使用 FlexibleSpace buttonSystem 作为分隔符
let space = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace,
target: nil,
action: nil);
这将自动缩放
干杯,
我已经用 Cocoa 和 Swift(我是菜鸟)和 运行 尝试了一些我似乎无法解决的问题:
假设我创建了一个 UIToolbar 并向其添加了两个按钮:'Cancel' 和 'Accept'。我想将取消按钮对齐到 UIToolbar 的左边缘(当然,默认情况下是有效的),并将接受按钮对齐到右边缘。有没有一些简单的方法可以实现正确的对齐?这是我目前拥有的:
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)
我正在使用另一个 UIBarButtonItem 来创建 space 但这不会是动态的(SO 上的许多答案都建议这个解决方案 - 不好)。所以我期待我会得到 UIBarButtonItems 的框架,但我似乎也无法完成这项工作,它们没有 .frame
属性。所以在另一个 SO 回答中,这是建议:
UIView *view= (UIView *)[self.toolbar.subviews objectAtIndex:0]; // 0 for the first item
这也不起作用,似乎没有直接的子视图子视图是 UIBarButtonItem(因此 returns 宽度为 375)。
设置标签是不行的...:[=15=]
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)
产量:
375.0
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)
有什么办法吗? :-/ 感谢任何帮助。
是的,只是在它们两者之间添加一个灵活的space。
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]
不确定语法是否正确,但 class 仍然是 UIBarButtonItem。唯一的区别是 systemItem 是 UIBarButtonSytemItem.FlexibleSpace
.
评论:
So in another SO answer this was the suggestion:
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).
不起作用的原因是因为 [self.toolbar.subviews objectAtIndex:0]
是 Objective-C 代码,而不是 Swift 代码。
您必须使用 FlexibleSpace buttonSystem 作为分隔符
let space = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace,
target: nil,
action: nil);
这将自动缩放
干杯,