Search code examples
iostextviewinfinitylayoutsubviews

Infinity cycle for textView on iOS 10.2


I connected the library - JVFloatLabeledTextField to my project. I started on a real device, but the problem can be reproduced on the simulator (I tried with iphone 7 plus iOS 10.2). If you enter some text in the textView, then select the text and click "Select All". The application will be in an infinity cycle, called layoutSubviews.

I tried to fix the problem and added a custom class for the textView. I redefine the intrinsicContentSize, it works, but there are other difficulties. Tell me how to fix this problem. The test project was add into my repository: https://github.com/ed8009/textView-With-JVFloatLabeledTextView


Solution

  • You have identified the issue. The problem is with this part of the code.

    if (!self.scrollEnabled && !CGSizeEqualToSize(self.bounds.size, [self intrinsicContentSize])) { 
     [self invalidateIntrinsicContentSize]; 
    }
    

    Especially, [self invalidateIntrinsicContentSize];

    Upon calling this, the intrinsicContentSize and self.bounds.size never match. Thus, the view is laid out infinitely. The way to combat this to change this to [self layoutIfNeeded]; This will synchronously update the layout and fix this problem. Your code should look like this.

    if (!self.scrollEnabled && !CGSizeEqualToSize(self.bounds.size, [self intrinsicContentSize])) {
        [self layoutIfNeeded];
    }