【问题标题】:How to align 2 buttons to left & right border of a UIToolbar如何将 2 个按钮与 UIToolbar 的左右边框对齐
【发布时间】:2015-01-22 17:22:46
【问题描述】:

我一直在尝试 Cocoa 和 Swift(我是菜鸟),但遇到了一个我似乎无法解决的问题:

假设我创建了一个 UIToolbar 并向其中添加了两个按钮:“取消”和“接受”。我想将取消按钮对齐到 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 来创建空间,但这不是动态的(这里的许多答案都建议使用这个解决方案 - 不好)。所以我期待我会得到 UIBarButtonItems 的框架,但我似乎也无法完成这项工作,它们没有 .frame 属性。所以在另一个 SO 答案中,这是建议:

UIView *view= (UIView *)[self.toolbar.subviews objectAtIndex:0]; // 0 for the first item

这也不起作用,似乎没有直接的 subview-child 是 UIBarButtonItem(因此它返回宽度为 375)。

设置标签是不行的...:

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

致命错误:在展开可选值时意外发现 nil

(lldb)

有什么办法吗? :-/ 任何帮助表示赞赏。

【问题讨论】:

    标签: ios objective-c swift


    【解决方案1】:

    是的,只需在两者之间添加一个灵活的空间。

    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]
    

    不确定这是否是正确的语法,但该类仍然是UIBarButtonItem。唯一的区别是 systemItem 是UIBarButtonSytemItem.FlexibleSpace

    评论:

    所以在另一个 SO 答案中,这是建议:

    UIView *view= (UIView *)[self.toolbar.subviews objectAtIndex:0]; // 0 for the first item
    

    这也不起作用,似乎没有直接的 subview-child 是 UIBarButtonItem(因此它返回宽度为 375)。

    不起作用的原因是因为[self.toolbar.subviews objectAtIndex:0] 是Objective-C 代码,而不是Swift 代码。

    【讨论】:

    • 如何用代码在它们之间添加一个灵活的空格?是哪个班?或者如何使该按钮灵活?
    • 就是这样!非常感谢。
    • 评论评论:我当然知道,我把它翻译成 Swift。 :) 为工具栏显示了 4 个孩子,他们都没有工作。所有这些都追踪到“is UIBarButtonItem”是错误的(或者是在幕后进行了一些转换?)并且宽度错误。
    【解决方案2】:

    您必须使用 FlexibleSpace buttonSystem 作为分隔符

    let space = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace,
                  target: nil,
                  action: nil);
    

    这会自动缩放

    干杯,

    【讨论】:

    • 谢谢,我猜你的回答比接受的答案得到“更正”的时间早,我至少会支持你的。
    • 呵呵.. 谢谢,重要的是你的代码已经修复了 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-28
    • 1970-01-01
    • 1970-01-01
    • 2021-08-25
    • 2021-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多