我有一个视图,这是完全使用自动布局编程。我在视图的中间有一个UITextView,上面和下面都有项目。一切都很好,但我希望能够在添加文本时扩展UITextView。随着它的扩展,这应该会把它下面的所有东西都压下来。
我知道怎么做“弹簧和支柱”的方式,但有没有这样做的自动布局方式?我能想到的唯一方法是在每次需要增长时删除并重新添加约束。
发布于 2013-07-18 20:09:56
包含UITextView的视图将由AutoLayout使用setBounds
指定其大小。所以,这就是我所做的。superview最初设置了所有其他约束,最后我为UITextView的高度设置了一个特殊的约束,并将其保存在一个实例变量中。
_descriptionHeightConstraint = [NSLayoutConstraint constraintWithItem:_descriptionTextView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:0.f
constant:100];
[self addConstraint:_descriptionHeightConstraint];
在setBounds
方法中,我随后更改了常量的值。
-(void) setBounds:(CGRect)bounds
{
[super setBounds:bounds];
_descriptionTextView.frame = bounds;
CGSize descriptionSize = _descriptionTextView.contentSize;
[_descriptionHeightConstraint setConstant:descriptionSize.height];
[self layoutIfNeeded];
}
发布于 2014-01-22 23:38:18
摘要:禁用文本视图的滚动,并且不限制其高度。
要以编程方式完成此操作,请将以下代码放入viewDidLoad
中
let textView = UITextView(frame: .zero, textContainer: nil)
textView.backgroundColor = .yellow // visual debugging
textView.isScrollEnabled = false // causes expanding height
view.addSubview(textView)
// Auto Layout
textView.translatesAutoresizingMaskIntoConstraints = false
let safeArea = view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
textView.topAnchor.constraint(equalTo: safeArea.topAnchor),
textView.leadingAnchor.constraint(equalTo: safeArea.leadingAnchor),
textView.trailingAnchor.constraint(equalTo: safeArea.trailingAnchor)
])
要在Interface Builder中执行此操作,请选择文本视图,取消选中Attributes Inspector中启用的滚动,然后手动添加约束。
注意:如果在文本视图的上方/下方有其他视图,请考虑使用UIStackView
将其全部排列。
发布于 2017-04-13 16:16:16
你可以通过故事板来实现,只需禁用“启用滚动功能”:)
https://stackoverflow.com/questions/16868117
复制相似问题