Questions tagged [swiftui-view]

For questions about View - a component in Apple's SwiftUI framework that represents part of your app’s user interface and provides modifiers that you use to configure views.. When using this tag also include the more generic [swiftui] tag where possible.

64 questions
98
votes
4 answers

SwiftUI View - viewDidLoad()?

Trying to load an image after the view loads, the model object driving the view (see MovieDetail below) has a urlString. Because a SwiftUI View element has no life cycle methods (and there's not a view controller driving things) what is the best…
Kraig Wastlund
  • 1,094
  • 1
  • 8
  • 9
10
votes
0 answers

Why does ForEach initialize its Child View twice as much in SwiftUI?

I came across a weird behaviour of SwiftUI's ForEach view. I've noticed that ForEach always initialize its child view twice as much as it should according to its repetition. Normally, when you render the view, its init is called once. But if you put…
Hollycene
  • 287
  • 1
  • 2
  • 12
8
votes
2 answers

Swiftui How can you use on Tap Gesture for entire row in a foreach loop

I am creating a SearchBar right now that gets data from API, the Text highlighted in red ( see image below) is what I want to pass into my userDefault string on tap . This is working as long as I tap the highlighted red text, is it possible to get…
user1591668
  • 2,591
  • 5
  • 41
  • 84
5
votes
1 answer

SwiftUI: Add List in to a scrollview with icon above

I'm trying to add a list of items into a view, while having an icon above. Currently when I build my code it, it separates the icon and the list, which is what I'm looking for but it makes the icon static and the list scrollable. I want both icon…
D J
  • 73
  • 6
4
votes
1 answer

How can you make Text in SwiftUI appear sideways while also rotating the frame?

The goal is to have something that looks like this: I am aware of .rotationEffect(), and the solution provided here. However the problem with this solution is that it does not rotate the frame, it only rotates the text itself. The image below is…
NSSpeedForce
  • 127
  • 9
4
votes
1 answer

SwiftUI: How to show some toolbar buttons only for iPhone

I have a set of toolbar buttons that should be presented only if the device is iPhone (not iPad). The following code fails with this error: Closure containing control flow statement cannot be used with result builder 'ToolbarContentBuilder' I do…
Satoshi Nakajima
  • 1,863
  • 18
  • 29
4
votes
0 answers

SwiftUI - View with a VStack initialized once but the body is called twice

Why, in SwiftUI, the body gets called twice when we use a VStack? Example without a VStack: struct ContentView: View { var body: some View { Text("Hello World") } } By placing a breaking point at Text("Hello World"), I can see the function…
alpennec
  • 1,864
  • 3
  • 18
  • 25
3
votes
1 answer

How do I get a SwiftUI view to appear in front of other views while it's being dragged?

I'm updating this question with new and more complete code, to show how I've attempted to implement the suggestion in the answer below from @HunterLion. Here's the original statement of the problem: I am implementing a version of Pentominos using…
jayboh
  • 267
  • 3
  • 12
3
votes
1 answer

SwiftUI Circle View animation glitch

import SwiftUI struct CircularProgressView: View { @Binding var progress: Float private let strokeStyle = StrokeStyle(lineWidth: 30.0, lineCap: .round, lineJoin: .round) private let rotation = Angle(degrees: 270.0) …
GuilhE
  • 11,591
  • 16
  • 75
  • 116
3
votes
1 answer

How to line up bottom text for child view in VStack

I have the following code which produces the following output. var body: some View { VStack(spacing: 0) { HStack(spacing: 0) { Rectangle() .fill(Color.green) .overlay( …
Petesta
  • 1,623
  • 3
  • 19
  • 29
3
votes
2 answers

Padding, offset, or position in SwiftUI

I’m trying to position a circle to have its center sitting on top of a rectangle like this. The diameter of the circle doesn't have to be the same width as the rectangle. What I’m trying to understand is what is the best and most practical way to…
Petesta
  • 1,623
  • 3
  • 19
  • 29
2
votes
3 answers

SwiftUI: Detect NavigationView goes back to main view

I have a NavigationView and the following view structure ViewA -> ViewB -> ViewC and I'm trying to figure out how to detect when ViewB goes back to ViewAViewB -> ViewA. Here is my Code: struct ViewA: View { var body: some View { …
user2924482
  • 8,380
  • 23
  • 89
  • 173
2
votes
1 answer

How can I return a struct which indirectly inherits the View protocol via another protocol in SwiftUI?

Suppose I have the following protocol which conforms to View: protocol Foo: View { init(field: Binding) } I then have two structs which conform to this protocol: struct BarView: Foo { @Binding private var field: Bool …
2
votes
2 answers

SwiftUI : How to get a Navigation Title aligned with Back Button

I'm trying to get the navigation title vertically aligned with the back button in a NavigationDetail view in SwiftUI. Here's what this looks like currently: Below's my code for adding the Navigation Bar title to the view. How do I get it vertically…
narner
  • 2,908
  • 3
  • 26
  • 63
2
votes
3 answers

How to prevent recreating a conditional view in SwiftUI?

I have a parent view that conditionally displays a subview: struct HomeView: View { @State var selectedView = true var body: some View { VStack { Picker("Selected", selection: $selectedView) { …
cjweeks
  • 267
  • 1
  • 4
  • 8
1
2 3 4 5