ios - 更改工具栏高度时如何防止按钮向右移动?

标签 ios iphone swift swift3

我有一个带有 GrowingTextView、固定空间和 UIBarButtonItem 的 UIToolbar。看起来像:

GrowingTextView and button

GrowingTextView library调整 TextView 的高度约束的大小,调整工具栏的高度的大小,同时将按钮移动到右侧:

GrowingTextView and wrong button 无论 TextView 有多大以及按钮的位置有多大。第一次调整 TextView 大小时,它总是向右移动 20 像素。只是第一次。

如何解决?我尝试在视觉模式下编写约束,但我无法以这种方式获得此布局。我也尝试过刷新固定空间。

添加固定间距Button的代码:

let item1 = UIBarButtonItem(customView: sendButton)
fixedSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.fixedSpace, target: nil, action: nil)
fixedSpace.width = UIApplication.shared.windows[0].frame.width - rightMargin - sendButton.frame.width
bottomToolbar.items = [fixedSpace, item1]

添加GrowingTextView的代码:

let views: [String: Any] = ["textView": messageToSendTextView]
let hConstraints = NSLayoutConstraint.constraints(withVisualFormat: "H:|-8-[textView]-60-|", options: [], metrics: nil, views: views)
let vConstraints = NSLayoutConstraint.constraints(withVisualFormat: "V:|-8-[textView]-8-|", options: [], metrics: nil, views: views)
bottomToolbar.addConstraints(hConstraints)
bottomToolbar.addConstraints(vConstraints)
self.view.layoutIfNeeded()

最佳答案

按钮两侧需要设置弹性空间。它将在两侧的空间中间保留一个按钮。

试试这个:

let item1 = UIBarButtonItem(customView: sendButton)
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
bottomToolbar.items = [flexibleSpace , item1, flexibleSpace]

关于ios - 更改工具栏高度时如何防止按钮向右移动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45986424/

相关文章:

ios - 延迟重试 - RxSwift

ios - 当还需要返回对象时,释放对象的正确方法是什么

objective-c - 带 iOS 的 Airstash

ios - 边框文本域

json - 在 Swift 中循环遍历 JSON 对象

ios - 显示您关注的用户的帖子 - swift

iphone - IOS + phone-gap 应用程序开发中的 db 文件实用程序

iphone - uiview屏幕淡入淡出

iphone - GH 单位和 "Undefined symbols"错误

iphone - 使用 CGRectMake 移动 subview 会导致崩溃