首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIView Constraints TextView contentSize错误

UIView Constraints TextView contentSize错误
EN

Stack Overflow用户
提问于 2019-05-28 07:42:29
回答 1查看 74关注 0票数 0

我有一个返回UIView的函数。我想根据UIView(fullView)中显示的文本数量动态地更改UITextView(thirdTextView)的大小。

我已经尝试设置高度依赖于thirdTextViewsizeThatFits,但它输出"30“每次?我做错了什么?

代码语言:javascript
复制
let fullView = UIView()

    fullView.backgroundColor = UIColor(red:0.82, green:0.83, blue:0.85, alpha:1.0)

    let firstButton = UIButton()
    firstButton.translatesAutoresizingMaskIntoConstraints = false
    let secondButton = UIButton()
    secondButton.translatesAutoresizingMaskIntoConstraints = false

    let thirdTextView = UITextView()
    thirdTextView.translatesAutoresizingMaskIntoConstraints = true
    thirdTextView.text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book."
    thirdTextView.backgroundColor = .clear
    thirdTextView.isScrollEnabled = false

    firstButton.setTitle("Button1", for: .normal)
    firstButton.setTitleColor(.black, for: .normal)
    firstButton.titleLabel?.font = UIFont.systemFont(ofSize: 15, weight: UIFont.Weight.medium)
    firstButton.contentHorizontalAlignment = .left

    secondButton.setTitle("Button2", for: .normal)
    secondButton.setTitleColor(.black, for: .normal)
    secondButton.titleLabel?.font = UIFont.systemFont(ofSize: 15, weight: UIFont.Weight.medium)
    secondButton.contentHorizontalAlignment = .right

    let descriptionBarStackView =  UIStackView(arrangedSubviews: [firstButton, UIView() ,secondButton])
    descriptionBarStackView.translatesAutoresizingMaskIntoConstraints = false
    descriptionBarStackView.axis = .horizontal
    descriptionBarStackView.alignment = .fill

    let viewWithStackViews = UIStackView(arrangedSubviews: [descriptionBarStackView, thirdTextView])
    viewWithStackViews.translatesAutoresizingMaskIntoConstraints = false
    viewWithStackViews.axis = .vertical
    viewWithStackViews.layoutMargins = UIEdgeInsets(top: 15, left: 10, bottom: 5, right:10)
    viewWithStackViews.isLayoutMarginsRelativeArrangement = true

    fullView.addSubview(viewWithStackViews)

    NSLayoutConstraint(item: descriptionBarStackView, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 30).isActive = true

    NSLayoutConstraint.activate([
        viewWithStackViews.topAnchor.constraint(equalTo: fullView.topAnchor, constant: 0),
        viewWithStackViews.leadingAnchor.constraint(equalTo: fullView.leadingAnchor, constant: 0),
        viewWithStackViews.trailingAnchor.constraint(equalTo: fullView.trailingAnchor, constant: 0),
        viewWithStackViews.bottomAnchor.constraint(equalTo: fullView.bottomAnchor, constant: 0),
        ])


    let test = thirdTextView.sizeThatFits(thirdTextView.frame.size).height
    print(test)

    fullView.frame = CGRect(x: 0, y: 0, width: textView.frame.width - 10, height: test)

    fullView.layer.cornerRadius = 5

    return fullView
}
EN

回答 1

Stack Overflow用户

发布于 2019-05-28 07:45:36

这里的问题是您在fullViewview之间添加了约束,而它不是一个子视图

代码语言:javascript
复制
 fullView.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -10).isActive = true
 fullView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
 fullView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true

也许你错过了

代码语言:javascript
复制
self.view.addSubview(fullView)

用于渲染的方法有很多种

1-给它一个框架

2-提供视图宽度/高度

3-将其添加为子视图,但将其隐藏

最好的选择是第一个

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56333483

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档