2016-06-10 128 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()