简体   繁体   English

UIStackView 中的 UITextview

[英]UITextview inside UIStackView

I added my UITextView inside UIStackView.我在 UIStackView 中添加了我的 UITextView。

But my textview is not Multiline.但是我的 textview 不是 Multiline。

Auto layout is shown as below:自动布局如下图:

在此处输入图片说明

As per Image it is just taken 1 line.根据图像,它只取了 1 行。 The textview frame shown as out of frame as per image.根据图像显示为框架外的文本视图框架。

UITextView用户界面文本视图

在此处输入图片说明 在此处输入图片说明

UIStackView界面堆栈视图

在此处输入图片说明

只要您的堆栈视图受到良好约束,如果您在UITextView上将isScrollEnabled设置为false ,它就可以在堆栈视图中调整自己的大小。

The problem is that you have fixed the height of UIStackview by giving top and bottom constraints. 问题是你通过给出顶部底部约束来修复UIStackviewheight

  1. delete the bottom constraint 删除底部约束

    在此输入图像描述

Disable textview scrolling. 禁用textview滚动。

Make your textview delegate to self and implement textViewDidChange 使textview委托给自己并实现textViewDidChange

Swift 迅速

func textViewDidChange(_ textView: UITextView) {
    view.layoutIfNeeded()
}

Objective C: 目标C:

-(void)textViewDidChange:(UITextView *)textView {
    [self.view layoutIfNeeded];
}

Make sure this method get called. 确保调用此方法。

Now your stackview should grow with your textview to multiline. 现在,您的stackview应该随着textview一起增长到多行。

Then reason that your textview is not multiline is because you have fixed the height. 那么你的textview不是多行的原因是因为你已经修正了高度。 So It will never be multiline. 所以它永远不会是多线的。

See the GIF: 查看GIF:

在此输入图像描述

Best solution I have found for this so far is to wrap the UITextView in a UIView and then setting the fixed height of the UITextView with a height anchor. 到目前为止,我发现的最佳解决方案是将UITextView包装在UIView中,然后使用高度锚点设置UITextView的固定高度。

let textView = UITextView()
let containerView = UIView()
textView.translatesAutoresizingMaskIntoConstraints = false
containerView.addSubview(textView)

textView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor).isActive = true
textView.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true
textView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor).isActive = true
textView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor).isActive = true

textView.heightAnchor.constraint(equalToConstant: 100).isActive = true
let stackView = UIStackView(arrangedSubviews: [containerView])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM