You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swift4项目中第二个UITextView不可见问题的解决方法

解决第二个UITextView(slogan)不可见的问题

兄弟,一眼就能看出问题出在Auto Layout约束的错误设置上,咱们拆解一下:

你给bannerText绑定了bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0),这意味着bannerText会从logo底部一直撑到屏幕最底部,把剩下的所有空间全占了。而slogan的topAnchor又绑在bannerText.bottomAnchor下面,这不直接被推到屏幕外面去了嘛,肯定看不见啊!

给你两种靠谱的解决方案,选哪种看你的需求:

方案一:手动修正Auto Layout约束

直接调整bannerText和slogan的约束,不让bannerText占满剩余空间:

private func setup(){
    logo.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true 
    logo.topAnchor.constraint(equalTo: view.topAnchor, constant: 100).isActive = true 
    logo.widthAnchor.constraint(equalToConstant: 127).isActive = true 
    logo.heightAnchor.constraint(equalToConstant: 127).isActive = true 
    
    // 移除bannerText的bottom约束,换成高度约束(自适应或固定)
    bannerText.topAnchor.constraint(equalTo: logo.bottomAnchor, constant: 29).isActive = true 
    bannerText.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true 
    bannerText.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true 
    // 方式1:固定高度(适合文本长度固定的情况)
    // bannerText.heightAnchor.constraint(equalToConstant: 35).isActive = true
    // 方式2:让高度自适应内容(因为你已经把isScrollEnabled设为false,UITextView会自动计算内容高度)
    bannerText.heightAnchor.constraint(greaterThanOrEqualToConstant: 30).isActive = true // 可选:设置最小高度
    
    // 调整slogan的约束,让它正常显示在bannerText下方
    slogan.topAnchor.constraint(equalTo: bannerText.bottomAnchor, constant: 4).isActive = true 
    slogan.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true 
    slogan.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true 
    // 同样可以让slogan自适应高度,不用绑bottom到屏幕底部
    slogan.heightAnchor.constraint(greaterThanOrEqualToConstant: 25).isActive = true
}

方案二:用UIStackView简化布局(更推荐)

如果是这种垂直排列的元素,用UIStackView能省不少事,它会自动帮你处理视图间的间距和约束,避免手动设置出错:

override func viewDidLoad() { 
    super.viewDidLoad() 
    
    // 创建垂直方向的StackView,把三个视图加进去
    let stackView = UIStackView(arrangedSubviews: [logo, bannerText, slogan])
    stackView.translatesAutoresizingMaskIntoConstraints = false
    stackView.axis = .vertical // 垂直排列
    stackView.alignment = .center // 子视图居中对齐(如果要占满宽度可以用.fill)
    stackView.spacing = 29 // 默认间距,logo和bannerText之间用这个
    stackView.setCustomSpacing(4, after: bannerText) // 单独设置bannerText和slogan之间的间距
    
    view.addSubview(stackView)
    
    // 给StackView设置约束
    stackView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    stackView.topAnchor.constraint(equalTo: view.topAnchor, constant: 100).isActive = true
    stackView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true // 让StackView和屏幕同宽
}

额外小提示

UITextView默认有内边距,如果觉得文本和边框间距太大,可以在创建textView的时候加上这两行代码:

textView.textContainerInset = .zero
textView.textContainer.lineFragmentPadding = 0

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 03:37:20