Questions tagged [viewbuilder]

54 questions
19
votes
3 answers

Is there any way to create/extract an array of Views using @ViewBuilder in SwiftUI

I'm trying to create a simple struct that accepts an array of Views and returns an ordinary VStack containing those Views except they all are stacked diagonally. Code: struct LeaningTower: View { var views: [Content] var body:…
rayaantaneja
  • 1,182
  • 7
  • 18
17
votes
3 answers

How to loop over viewbuilder content subviews in SwiftUI

So I’m trying to create a view that takes viewBuilder content, loops over the views of the content and add dividers between each view and the other struct BoxWithDividerView: View { let content: () -> Content init(@ViewBuilder…
Eddy
  • 274
  • 3
  • 17
8
votes
4 answers

How to execute non-view code inside a SwiftUI view

I have been struggling with this over and over again, so I think I'm missing something. I need to do math, make a setting, assign a value or any of a host of simple operations in reaction to some user action, such as the example shown here, and…
Russ
  • 467
  • 3
  • 10
5
votes
3 answers

SwiftUI View: two different initializers: cannot convert value of type 'Text' to closure result type 'Content'

The code: import SwiftUI public struct Snackbar: View where Content: View { private var content: Content // Works OK public init(@ViewBuilder content: () -> Content) { self.content = content() } init(_ text:…
Richard Topchii
  • 7,075
  • 8
  • 48
  • 115
5
votes
1 answer

Closure containing control flow statement cannot be used with result builder 'CommandsBuilder'

I've made a button style to create circular neumorphic buttons in my app as follows: struct NeumorphicCircleButtonStyle: ButtonStyle { var startPoint: UnitPoint var endPoint: UnitPoint var padding: CGFloat? var bgColor: Color? …
theZ3r0CooL
  • 147
  • 2
  • 10
5
votes
3 answers

Individually modifying child views passed to a container using @ViewBuilder in SwiftUI

In SwiftUI you are able to pass multiple views as one parameter to another view, thanks to @ViewBuilder. Each child view is treated individually, and so after being passed, can be contained in a VStack, HStack etc. My question is: is it possible for…
5
votes
1 answer

SwiftUI, Passing a View as params in @Viewbuilder

My curiosity takes me to pass a View type as parameter to @ViewBuilder. Passing a Model/Primitive type as param in @ViewBuilder is perfectly valid. As shown below code. struct TestView: View { let content: (String) ->…
Nasir
  • 1,617
  • 2
  • 19
  • 34
3
votes
0 answers

How to make SwiftUI ViewBuilder accept only Views of certain protocol as input blocks?

I am trying to use a SwiftUI @ViewBuilder to allow users of my package to dynamically specify a view's body content. However, I am trying to restrict the possible views that can be used as an input to just a few default views I implemented (for…
linus_hologram
  • 1,595
  • 13
  • 38
3
votes
1 answer

How to use SwiftUI @ViewBuilder to create views which require parameters?

In the answers to my question on How to add a generic SwiftUI view to another view, I learned that this can be done using @ViewBuilder. While this works fine for most of my use cases, I now came across a new problem: The @ViewBuilder solution…
Andrei Herford
  • 17,570
  • 19
  • 91
  • 225
3
votes
1 answer

SwiftUI View Builder parameter cannot change state

I am trying to create a SwiftUI view where I can pass a view builder. But by using a view builder parameter I cannot change the state. Is this due to the creation in the init method? How can I achieve my expected behaviour? struct Card_Previews:…
Tbi
  • 154
  • 10
3
votes
1 answer

SwiftUI custom View's ViewBuilder doesn't re-render/update on subclassed ObservedObject update

This one I've been researching for a few days, scouring the Swift & SwiftUI docs, SO, forums, etc. and can't seem to find an answer. Here is the problem; I have a SwiftUI custom View that does some state determination on a custom API request class…
Othyn
  • 829
  • 1
  • 9
  • 21
2
votes
1 answer

How is Conditional Content created in SwiftUI?

I was trying to understand the inner workings of _ConditionalContent but I wasn't able to implement a resultBuilder for it. This is how some of the resultBuilders for _ConditionalContent are defined: static func buildEither(first:…
2
votes
2 answers

Animate ViewBuilder content size change in SwiftUI

I am wondering how I can animate the content size of a ViewBuilder view. I have this: struct CardView: View where Content: View { private let content: Content init(@ViewBuilder content: () -> Content) { …
BlackWolf
  • 5,239
  • 5
  • 33
  • 60
2
votes
2 answers

How to pass in content for TabView through a function

I am trying to build a reusable onboarding pager overlay. So I want to pass in different "slides" into the pager TabView based on a variable IntroType. But since TabView takes content plainly without any container, what would be the return type for…
Big_Chair
  • 2,781
  • 3
  • 31
  • 58
2
votes
1 answer

SwiftUI & iOS 13.3 TupleView initializer stop working. what happens to it?

I have such inits with ViewBuilders with TupleView // MARK: TupleView support public init(@ViewBuilder content: () -> TupleView<(A, B)>) { let views = content().value self.childs = [AnyView(views.0),…
Michał Ziobro
  • 10,759
  • 11
  • 88
  • 143
1
2 3 4