Skip to content
Permalink
Browse files

Use generics for ExampleRow instead of AnyView

- Cleaner code and should only be using AnyView as a last resort.
  • Loading branch information...
dempseyatgithub committed Jun 12, 2019
1 parent c7a760a commit 8d160db7d271a374b6f69cd3fcffdf863ff09219
Showing with 11 additions and 15 deletions.
  1. +6 −11 MemeMaker/ExampleList.swift
  2. +5 −4 README.md
@@ -14,27 +14,22 @@ struct ExampleList: View {
ExampleRow(
title: "Overlay Modifier",
subtitle: "So far the cleanest example yet",
exampleView: AnyView(
OverlayMeme()
exampleView: OverlayMeme()
.navigationBarTitle(Text("Overlay Modifier"), displayMode: .inline)
)
)

ExampleRow(
title: "Nested ZStacks",
subtitle: "The original example",
exampleView: AnyView(
NestedZStacks()
exampleView: NestedZStacks()
.navigationBarTitle(Text("Nested ZStacks"), displayMode: .inline)
)
)

ExampleRow(
title: "Text Gone Wild",
subtitle: "An example of not getting the desired layout",
exampleView: AnyView(
TextGoneWild()
.navigationBarTitle(Text("Text Gone Wild"), displayMode: .inline)
exampleView: TextGoneWild()
.navigationBarTitle(Text("Text Gone Wild"), displayMode: .inline
)
)

@@ -45,10 +40,10 @@ struct ExampleList: View {
}


struct ExampleRow: View {
struct ExampleRow<T: View>: View {
var title: String
var subtitle: String
var exampleView: AnyView
var exampleView: T

var body: some View {
NavigationButton(destination: exampleView) {
@@ -3,9 +3,9 @@ MemeMaker

MemeMaker is a little app using SwiftUI that shows a few things:

- Main list uses AnyView to pass destination view into row
- Main list uses a generic type constrained to View to pass destination view into row

Now has three examples
Now has three examples:

Overlay
- Uses the overlay modifier to overlay the text on top of the image. Probably the cleanest solution.
@@ -39,12 +39,13 @@ The app composes some text over an image in a few different ways and then you ca

The image is too wide for the screen, so the ends are clipped.

There should be a more elegant way to pin text at the top and bottom of the image than using two ZStacks.
There should be a more elegant way to pin text at the top and bottom of the image than using two ZStacks.
UPDATE: There is! Check out the Overlay example in the app.

Clearly, I need to learn more about having views size themselves in SwiftUI.

I also need to brush up on my generics and protocols. I know I need the type-erased AnyView type to keep from hardcoding destination views in the row view, but I couldn't explain to you very clearly why.
I also need to brush up on my generics and protocols. I know I need the type-erased AnyView type to keep from hardcoding destination views in the row view, but I couldn't explain to you very clearly why.
UPDATE: In fact, I should really only be using AnyView as a last resort. Based on that feedback from Matt Ricketson ([@ricketson_](https://twitter.com/@ricketson_)), I've updated to use generics.

So, I'm putting this out there as a starting point if you want to check it out or play with it. Feel free to download or fork it and play with it on your own. If you discover better techniques for any of it, I'd love it if you'd let me know. I'm @jamesdempsey at [Micro.blog](https://micro.blog/jamesdempsey) and [Twitter](https://twitter.com/jamesdempsey).

0 comments on commit 8d160db

Please sign in to comment.
You can’t perform that action at this time.