Mirek
Mirek

Reputation: 500

Adding modifier to View's body

Given I have a simple View like:

public struct MyView: View {
  public var body: some View {
     Text("My text 1")
     Text("My text 1")
  }
}

Is there an elegant way of setting a modifier on the View's body (such as setting AccessibilityIdentifier) straight in this struct without having to do this in the class that embeds MyView or creating another View layer?

Upvotes: 0

Views: 38

Answers (1)

vadian
vadian

Reputation: 285079

A possible way is to wrap the content of the body in a Group

public struct MyView: View {
  public var body: some View {
      Group {
          Text("My text 1")
          Text("My text 1")
      }
      .font(.title)
  }
}

Upvotes: 1

Related Questions