Links

View Controllers in iOS 8 - A quick review

High level changes

Traits replaced UIViewController methods and properties for interface orientation PFB
UIAlertController replaced UIActionSheet and UIAlertView
UISearchController replaced UISearchDisplayController
UIUserNotificationSettings replaced UIApplication methods and properties for registering notifications, supported register the types of alerts local or push notifications with some custom actions apart from remote notifications
UISplitViewController supported on iPhone as well as iPad with new collapsed property PFB
UINavigationController added options to condensing and hiding PFB
UIPresentationController provides advanced view and transition management for presented view controllers PFB
UIPrinterPickerController view controller-based way to display a list of printers
UIPopoverPresentationController manages the display of content in a popover


Methods for showing view controller are changed to;
  1. showViewController: sender: - replacement for all showing view controller methods
  2. showDetailViewController: sender: - its implimented by split view controller
and these are implemented by all custom view controllers as well.

Previously we used to differentiate orientation with UIInterfaceOrientation and UIUserInterfaceIdiom, in iOS 8 insteed of this there is a new concept of Size Classes. Size Classes have four different variations based on Vertical, Horizontal, Regular and Compact ie;
  1. Horizontal Regular - iPad landscape, iPhone portrait
  2. Horizontal Compact - iPhone landscape
  3. Vertical Regular - iPad portrait
  4. Vertical Compact - iPhone portrait, iPhone landscape
An iPad have regular size classes only.

Traits and Trait collections

Traits are the bunch of properties that we can use to determine how the layout of your application should change as its environment changes. It is consist of following properties;
  • Horizontal size class - Regular / Compact
  • Vertical size class - Regular / Compact
  • User interface idiom - iPhone / iPad / iPod Touch
  • Display scale - 1.0 / 2.0
These properties can be "Unspecified" in such case when a UIView is not in the view hierarchy or we have specified a custom Trait Collection. We can also customize the Trait Collection of a child view with Appearance Proxy. Trait Collections (a.k.a UITraitCollection) vended from trait container have both a horizontal and vertical size class trait, it used to describe a collection of traits assigned to an object.

All these Traits and its values are wrapped in a container i.e; Trait Collections. Trait Environments of a child are inherited from its parent because of this we can access Trait Environments till root from any child. We can get change notification with traitCollectionDidChange: method and override it in UIView and UIViewController subclasses to react to a trait change.

Trait Environments (a.k.a UITraitEnvironment) is a protocol which provides access to a view collection’s traits (object.traitCollection) and a way to track changes in the trait collection ([object traitCollectionDidChange:]). Its confirmed by most of the objects in view hierarchy i.e; Screen > Window > View Controller(s) > View(s) UIViewController adopts traits and the new sizing techniques for adjusting the view controller’s content. Traits are consist of Size Class, Display Scale and Idiom for a particular view object. Size Class coarsely defines the space available and categorizes available space horizontally and vertically.

Trait Collection are also implemented in UIImage so that we can provide image for any size rather then 1x and 2x versions using Assets Catalog. UIImage view automatically picks the write image based on its Trait Collection, this will be usefull in such case when we need saperate image with respect to varity of size classes.

We can also add multiple Trait Collection with the help of traitCollectionWithTraitsFromCollections:, on addition the unspecified Traits are filled by the specified and for multiple specified Traits, the last one will be picked.

UIViewController also have new methods which helps Parent View Controller to override the traits for a child, i.e;
  • -(void)setOverrideTraitCollection: forChildViewController:
  • -(UITraitCollection *)overrideTraitCollectionForChildViewController:


Split View Controller

UISplitViewController is by default collapsed for horizontally compact container (e.g; iPhone, iPod Touch..) otherwise they are expended, the same can be changed further. We can also adjust the % width of both partitions a UISplitViewController.

Good to know: We can also track split view controller expand/collapse with UIViewControllerShowDetailTargetDidChangeNotification in NSNotificationCenter

Navigation Controller Condensing

There are several awaited features are implimented in UINavigationController, auto and manual toggling nav bar i.e;
  • navController.hideBarOnTap - to enable auto toggle hide/show on tapping
  • navController.condensesBarOnSwiped - to enable safari like auto condensing bars
  • navController.hidesBarWhenVerticallyCompact - to enable auto hides bar for vertically mode
  • navController.condensesBarsWhenKeyboardAppears - to enable auto condensing bars when keyboard appears
  • navController.navugationBarCondensed - to manually toggle nav bar


Presentation Controllers

Presentation Controller enhance the exesting API for creating custom presentation, all presentation styles now have an associated Presentation Controller with them. Some new presentation styles are introduced as well, i.e;
  • UIModelPresentationOverFullscreen - shown like an overlay which means it don't remove the presenting view controller
  • UIModelPresentationOverCurrentContext - cover upon the presenting view controller only
  • UIModelPresentationPopover - it is more like a popover itself
Now most of the iPad-only styles are avilable in iPhone as well, some of them are in full screen mode (for iPhone) by default but configurable to be non-full screen.

Presentation Controllers can easily adapt to size class change. We can use Adaptive Presentations (a.k.a UIAdaptivePresentationControllerDelegate) to track the presentation orientation change. We can create custom presentation controllers which will adapt if shouldPresentInFullscreen returns YES.

Transition Coordinators (a.k.a UIViewControllerTransitionCoordinator) are part of the UIViewController adaptive UI story. During an active view controller transition, use Transition Coordinator to respond to user cancelation of the transition to run a custom animation or register a completion handler. UIContentContainer can help you to adapt the contents of your view controllers to size and trait changes. New methods are added to it to provide support for Trait, i.e;
  1. -(void)willTransictionToTraitCollection:withTransitionCoordinator: -
  2. -(void)viewWillTransitionToSize:withTransitionCoordinator: -


UIScreen is now interface oriented, which means;
  • -[UIScreen bounds] are interface-oriented
  • -[UIScreen applicationFrame] are interface-oriented
  • Status bar frame notification are interface-oriented
  • Keyboard frame notification are interface-oriented
To still work in fixed cordinate system we can use protocol UICordinateSpace

New in Interface Builder

  • We can use same Storyboard/XIB for any screen size of all iOS devices like iPhone4S, iPhone5, iPad..
  • We can deploy backwards to iOS 6 and iOS 7
  • We can preview devices, orientation and OS versions. Go to Jump Bar > Preview Assistance > Chose plus button from bottom to select device and desired orientation
  • We can to use Auto Layout to design flexible UI
  • We can remove or add unique Auto Layout constraints for each screen size
  • We can use Assets Catalog for images to use different versions of same image based on size
  • We can override subviews for specific size class



4 comments:

  1. Horizontal Regular - iPad landscape
    Horizontal Compact - iPhone portrait, iPhone landscape
    Vertical Regular - iPad portrait, iPhone portrait
    Vertical Compact - iPhone landscape

    ReplyDelete
  2. ABPeoplePickerNavigationController how we can use this ... in IOS 8

    ReplyDelete
    Replies
    1. iOS 8 introduced a new delegate for the same
      - (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier

      Delete