2016-06-10 58 views
0

我使用這個代碼來創建假設,以適應一個UIView容器內一個UITextView:UITextView的不擬合內容

// Make the cellTextView. 
let cellTextView = UITextView() 
cellTextView.backgroundColor = UIColor.clearColor() 
cellTextView.translatesAutoresizingMaskIntoConstraints = false 
cellTextView.scrollEnabled = false 
superview.addSubview(cellTextView) 

// Constrain the cellTextView. 
cellTextView.topAnchor.constraintEqualToAnchor(superview.topAnchor, constant: 8).active = true 
cellTextView.bottomAnchor.constraintEqualToAnchor(superview.bottomAnchor, constant: -8).active = true 
cellTextView.leadingAnchor.constraintEqualToAnchor(superview.leadingAnchor, constant: 8).active = true 
cellTextView.trailingAnchor.constraintEqualToAnchor(superview.trailingAnchor, constant: -8).active = true 

但它產生這樣的結果: Look at the padding

這是什麼它應該看起來像: No padding

我已經嘗試了很多不同的東西,但沒有任何修復它。 任何建議將不勝感激。

+0

什麼是你的形象限制? – CBortoli

+0

圖像不影響文本視圖。這是因爲層次結構如下所示:ImageView(Background),ContainerBar(在ImageView之上疊加),UITextView(在ContainerBar之內)。 ContainerBar根據需要展開和縮小以適應UITextView,但我猜測UITextView的內在內容大小需要多一點。圖像約束只是設置在每一邊,以便填充。 –

+0

什麼是簡單的解決方案...我所要做的就是擺脫常量,現在它完美。 –

回答

0

只是擺脫你用作邊距的常量。這將解決它。

// Make the cellTextView. 
let cellTextView = UITextView() 
cellTextView.backgroundColor = UIColor.clearColor() 
cellTextView.translatesAutoresizingMaskIntoConstraints = false 
cellTextView.scrollEnabled = false 
superview.addSubview(cellTextView) 

// Constrain the cellTextView. 
cellTextView.topAnchor.constraintEqualToAnchor(superview.topAnchor).active = true 
cellTextView.bottomAnchor.constraintEqualToAnchor(superview.bottomAnchor).active = true 
cellTextView.leadingAnchor.constraintEqualToAnchor(superview.leadingAnchor).active = true 
cellTextView.trailingAnchor.constraintEqualToAnchor(superview.trailingAnchor).active = true 
0

我認爲你必須設置高度約束

// Constrain the cellTextView. 
    cellTextView.topAnchor.constraintEqualToAnchor(superview.topAnchor, constant: 0).active = true 
    cellTextView.bottomAnchor.constraintEqualToAnchor(superview.bottomAnchor, constant: 0).active = true 
    cellTextView.leadingAnchor.constraintEqualToAnchor(superview.leadingAnchor, constant: 0).active = true 
    cellTextView.trailingAnchor.constraintEqualToAnchor(superview.trailingAnchor, constant: 0).active = true   

    cellTextView.sizeToFit() 
    cellTextView.heightAnchor.constraintEqualToConstant(self.cellTextView.contentSize.height).active = true 

也可嘗試致電

self.view.setNeedsLayout() 
    self.view.layoutIfNeeded()