滚动视图动态高度问题

Scroll View dynamic height issue

在我的应用程序中,我有动态高度的滚动视图。 它里面有一个文本视图和一个 Table 视图 - 都具有动态高度和滚动禁用。 这两个元素每次只显示一次,因此如果文本视图可见,那么 Table 视图不可见。

我的问题是在屏幕加载后滚动视图高度计算不正确,当您第一次切换到 Table 视图时 - 它被背景 UIView 覆盖。

这是它的样子:

第一张图片 - 屏幕刚打开,初始位置。

第二张图片 - 切换到 table 视图,它被 bg 视图覆盖。

这是我的代码:

override func viewDidLoad() {
    super.viewDidLoad()
    output?.viewIsReady()

    setSpeakerData()

    contentTableView.register(UINib(nibName: "SpeakerEventsTableViewCell", bundle: nil), forCellReuseIdentifier: "speakerEventsTableViewCell")
}

override func viewWillAppear(_ animated: Bool) {
    showSpeakerInfo()
}

func showSpeakerInfo() {
    aboutSpeakerTextView.isHidden = false
    contentTableView.isHidden = true
    aboutSpeakerTextView.sizeToFit()
    aboutSpeakerView.backgroundColor = blueColor
    eventsView.backgroundColor = UIColor.clear
    contentTableViewHeight.constant = aboutSpeakerTextView.frame.height
}

func showSpeakerEvents() {
    aboutSpeakerTextView.isHidden = true
    contentTableView.isHidden = false
    aboutSpeakerView.backgroundColor = UIColor.clear
    eventsView.backgroundColor = blueColor
    contentTableViewHeight.constant = contentTableView.contentSize.height
    contentTableView.reloadData()
}

奇怪的是,当您多次在选项卡之间切换时 - 一切开始正常工作,并且 Table 视图不会被背景 UIView 覆盖。

如有任何帮助,我们将不胜感激!提前致谢!

进一步发现,只有当文本视图有1行文本时才会出现此错误。当它有 2+ 行时 - 它消失了。我不知道是 Xcode、Swift 还是我导致的)

因此,为了克服这个错误,我在从服务器收到的原始文本中添加了一行明文,并且它正常工作。

这不是推荐的解决方案,但既然它有效 - 为什么不呢。

这是我的代码现在的样子:

func setSpeakerData() {
    let originalTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor(red: 0.41, green: 0.43, blue: 0.51, alpha: 1.0),
                          NSAttributedString.Key.font: UIFont(name: "Roboto-Light", size: 14.0) as Any]
    let dummyTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.clear,
                               NSAttributedString.Key.font: UIFont.systemFont(ofSize: 15)]

    let partOne = NSMutableAttributedString(string: speaker[0].speakerDetailedText, attributes: originalTextAttributes)
    let partTwo = NSMutableAttributedString(string: randomText, attributes: dummyTextAttributes)

    let combination = NSMutableAttributedString()

    combination.append(partOne)
    combination.append(partTwo)

    speakerImageView.kf.setImage(with: URL(string: speaker[0].speakerImage), placeholder: UIImage(named: "PlaceholderImage"))
    speakerNameLabel.text = speaker[0].speakerName
    speakerPositionLabel.text = speaker[0].speakerPosition

    aboutSpeakerTextView.attributedText = combination
}