Visual glitches and malfunctioning magnifying glass on UITextView

I recently ran into a bug with the insertion point and selection magnifying glasses malfunctioning on a UITextView in a simple UIView that I had set up using Interface Builder.

The symptoms were that the touch-and-hold insertion point magnifying glass would appear and work properly only the first time it was used. It would then add some visual corruption to the text view and refuse to appear on subsequent touch-and-holds. The selection magnifying glass would work and, after using it, the insertion point magnifying glass would also work (but again, just once).

Even with the selection magnifying glass, however, you would get a visual glitch (a small ghost image would remain on the text view.)

At first, I thought I was seeing an identical issue to the issue that affects UITextFields in UITableViews (see the solution to that issue on StackOverflow).

That, however, was a red herring.

Here's the real reason: the UITextView didn't like that I was turning off animations on UIWindow to display the keyboard:

[UIWindow setAnimationsEnabled: NO];
[self.myTextView becomeFirstResponder];

The fix is either to not disable animations, or, if you want to disable them, to renable them afterwards.

Adding a small delay of 0.1 seconds and re-enabling animations did the trick:

- (void)showKeyboardWithoutAnimation
{
  [UIWindow setAnimationsEnabled: NO];
  [self.myTextView becomeFirstResponder];

  [self performSelector:@selector(keyboardAnimationDidEnd) withObject:nil afterDelay:0.1f ];
}

- (void)keyboardAnimationDidEnd
{
  // So that we don't screw up the
  // UITextView's magnifying glasses.
  [UIWindow setAnimationsEnabled: YES];
}