When dismissing a modal causes your view to drop 20 pixels…
Today, I ran into a problem where I have a view displaying and I present a modal. Everything is fine, but when I dismiss the modal, the original view has now moved up about 20 pixels — very noticeably the height of a status bar.
I’ve run into this problem a few times during a few different projects. Each time, I did some quick investigation, but time pressure and opportunity induced me to just make a quick workaround fix instead. The problem has been that while I’ve seen a ton of developers report the problem, there are still no solutions!
Well, I was able to remedy it. I can’t tell you what the underlying problem is (iOS is closed source), but I eventually fixed it by pulling things and out and eventually comparing to a new iPad project. All of this happened after a couple of hours of tweaking code and my XIB with no success. In my mind, this was something deeper and out of my control, perhaps the tools.
My first hypothesis was that the XIB was being corrupted by Interface Builder. So, I created a new XIB to hook up to the view controller. I kept it empty except for a search bar at the top simply so I could gauge if it was displaying right. To my shock, even this XIB was moving down! What?!
So, I determined that it must be a problem with the UIWindow XIB. I created a new iPad project and compared it with all of the top-level pieces of my app. Everything was the same. The top level view wanted to be full screen, the size was the same, everything. What gives?
And here’s where I found my answer. It came from a hail mary, really. I noticed that whenever I’ve run into this issue, it’s always the top-level view that drops down the 20 pixels. Children always behave as expected. And so, the one difference between my app and the new app is that the new app creates the top level view controller from the UIWindow as it outlets it. I create it with code. I changed my strategy so that instead of creating the top-level view controller via code, I did it the same way and…
This makes no sense to me. Something in Apple’s code, probably the timing where the view controller is created in relation to the window, at the same time versus later, seems to be misunderstanding the Y=0 position in relation to the status bar. I simply can’t identify what the bug probably is.
Yet, it does tell me what some people see it and some don’t. I generally create new projects as Window-based rather than View-based. With View-based projects, Apple creates the view controller in the XIB. I’m willing to bet the iOS developers who see this bug are creating theirs in code.
So, give it a try and lemme know. :-)