I have an intermittent bug that is confounding me. Any advice on how to track it down or what might be the cause are greatly appreciated.
I have a “DetailView” with a few labels, an Image View and a Text View. In the navbar I also have a camera button to open an Image Picker and take a picture (later added to the image view). Basic stuff.
Sometimes, when taking a picture and then editing the text: the whole view between the navbar and the keyboard goes blank (to my background color). Happens more often the “first time”. Repeating the procedure does not give the same problem. Happens almost only on the 3Gs (very rare on the 3G and the original iPhone).
I have two theories.
1 is that it has something to do with the scroll view that is the container for the disappearing GUI elements. The view hierarchy is: ScrollView -> UIView -> labels, texts and image. Is it a bad thing to have the scrollview as the “main” view?
2 is that it has to do with memory. The 3Gs has a better camera and takes bigger pictures… Possibly something happens if the app gets a low memory warning while taking the picture (not uncommon)?
Are any of these two at all feasible? Any other ideas on what to look for?
thanks
Update:
Could two simultaneous animations cause the bug?
On the KeyboardWillShow notification I resize the ScrollView using the UIView beginAnimations … commitAnimations and right after that (which happens asynchronously I believe) I also tell the scroll view to scrollRectToVisible for the TextView.
like this:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:animationDuration];
[UIView setAnimationCurve:animationCurve];
CGRect rect = [[self view] frame];
rect.size.height -= keyboardFrame.size.height * (up? 1 : -1);
[[self view] setFrame: rect];
[UIView commitAnimations];
// Scroll the active text field into view.
DetailView *tempScrollView = (DetailView *) [self view];
CGRect textFieldRect = [comments frame];
[tempScrollView scrollRectToVisible:textFieldRect animated:YES];
I managed to track it down in the Sim.
Here it goes:
Under these circumstances the view will receive TWO UIKeyboardWillShowNotification are eachother… so my copied example code for resizing the view is run twise… making it 416px – 216px – 216px = -16px in height… not a good thing.
The reason for the double notifications were of-course that the ViewController added itself as an observer in viewDidLoad… which runs again when the view “appears” after the memory warning… but the ViewController never removed itself as an observer.
Doing that fixed the bug for sure:
}
[[NSNotificationCenter defaultCenter] removeObserver:self];
}