Swift UI
Swift UI
SWIFT UI
Swift UI ditches the concept of UIKIT such as Auto Layout for
an easier to use declarative programming model. It allows
fast and easy creation of work across Apple platforms.
IMPERATIVE VS DECLARATIVE
Procedural programming paradigm Logic Programming
- Industry Adoption
ARE YOU STILL WONDERING WHETHER YOU SHOULD LEARN SWIFTUI? FEW REASONS TO
C O N V I N C E Y O U 👆🏻
LAYING OUT COMPONENTS
HOW IT WORKS
LAYOUT
A new SwaiftUI starts with a single Text view located at the
center of the screen.The body view only returns a single view.
To add other views vertically, we replace the initial Text view
with a VStack, and then add some content.
STEPS;
1. Figure out its internal spacing & subtract that from the size
poposed by its parent view.
2. Divide the remaining space into equal parts.
3. Process the size of its least flexible view.
4. Divide the remaining unclaimed space by the unallocated
space and repeat step 2
5. The stack then aligns its content & chooses its own size to
exactly enclose its children.
SPACER & DIVIDER
Adding.a spacer() forces the view to use The Divider() component is used to draw a
the maximum amount of vertical space. horizontal line across the width of its parent
view.
The Hstack is like the VStack but its conents are displayed horizontally from left to right. Adding a Spacer() in an
HStack thus causes it to fill all available horizontal space, and a divider draws a vertical lne between components
in the HStack.
The ZStack is like HStack and VStack but overlays its content on top of existing items.
You can also use the .frame modifier to adjust the width and height of the component. Try remove Spacer() and
Divider() from Hstack and apply
DEALING WITH TEXTS
HOW IT WORKS..
Text views have several modifiers for font, spacing,
01
& other formatting requirements. When in dount
type . after the text view & choose from a list of
possible options that will appear.
Unlike regular Text views, textfield & secureField require variables
02
to store the value entered by the user. SwiftUI manages the
storage of properties declared by using @State & refreshes the
body each time the value of the state variable changes
Ther's more..Try add an 11th element to the VStack. It wont allow
03
as maximum capacity of views is 10.You will have to enclose some
elements in a Group view. Lets see example of texts: 02-Dealing-
With-Text
ADDING SWIFTUI TO AN
EXISTING APP
The Main.storyboard file is to UIKit what ContentView.swift file is to
SwiftUI. They are both default home views that are created when
you start the project.
Lets look at a sample with simple UIKit project that contains a button
Getting Ready
CHANGING EXISTING APP..
HOW IT WORKS To host SwiftUI views in an existing app, we
need to wrap the SwiftUI hierarchy in a
ViewController or InterfaceController.
Yes
No
QUIZ TIME
QUESTION 2
Yes
No
QUESTION 3
Is UIKit dead?
Yes
No
QUESTION 4
Yes
No
QUESTION 5
Yes
No
QUESTION 7
UIHostingController
NavigationController
NavigationLink
Segue
SWIFTUI TIPS AND TRICKS
SWIFTUI VIEWS
100
75
50
UIView testing
While snapshot testing is more common in other
technologies—for example, JavaScript and the Jest
25 testing library.
—its usage in the iOS community is still a niche practice.
0
2018 2019 2020 2021 2022
JUST TWO
STEPS..
A snapshot test consists of two steps:
Getting ready
Step 1
2022 PRESENTATION
Add the swift-snapshot-testing package
Step 2
2022 PRESENTATION
How to do it…
Step 3
5. The first time we run the test, it fails since it
Here are the steps:
doesn't find the reference image. While failing,
it also takes a screenshot of the current state
1. Start by defining a list of cards
of the screen:
2. Then, for each card, create a rounded
rectangle of the color defined in the list.
2022 PRESENTATION
SUCCESSFULL
HOW IT WORKS..
Explanation..
Add Custom
2. You will then be prompted with a dialog box. Be sure to check your project
name under “Add to targets.” This will ensure that your fonts are compiled as
assets at build time.
CUSTOM FONTS..
CUSTOM
3. Add Your Fonts to the Info.plist
Your font files have now successfully been added to your
project. Now you have to tell your project to look for them.
Navigate to the Info.plist...
5. Next, click the “+” icon next to the new array you just created.
Create an item for every font file you wish to support and enter
the name in the string field exactly how it appears as the font
file name. The string should also include the extension of the
font file.