2017-08-31 50 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

相關問題