Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between creating ViewModifier and View extension in SwiftUI

Tags:

ios

swift

swiftui

I'm trying to find out what is practical difference between these two approaches. For example:

struct PrimaryLabel: ViewModifier {     func body(content: Content) -> some View {         content             .padding()             .background(Color.black)             .foregroundColor(Color.white)             .font(.largeTitle)             .cornerRadius(10)     } }  extension View {     func makePrimaryLabel() -> some View {         self             .padding()             .background(Color.black)             .foregroundColor(Color.white)             .font(.largeTitle)             .cornerRadius(10)     } } 

Then we can use all of them following way:

Text(tech.title)     .modifier(PrimaryLabel()) Text(tech.title)     .makePrimaryLabel() ModifiedContent(     content: Text(tech.title),     modifier: PrimaryLabel() ) 
like image 556
Timur Bernikovich Avatar asked Aug 08 '19 11:08

Timur Bernikovich


People also ask

What is SwiftUI Viewmodifier?

A modifier that you apply to a view or another view modifier, producing a different version of the original value.

What is an extension SwiftUI?

Extensions add new functionality to an existing class, structure, enumeration, or protocol type. This includes the ability to extend types for which you don't have access to the original source code (known as retroactive modeling). Extensions are similar to categories in Objective-C.

What is a viewmodifier in SwiftUI?

SwiftUI has a place for everyone. It turns out, ViewModifier can do something that View extensions can’t. A ViewModifier can keep its own internal state, by declaring @State variables. Let’s work with an example We are going to create a modifier, that when applied to a view, will make it selectable.

How to avoid using viewmodifier in Android?

The only way to avoid using ViewModifier, is if we keep the tapped status outside the view extension. But that’s no fun. We would be splitting the logic half inside the view extension and half in the calling view.

What is viewmodifier in Salesforce?

What is ViewModifier? As you might be able to imply from the name. ViewModifier is a modifier that you apply to a view or another view modifier, producing a different version of the original value. It is a simple protocol with required only one method.

How does a viewmodifier keep its state?

A ViewModifier can keep its own internal state, by declaring @State variables. Let’s work with an example We are going to create a modifier, that when applied to a view, will make it selectable. When the modified view is tapped, a border will be drawn around it: If you think about it, this can only be accomplished with a ViewModifier.


2 Answers

All of the approaches you mentioned are correct. The difference is how you use it and where you access it. Which one is better? is an opinion base question and you should take a look at clean code strategies and SOLID principles and etc to find what is the best practice for each case.

Since SwiftUI is very modifier chain base, The second option is the closest to the original modifiers. Also you can take arguments like the originals:

extension Text {     enum Kind {         case primary         case secondary     }      func style(_ kind: Kind) -> some View {          switch kind {         case .primary:             return self                 .padding()                 .background(Color.black)                 .foregroundColor(Color.white)                 .font(.largeTitle)                 .cornerRadius(10)          case .secondary:             return self                 .padding()                 .background(Color.blue)                 .foregroundColor(Color.red)                 .font(.largeTitle)                 .cornerRadius(20)         }     } }  struct ContentView: View {     @State var kind = Text.Kind.primary      var body: some View {         VStack {         Text("Primary")             .style(kind)             Button(action: {                 self.kind = .secondary             }) {                 Text("Change me to secondary")             }         }     } } 

We should wait and see what is the BEST practices in new technologies like this. Anything we find now is just a GOOD practice.

like image 113
Mojtaba Hosseini Avatar answered Oct 11 '22 18:10

Mojtaba Hosseini


I usually prefer extensions, as they get you a more readable code and they are generally shorter to write. In fact I am currently working on an article with some tips. I finished an article about View extensions, available here.

However, there are differences. At least one. With ViewModifier you can have @State variables, but not with View extensions. Here's an example:

struct ContentView: View {     var body: some View {         VStack {             Text("Hello, how are you?").modifier(ColorChangeOnTap())         }     } }  struct ColorChangeOnTap: ViewModifier {     @State private var tapped: Bool = false      func body(content: Content) -> some View {         return content.foregroundColor(tapped ? .red : .blue).onTapGesture {             self.tapped.toggle()         }     } } 
like image 43
kontiki Avatar answered Oct 11 '22 16:10

kontiki