添加约束时不会显示文本

时间:2021-08-14 21:12:43

So here is my current code for the viewdidload and the setup view func

所以这是我目前的viewdidload和setup视图函数的代码

    override func viewDidLoad() {
        super.viewDidLoad()



        view.addSubview(bearImageView)
        view.addSubview(descriptionText)
        view.addSubview(startButton)

        setupView()

    }

    @objc private func start() {

    }

private func setupView() {
    bearImageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    bearImageView.topAnchor.constraint(equalTo: view.topAnchor, constant: 100).isActive = true
    bearImageView.widthAnchor.constraint(equalToConstant: 200).isActive = true
    bearImageView.heightAnchor.constraint(equalToConstant: 250).isActive = true

    descriptionText.topAnchor.constraint(equalTo: bearImageView.bottomAnchor, constant: 10).isActive = true
    descriptionText.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
    descriptionText.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true

    startButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    startButton.topAnchor.constraint(equalTo: descriptionText.bottomAnchor, constant: 140).isActive = true
    startButton.widthAnchor.constraint(equalToConstant: 80).isActive = true
    startButton.heightAnchor.constraint(equalToConstant: 40).isActive = true
    }

Both the bearimage and button constraints work fine (minus flipping the phone horizontally but ill fix that later) however the text just refuses to show. The text are made programmatically for istance let descriptionText = UITextView = {...}() and etc. Any of you guys have an idea?

bearimage和按钮约束都可以正常工作(减去水平翻转手机,但稍后会解决问题)但是文本只是拒绝显示。文本是以编程方式为isiance letText = UITextView = {...}()等等。你们有什么想法?

2 个解决方案

#1


1  

If you look closely you have missed the Height Constraint for your UITextView. If you're using a UILabel or UITextField they don't need a height constraint and can calculate their height based on it's inner contents but UITextView is not going to do that because it will start scrolling if the contents is more than it's height and that's why it can not set the height based on it's inner contents and it's height is zero by default.

如果你仔细观察,你就错过了UITextView的高度约束。如果您正在使用UILabel或UITextField,它们不需要高度约束,并且可以根据它的内部内容计算它们的高度,但UITextView不会这样做,因为如果内容超过它的高度它会开始滚动为什么它不能根据它的内部内容设置高度,默认情况下它的高度为零。

add a HeightConstraint to your UITextView as well.

也可以在UITextView中添加HeightConstraint。

// This will fix your problem
descriptionText.heightAnchor.constraint(equalToConstant: 120).isActive = true

#2


0  

It's possible the image's intrinsic content size is so large that it is expanding such that there is no more space available for the descriptionText label. Try updating the content compression resistance priority of the label to required so it cannot be compressed by the image view.

图像的内在内容大小可能很大,以至于没有更多可用于descriptionText标签的空间。尝试将标签的内容抗压优先级更新为所需,以便图像视图无法对其进行压缩。

Swift 3:

斯威夫特3:

descriptionText.setContentCompressionResistancePriority(UILayoutPriorityRequired, for: .vertical)

Swift 4:

斯威夫特4:

descriptionText.setContentCompressionResistancePriority(.required, for: .vertical)

#1


1  

If you look closely you have missed the Height Constraint for your UITextView. If you're using a UILabel or UITextField they don't need a height constraint and can calculate their height based on it's inner contents but UITextView is not going to do that because it will start scrolling if the contents is more than it's height and that's why it can not set the height based on it's inner contents and it's height is zero by default.

如果你仔细观察,你就错过了UITextView的高度约束。如果您正在使用UILabel或UITextField,它们不需要高度约束,并且可以根据它的内部内容计算它们的高度,但UITextView不会这样做,因为如果内容超过它的高度它会开始滚动为什么它不能根据它的内部内容设置高度,默认情况下它的高度为零。

add a HeightConstraint to your UITextView as well.

也可以在UITextView中添加HeightConstraint。

// This will fix your problem
descriptionText.heightAnchor.constraint(equalToConstant: 120).isActive = true

#2


0  

It's possible the image's intrinsic content size is so large that it is expanding such that there is no more space available for the descriptionText label. Try updating the content compression resistance priority of the label to required so it cannot be compressed by the image view.

图像的内在内容大小可能很大,以至于没有更多可用于descriptionText标签的空间。尝试将标签的内容抗压优先级更新为所需,以便图像视图无法对其进行压缩。

Swift 3:

斯威夫特3:

descriptionText.setContentCompressionResistancePriority(UILayoutPriorityRequired, for: .vertical)

Swift 4:

斯威夫特4:

descriptionText.setContentCompressionResistancePriority(.required, for: .vertical)