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

当GrowingTextView library调整文本视图的高度约束时,会调整工具栏的高度,但也会将按钮向右移动:

与文本视图的大小和按钮的位置无关。它总是在文本视图第一次调整大小时向右移动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()发布于 2017-09-01 01:45:10
您需要在按钮的两侧设置灵活的空间。它将在两侧的空间中间保留一个按钮。
试试这个:
let item1 = UIBarButtonItem(customView: sendButton)
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
bottomToolbar.items = [flexibleSpace , item1, flexibleSpace]https://stackoverflow.com/questions/45986424
复制相似问题