2017-08-31 90 views
0

我有一个UIToolbar与GrowingTextView,一个固定的空间和一个UIBarButtonItem。看起来像:如何在更改工具栏高度时防止按钮向右移动?

GrowingTextView and button

GrowingTextView library大小调整的TextView的高度约束,重新调整工具栏的高度,但也动按钮向右:

GrowingTextView and wrong button 没有问题到底有多大的文本视图和按钮的位置。第一次文本视图调整大小时,它总是像20像素一样移动。只有第一次。

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

代码与固定空间添加一个按钮:

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() 
+0

当你强迫/会发生什么事触发布局的观点已经出现之后? –

+0

在你的约束中:'H:| -8- [textView] -60- |',为什么你没有任何按钮?我的意思是'H:| -8- [textView] -10- [button] -60-'' – Honey

+0

@Honey我试过了,但显然不能这样工作。与此,你得到[这](https://www.dropbox.com/s/clpv6q3onz4o4pe/IMG_0023.PNG?dl=0)(没有文字视图) – Ivan

回答

0

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

试试这个:

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

感谢您的回答。但即使我在右侧设置了灵活或固定的空间,仍然是同样的问题。 – Ivan

相关问题