What’s new in Cocoa Touch (WWDC 2017 Session 201) TL;DR
Here’s a quick recap of most of the “What’s new in Cocoa Touch” session of WWDC 2017 (Session 201). This is all typed live so if something isn’t accurate feel free to correct me :-)
Productivity
Drag and Drop
Enabling Drag is done by creating a UIDragInteraction (subclass of UIInteraction) and attach it to the view you want to have Draggable (and assign its delegate).
The delegate provides data for dragged item, allows customizing the “lift” animation and customize the draggin preview.
Enabling Drop is done by creating a UIDropInteraction and setting its delegate in a similar way. The delegate allows Updating the UI as the drag moves, receive general data on the drop and customize the drop animation.
Many UIKit elements are supported out of the box — TableView, CollectionView, TextView, TextField and WebView.
File Management
Introduced new File Explorer that allows presentation from within our apps.
a UIDoumentBrowserViewController is the ViewController we can present in our apps. It’s highly customizable and Allows accessing device content, iCloud content and external cloud service.
It’s highly crucial to coordinate file access since we can’t count on our app being the only one to touch certain files/assets. Can be done by using NSFileCoordinator/UIDocument.
UI Refinements
Large Title Navigation Bar can have a built in Search Bar, but as you scroll content up, the Search Bar will collapse following with the collapsing of the Large title into a regular small title as we’re used to from iOS 10.
In most cases this can be done almost automatically as the system can handle it autoamtically by setting UINavigationBar.prefersLargeTitle
to true
.
You could also use UINavigationItem.largeTitleDisplayMode
to control separate the large title display mode in a more granular way around levels of navigation.
Enabling the unified search bar is done by setting UINavigationBar.searchBarController
.
Usually the top controller in a Navigation Controller/stack would be the only one to use the “Large” style titles, while the inner ones should use the standard small variation.
There is a built in Pull-to-refresh in the Navigation Bar which looks great.
We can’t keep counting on a static height for a Navigation Bar since it might change height for different content types. In most cases this would be handled automatically by the various UINavigation classes. There is a new safeAreaInsets.top
property on UIView that we can accurately and reliably use to know the size of the "obscured" area by the navigation bar. There is also a bbottom values. (safeAreaInsets.bottom
)
There is also a safeAreaLayoutGuide
that you could use as well.
For manual layouts you can manually read safeAreaInsets
and subscribe to changes on the size of the Safe Area.
UINavigationController
now longer controls the insets values as it is just using the safe insets, so setting manual insets on a Scroll View should no longer have the kinks we are used to.
UIScrollView
has new properties called contentInsetAdjustmentBehavior
and adjustedContentSize
we can use to manually define how the insets behave on UIScrollView.
UITableViewCell
s now support built in swipe actions on both left and right side using the new UIContextualAction
class. It seems similar to how UITableRowAction
works but is a bit more refined since you can separately use UISwipeActionsConfiguration
to group these UIContextualAction
s.
There is a new proprety on UITableView
called separatorInsetReference
that lets you set whether the values of the insets are absolute or "Deltas" e.g. in reference ot the original inset reference the insets would have been at previously.
Swift 4 and Foundation
Archiving Swift native types
New Codable
protocol allows to more easily "Archive" and "Unarchive" and allows them to participate in NSCoding by default.
Key paths
New KeyPath type has an option for a literal syntax (e.g. \Object.path.subPath
) so you could dynamically create a keypath and then access using collection[keyPath: someKeyPath]
.
New block-based KVO instead (!!) using object.obserer(keyPath) { object, change in ... }
Touches related to Status Bar and Control Center
in iOS 11 we can add a new preferredScreenEdgesDeferringSystemGestures()
method where you can return a UIRectEdge
to set explicitly which edges you would want to protect from system override of touch events.
Autolayout and ScrollView
You can be explicit with contentLayoutGuide
and frameLayoutGuide
where you could set a constraint on the frame of the scroll view but also on the content of the scroll view itself.
imageView.centerXAnchor.constraints(equalTo: scrollView.contentLayoutGuide.centerXAnchor)
imageView.centerYAnchor.constraints(equalTo: scrollView.contentLayoutGuide.centerYAnchor)
Dynamic Type
Deciding on a font appropriate for your user’s dynamic type size.
Oldschool way was asking UIFont for a preferredFont for a specific text style.
Introduced a new UIFontMetrics
that you can instantiate for a specific text style, which can handle getting a scaled font based on the standard font which allows using custom fonts for Dynamic Type.
It also allows scaling some arbitrary layout values, meaning it can return a scaled value based on a standardValue you provide, so it actually sort of calculates the correct height size for containers of text participating in Dynamic Type.
It’s now possible to dynamically set the baseline of different labels to eachother based on their Dynamic Type.
lettopAnchor = topLabel.lastBaselineAnchorlet bottomAnchor = bottomLabel.firstBaselineAnchorbottomAnchor.constraintEqualToSystemSpacing(Below: topAnchor)
Password Autofill
iOS 11 introduces a new feature showing a “Key” icon in the autofill bar if it detects a “Username & Password” situation, so you could choose an AutoFill password for a specific app during login from your iCloud keychain.
Apps can also set an entitlement and another configuration (which are actually also used for Universal Links), which will display an action for your specific app in the autocompletion bar.
Named colors in Asset Catalog
We can now have named colors in our Asset Catalogs that we can use in Interface Builder. This also works for Wide Gamut colors. There is now also App thinning for icons based on the assets your app would need.
Vectors
We can use PDF-backed images so we could preserve Vector Data.
It’s very useful for UITabBars, since it would create accessibility related assets by default without any extra code.