Day-18-geometryReader
- Apr 19, 2021
- iOS Foundations Module 4 Lesson 4
GeometryReader
Geometry reader is a container that acts similar to a ZStack but can provide position information.
GeometryReader { geo in
Rectangle()
.frame(width: geo.size.width)
geo.frame(in: .local).minX // get the location in local or global positioning
}
Shapes
- Circle()
- Rectangle()
Gestures
Modifier that can be applied to an object like the Circle or Rectangle or container.
.onTapGesture {
// code to execute onTap gesture
}
Positioning
.padding
.offSet
Day-19-environmentObject
- Apr 23, 2021
- iOS Foundations Module 4 Lesson 5,6
The environmentObject modifier can be attached to a view object to supply data to all downstream views
So at the top level view code may look like this with RecipeModel() containing the data we want to use in the downstream views.
TabView {
}
.environmentObject(RecipeModel())
Each downstream view will need to have a line of code that defines a variable that the environmentObject data can be placed.
@EnvironmentObject var model:RecipeModel
Something to keep in mind: I read that environmentObjects are kept in memory for the lifetime of the app, which means, we want to be careful the type can quantity of data we store in an environmentObject.
This website had a really great explanation between @State
, @Binding
@EnvironmentObject
and when to use them.
Day-20-challenge
- Apr 24, 2021
- iOS Foundations Module 4 Lesson 6 Challenge
Spoiler Warning lots of hints on how I solved this challenge below.
Create an app with 2 tabs that displays person information.
When creating a class, either the class has to have a default init function
init() {
//code here that will create default values for all variables in the class
}
or
All the elements in the class should be optional except say the Id.
class Person: Identifiable {
var id = UUID()
var name:String?
var address:String?
var company:String?
var experience:Int?
}
What we want here though is actually a struct not a class as it makes it easier to create a people Array of Persons.
Error: invalid mode 'kCFRunLoopCommonModes'
StackOverflow suggests this is just a warning that Apple is working on and can be safely ignored.
toggles object
We can only pass one object to the environmentObject modifier that we use on the TabView. So we need to create an object that can hold the for bool variables.
class toggles: ObservableObject {
@Published var showName = true
@Published var showAddress = true
@Published var showCompany = false
@Published var showExperience = true
}
Those variables need to be @Published
or else the toggles won’t update the class variables and the class needs to be an ObservableObject
.
I really liked how this challenged forced me to combine all the elements we have learned so far.
- TabView
- View Instances for FirstTab and Second Tab
- environmentObject, @EnvironmentObject
- ObservableObject, @Published, @State
- Structs, arrays,
- Lists,
- If statements
- Toggle
- MVVM model