【问题标题】:Scroll View dynamic height issue滚动视图动态高度问题
【发布时间】:2020-01-30 04:47:42
【问题描述】:

在我的应用程序中,我有动态高度的滚动视图。 里面有一个文本视图和一个表格视图——都禁用了动态高度和滚动。 这两个元素每次只显示一次,因此如果 Text View 可见,则 Table View 不可见。

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

如下所示:

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

第二张图片 - 切换到表格视图,它被 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 View 没有被后台 UIView 覆盖。

将不胜感激任何帮助!提前致谢!

【问题讨论】:

  • 您是如何将组件添加到视图控制器的主视图中的?这个覆盖表格视图的背景视图是什么?
  • 尝试将调用的方法放入DispatchQueue.main.async?
  • @AhmadF 我的层次结构:滚动视图 -> UIView(覆盖表格视图的那个) -> 其他视图,如文本视图、表格视图按钮等。
  • @MumtazHussain 不太明白这有什么帮助? showSpeakerInfoshowSpeakerEvents 稍后会从按钮操作中调用

标签: ios swift uitableview uiscrollview uitextview


【解决方案1】:

进一步的发现表明,这个 bug 只在文本视图有 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
}

【讨论】:

    猜你喜欢
    • 2017-01-16
    • 1970-01-01
    • 1970-01-01
    • 2016-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-03
    • 2021-11-26
    相关资源
    最近更新 更多