Swift: Difference between revisions

From Bitpost wiki
Line 10: Line 10:


== Lessons learned ==
== Lessons learned ==
=== View Hierarchy ===
Apple's Views are FUCKED.  You cannot use them like you think you can.  You MUST use this layout (or shed tears)...
MainTabView
  if ( loggedIn )
    TabView {
      NavigationView {
          MyComplexView
      }
     
      MySimpleView: like this NavigationView { VStack { ... } }
    }
=== Lists ===
=== Lists ===
* Lists should have NavigationView parents
* Lists should have NavigationView parents

Revision as of 20:53, 31 July 2022

Concepts

  • @Binding: allows one "source of truth" with many references called bindings
  • @StateObject: an object owned and mutated by its parent class
  • Codable: a designation that an object can be en/decoded
  • Identifiable: a designation that an object uses predictable identification (eg via an ID)
  • Custom modifiers allow you to put all your modifiers (eg .font(.headline)) into one reusable set.

Modifiers vs Custom View

Tip: Often folks wonder when it’s better to add a custom view modifier versus just adding a new method to View, and really it comes down to one main reason: custom view modifiers can have their own stored properties, whereas extensions to View cannot.

Lessons learned

View Hierarchy

Apple's Views are FUCKED. You cannot use them like you think you can. You MUST use this layout (or shed tears)...

MainTabView
  if ( loggedIn )
    TabView {

      NavigationView {
         MyComplexView
      }
      
      MySimpleView: like this NavigationView { VStack { ... } }

    }

Lists

  • Lists should have NavigationView parents
  • To avoid the FAT ASSED UGLY NavigationView header in the top navigation view (where you do not WANT it, doh), you have to set this on the EMBEDDED view (not NavigationView):
.navigationBarHidden(true0
.navigationBarTitle("") // don't skip this or it won't work, DOH
  • Always use listStyle plain or you will get awful default Swift 5 LIGHT/DARK backgrounds; it is the only way to set ANY other kind of background on the list
.listStyle(.plain)

SwiftUI vs UIKit

UIKit is OLD and BIG. You'll still need it to get to some Apple services until SwiftUI is fully fleshed out. For my JSON API driven apps, I WILL AVOID IT LIKE THE PLAGUE, and hold out for SwiftUI expansion.