What Is Content in Swiftui

SwiftUI TabView covers content view

The contentView contains a List, and it fills all available space, moving buttons that are located below the List under the TabView. Adding Space after the contentView solves the issue.

var body: some View {
TabView {
VStack {
self.contentView
Spacer()
}
.tabItem {
Image(systemName: "chart.bar.fill")
Text("Chart")
}
Text("Screen 2")
.tabItem {
Image(systemName: "person.3.fill")
Text("Leaderboard")
}
Text("Screen 3")
.tabItem {
Image(systemName: "ellipsis")
Text("More")
}
}
}

Thank you @Asperi for pointing in right direction, and checking the children of the contentView.

Swift UI exporting content of canvas

This works fine!

The issue you are encountering is that in your sample code, canvasPallete didn't get a frame modifier when snapshot() was called on it. So it got rendered at a size that's not what you want, probably (0,0).

If you change the snapshot line it will work:

let image = canvasPallete
.frame(width: 300, height: 300)
.snapshot()

One more thing! In iOS 16, there's new API for rendering a view to an Image. So for iOS 16 and up, you can skip the View extension and write:

let renderer = ImageRenderer(content: canvasPallete)
if let image = renderer.uiImage {
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
}

Dynamic content hight in VStack SwiftUI

ScrollView

Solved was putt all view in ZStack and add some fake views in Scroll view.
1 view is responsible for the size where the scroll cannot fall through.
2 view is responsible for the size between the maximum and minimum size of the header
Here's a examples

import Introspect

struct ContentView: View {

@StateObject private var myCoord = MyCoord()

var body: some View {
let sizeOfPlaceholder = myCoord.maxSize - myCoord.minSize

ZStack(alignment: .top) {
VStack(spacing: 0) {
Color.black
.frame(height: myCoord.minSize)

ScrollView {
Color.brown
.frame(height:sizeOfPlaceholder)

ForEach(0..<20) { number in
Text("\(number)")
.background(Color.red)
.frame(height: 20)
}
}
.padding(.vertical, 1)
.introspectScrollView { scroll in
scroll.delegate = myCoord
}
}

Color.green
.frame(height: myCoord.height)
}

}
}

class MyCoord: NSObject, UIScrollViewDelegate, ObservableObject {

let maxSize: CGFloat = 76
let minSize: CGFloat = 56

@Published var height: CGFloat = 76

func scrollViewDidScroll(_ scrollView: UIScrollView) {
print(scrollView.contentOffset.y)
let size = maxSize - scrollView.contentOffset.y
height = min(maxSize, max(minSize, size))
}
}

dynamic content not updating in list Swift UI

struct ListItem: View {
var item: VocabItem
...
}

struct VocabList {
.
.
.

ForEach(items) { item in
ListItem(item: item)
}
}


Related Topics



Leave a reply



Submit