UITextView textContainer.exclusionPaths not working well

14 Views Asked by At

I have a UITableViewCell with a UITextView and a button that appears when the tableview and cell are in edit mode (isEditing = true). I set the editing mode with setEditing(, animated: ) in my view controller. In the cell, I need that no text appears behind the button when it is visible. This is controlled within layoutSubviews() in my cell class:

import UIKit

class NoteTableViewCell: UITableViewCell {
    
    @IBOutlet weak private var noteTextView: UITextView!
    @IBOutlet weak var editNoteButton: UIButton!
    
    var note: String? {
        didSet {
            noteTextView.text = note
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        if isEditing {
            guard let exlusionFrame = editNoteButton.frame(in: noteTextView) else {return}
            noteTextView.textContainer.exclusionPaths = [UIBezierPath(rect: exlusionFrame)]
            editNoteButton.isHidden = false
        }
        else {
            noteTextView.textContainer.exclusionPaths = []
            editNoteButton.isHidden = true
        }
        // super.layoutSubviews() Putting this here does not change anything
    }
}

Even though the code is executed as expected when the view controller is set to edit mode, there is text behind the button. See next picture.

The result is not as desired

I found out that if I put the iPhone in landscape mode while in edit mode, the text is then drawn as expected and the behaviour is also as expected after Save/Edit, even when back to portrait mode.

Expected result in edit mode

Expected result in non edit mode

Does someone has an idea about what is wrong and how to fix it?

Thank you.

0

There are 0 best solutions below