Skip to content →

snewolnivekios Posts

Portrait Orientation: Multiple Scenes

Here’s a simple scenario. You need to present a set of storyboard scenes in portrait mode for compact-width layouts (iPhone, iPod). These scenes share a root navigation controller.

Create a custom subclass of UINavigationController and override the supportedInterfaceOrientations() method, like so:

class PortraitNavigationController: UINavigationController {
  override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return .Portrait
  }
}

Then in the storyboard’s navigation controller, set the “Custom Class” in Interface Builder to your subclassed UINavigationController.

PortraitNavigationController
Assigning a Custom Navigation Controller

That’s it.

On the compact-width devices (the iPhone and iPod), this will prevent a landscape orientation on all scenes presented with that navigation controller’s stack. For the iPad, either orientation will still be supported.

Note. This is technically a solution for iOS 6 and iOS 7, for Apple tells us that “as of iOS 8, all rotation-related methods are deprecated,” although this approach continues to work in iOS 8 and iOS 9 and the documentation for supportedInterfaceOrientations() makes no mention of deprecation. See “Handling View Rotations” in the UIViewController Class Reference for more information. Do you have a pure iOS 9 solution?

Leave a Comment

Inline Code Coverage Metrics

Well this was a timely find! My teammates and I were just sitting around the big screen this past week doing a code review and wondering what those numbers were along the right margin of the Xcode source view.

Code coverage in Xcode
Xcode Code Coverage (BigNerdRanch.com)

Mark Dalrymple explains:

Lines of code that were run during the test are displayed normally. Lines of code that never got run are given a shaded background. The gutter on the right-hand side of the source editor shows how many times that particular line of code was run during the test.

Mark also shares a cool tip for preventing the loading of your UI when running tests, by adding this line to your AppDelegate’s application(_: didFinishLaunchingWithOptions:) method just before your main storyboard is loaded:

if NSClassFromString(”XCTestCase”) { 
  return true 
}
Leave a Comment

Xcode Navigation HUD

Thanks to the WWDC 2015 video, Implementing UI Designs in Interface Builder, I learned, among other things, not only that Xcode supports tabs with different view configurations and that you can have multiple assistant editors open in each tab, but also that there’s a pretty nifty tip for quickly placing a file in any of those tabs, and primary or assistant editors!

Just option-shift-click on the file, and up pops this HUD…

image
Xcode Navigation HUD: Option-Shift-Click

Then select which view you want to place it in, or first add a new tab or assistant editor, and then hit return. Simple!

Leave a Comment

Container Views

I recently developed a passcode view with core functionality that allows a user to tap in a 6-digit passcode. I then added functionality to support two different modes of operation: new passcode entry and existing passcode entry. I had a single view controller that managed these two modes of operation. The mode was ultimately determined by two different calling view controllers that segued to it.

And it was just too complicated for my tastes.

So I set out this weekend to break it up. I ended up with three controllers: one for the basic passcode-entry functionality, a second for the new-passcode additions, and a third for the existing-passcode additions.

But how to handle this in Interface Builder? Thanks to Xcode 7 and the help of Mike Woelmer, container views was the answer.

Screen Shot 2016-03-05 at 10.29.51 PM
Define Once, Use Twice

As illustrated above, I simply added two additional view controllers to the storyboard, one for each mode of operation, set their Custom Class to one of the new UIViewController subclasses I’d just created, and filled their view with a Container View. Then I embedded the single passcode view controller into each by control-dragging from the container to the passcode controller.

Leave a Comment