我有一个带有 GrowingTextView、固定空间和 UIBarButtonItem 的 UIToolbar。看起来像:
当 GrowingTextView library调整 TextView 的高度约束的大小,调整工具栏的高度的大小,同时将按钮移动到右侧:
无论 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/