You need to enable JavaScript to run this app. UITextView如何随文本自适应?带ScrollView界面如何保持元素等距约束
优惠活动
大模型
产品
解决方案
定价
更多

UITextView如何随文本自适应?带ScrollView界面如何保持元素等距约束

iOS 滚动视图内多控件布局错位问题

我开发的UIViewController包含4个UITextView与4个UILabel,各控件对应的展示文本已存入数组。我希望实现无论展示文本长度如何,UITextView与UILabel之间始终保持固定相等的间距约束,但每次尝试为该控制器添加约束时,界面上所有元素(标签、文本视图等)的布局都会出现错乱。
该控制器内置UIScrollView,对应界面截图与实现代码如下:
Xcode中DetailViewController的Storyboard截图

import UIKit

class DetailViewController: UIViewController {

    var name = ""
    var img: UIImage?
    var desc = ""
    var healingProp = ""
    var spiritualProp = ""
    var emotionalProp = ""
    
    @IBOutlet weak var detailCrystalPhotoImageView: UIImageView!
   // 水晶介绍模块
    @IBOutlet weak var crystalDescriptionTextView: UITextView!
    @IBOutlet weak var backgroundInfoLabel: UILabel!
    // 疗愈属性模块
    @IBOutlet weak var healingPropertiesLabel: UILabel!
    @IBOutlet weak var healingPropertiesInfoTextView: UITextView!
    @IBOutlet weak var scrollView: UIScrollView!
   // 精神属性模块
    @IBOutlet weak var spiritualPropertiesLabel: UILabel!
    @IBOutlet weak var spiritualPropertiesInfoTextView: UITextView!
    // 情绪属性模块
    @IBOutlet weak var emotionalPropertiesLabel: UILabel!
    @IBOutlet weak var emotionalPropertiesInfoTextView: UITextView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 配置滚动视图
        scrollView.contentSize = CGSize(width: self.view.frame.width, height: self.view.frame.height+1000)
        
        // 展示水晶标题与图片
        title = name
        detailCrystalPhotoImageView.image = img
        
        // 配置介绍模块标签与文本
        backgroundInfoLabel.text = String("About " + name + " ℹ️")
        backgroundInfoLabel.font = UIFont.systemFont(ofSize: 24, weight: .light)
       
        // 配置水晶介绍文本视图
        crystalDescriptionTextView.text = desc
        crystalDescriptionTextView.font = UIFont.systemFont(ofSize: 18, weight: .thin)
        
        // 配置疗愈属性模块标签与文本
        healingPropertiesLabel.text = String("Healing Properties 💟")
        healingPropertiesLabel.font = UIFont.systemFont(ofSize: 24, weight: .light)
        healingPropertiesInfoTextView.text = healingProp
        healingPropertiesInfoTextView.font = UIFont.systemFont(ofSize: 18, weight: .thin)
        healingPropertiesInfoTextView.sizeToFit()
        
        // 配置精神属性模块标签与文本
        spiritualPropertiesLabel.text = String("Spiritual Properties 🕉️")
        spiritualPropertiesLabel.font = UIFont.systemFont(ofSize: 24, weight: .light)
        spiritualPropertiesInfoTextView.text = spiritualProp
        spiritualPropertiesInfoTextView.font = UIFont.systemFont(ofSize: 18, weight: .thin)
        
        // 配置情绪属性模块标签与文本
        emotionalPropertiesLabel.text = String("Emotional Properties 😄")
        emotionalPropertiesLabel.font = UIFont.systemFont(ofSize: 24, weight: .light)
        emotionalPropertiesInfoTextView.text = emotionalProp
        emotionalPropertiesInfoTextView.font = UIFont.systemFont(ofSize: 18, weight: .thin)
    }
}

内容的提问来源于stack exchange,提问作者bigdogg99juherd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.27 18:15:01