Search code examples
iosswiftuitableviewuiscrollviewuitextview

Scroll View dynamic height issue


In my application I have Scroll View with dynamic height. Inside it there is one Text View and one Table View - both with dynamic height and scrolling disabled. These two elements are presented only once per time, so if Text View is visible, then Table View is not.

My issue is that after the screen loaded Scroll View height doesn't get calculated correctly and when you switch for the first time to Table View - it get's covered by background UIView.

Here's how it looks like:

First image - screen just opened, initial position.

Second Image - switched to table view, where it got covered by bg view. First position Second position

Here's my code:

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()
}

Strange thing is that when you switch between tabs for several times - everything starts to work properly and Table View doesn't get covered by background UIView.

Would be grateful for any help! Thanks in advance!


Solution

  • Further discovery showed that this bug appears only when text view had 1 lines of text. When it had 2+ lines - it disappeared. I don't know if it's Xcode, Swift or me that led to this)

    So, to overcome this bug I've added one line of clear text to my original text received from server and it worked as it should.

    This is not a recommended solution, but since it is working - why not.

    Here's how my code looks right now:

    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
    }