Thursday, June 13, 2024
HomeIOS DevelopmentUtilizing Xcode Previews in UIKit Growth

Utilizing Xcode Previews in UIKit Growth


When SwiftUI was first launched, one of many nice options that piqued my curiosity was the moment preview operate. This characteristic empowers builders to preview the person interface of any view inside Xcode, solely bypassing the necessity for a simulator.

Previous to Xcode 15, the preview characteristic was unique to the SwiftUI framework. Nevertheless, with the newest launch of Xcode, Apple expanded the utility of this characteristic to UIKit as properly.

On this tutorial, let’s see how one can make use of this preview characteristic when growing UIKit apps.

Utilizing #Preview to Preview View Controllers

To preview a UIKit view or view controller in Xcode, all that you must do is ready up a preview code block utilizing the #Preview macro. Right here is an instance:

For many who have expertise utilizing the #Preview characteristic in SwiftUI, the syntax ought to be fairly acquainted. When you enter the preview code, Xcode exhibits an extra pane, offering a preview of your view controller.

uikit-preview-xcode-view-controller

As you alter the code of ViewController, Xcode ought to show the change immediately. For instance, you’ll be able to attempt to modify the code like beneath:

The preview pane will present a button that claims “Whats up”. Like in SwiftUI growth, you’ll be able to test the person interface straight within the preview. For those who press the “Whats up” button, a warning or alert will pop up.

Xcode-uikit-preview-button-action

Previewing View Controllers in Interface Builder

The #Preview macro will also be used to preview view controllers designed in Interface Builder (or Storyboard). Assuming you’ve created a view controller, configured with a storyboard ID, you’ll be able to write the next code to preview it in Xcode:

You employ the instantiateViewController methodology to instantiate the view controller and preview it in Xcode. Optionally, you may give the preview a reputation (e.g. LoginView).

swiftui-uikit-preview-storyboard

Abstract

With the discharge of Xcode 15, Apple has expanded the moment preview characteristic, beforehand unique to SwiftUI, to UIKit as properly. Builders can now preview the person interface of any UIKit view or view controller inside Xcode utilizing the #Preview macro, eliminating the necessity for a simulator. This characteristic additionally extends to view controllers designed in Interface Builder or Storyboard. Going ahead, take advantage of this preview characteristic to expedite your UIKit growth course of.


Founding father of AppCoda. Creator of a number of iOS programming books together with Starting iOS Programming with Swift and Mastering SwiftUI. iOS App Developer and Blogger. Observe me at Fb, Twitter and Google+.



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments