Monthly Archives: March 2016

iOS Size Classes, Navigation Bar Titles, UI Testing

I’m working on a particularly difficult part of my user interface and I ran into an interesting and confusing problem.  UI Testing is a new feature in Xcode.  Despite its newness, it has already helped save time in development.  However, I started using it with an app I’ve been trying to update for a while when I ran into this problem.

UI Testing depends on accessibility: a mode for those with disabilities.  As it turns out, it is highly dependent on titles or names you give different user interface items.  So, the names you give things are key to getting UI Testing in Xcode right.

In some cases, however, this can cause a problem.

I have a navigation bar that has enough elements that a title looks too stuffed into the bar when the device has a compact horizontal size class.  When regular, it looks fine.  So, I simply added a title when the display was regular, and did not add the title when compact.  This causes UI Testing problems.

The navigation item must always have the same name in the testing environment or you have to use a NSPredicate to search for the title or the class name (if you don’t set the title, then the UI Test record will record the class name).  This seems quite fragile and quite annoying since I don’t want duplicate tests for different devices unless there is a major difference in layout.

The apparent solution at this stage is to go ahead and name the navigation item to the title I want.  However, to prevent the title from appearing, I simply sent an empty UILabel to the “titleView” property of the navigation item.  This appears to work at this stage…