Use LazyVStack in SwiftUI 1 - swiftui

One of the new features of SwiftUI 2 are LazyVStacks. Is it possible to implement its functionality in the current SwiftUI framework? I have the following code sample where I want to use it:
var body : some View {
VStack(alignment: .leading){
ScrollView {
Text("sample")
VStack{ // I want to have a LazyVStack here
ForEach(1..<10000, id: \.self) {_ in
Text("test")
}
}
}
}
}
Normally i would use a List which is by default lazy. But due to other constraints it's not possible.
Thanks in advance.

You have to install Xcode 12 beta in order to use LazyStacks. If you are coding for an iOS app, the simulator will run correctly, but if you are coding for a macOS app you will have to update to Big Sur also in order to run the SwiftUI 2 code.

Related

Is SwiftUI build on top of UIKit?

i can't explain why extension on UIKit's UIPickerView affects on SwiftUI's picker.
and does that mean that knowing basics of uikit is very useful for learning swiftui?
does it even make sense to learn swiftUI if it's basically UIkit library(if I am right)
extension UIPickerView {
open override var intrinsicContentSize: CGSize {
return CGSize(width: UIView.noIntrinsicMetric, height: 200)
}
}
GeometryReader { geometry in
HStack(spacing: 0) {
Picker(selection: self.$daySelection, label: Text("")) {
ForEach(0 ..< self.days.count) { index in
Text("\(self.days[index]) d").tag(index)
}
}
.pickerStyle(.wheel)
Picker(selection: self.$daySelection, label: Text("")) {
ForEach(0 ..< self.days.count) { index in
Text("\(self.days[index]) d").tag(index)
}
}
.pickerStyle(.wheel)
Picker(selection: self.$daySelection, label: Text("")) {
ForEach(0 ..< self.days.count) { index in
Text("\(self.days[index]) d").tag(index)
}
}
.pickerStyle(.wheel)
This extension solves problem of overlapping gesture areas of pickers in ios 15.
Is SwiftUI built on top of UIKit?
No... and yes, sometimes.
There are controls in SwiftUI that do reach into UIKit to use those. Although, not all of them.
And as Vacawama rightly pointed out… if running on MacOS then AppKit also.
The thing about SwiftUI is to not worry too much about the underlying technology. It may be that Apple chooses to significantly rewrite the foundation of it. Perhaps taking a previously used UIKit component and writing it natively in SwiftUI. But the SwiftUI code shouldn’t change.
Is SwiftUI a UIKit library?
No, absolutely not. They are fundamentally different ways of thinking about and creating UI.
Does it make sense to learn SwiftUI?
Yes, it makes sense to learn SwiftUI AND UIKit. Also, Combine and CoreGraphics and GameKit... Do you want to be an iOS developer or a SwiftUI developer?
Random bit of badly indented code...
I'm not sure what this is doing in your question.

SwiftUI PageView iOS 13 - Navigation link not working as expected

My project is target for iOS 13 onwards hence could not use PageTabViewStyle(). I tried with Mr. John suggestion from this link
SwiftUI create image slider with dots as indicators
Now I need to open the detail view on clicking the image in the slideshow. But now when I try to move the images the detail view is opening and could not move the images as in Pageview. I implemented the below code. Please let me know the solution to fix this. Thanks in advance.
PagingView(index: $index.animation(), maxIndex: images.count - 1){
ForEach(articles, id: \.self) { article in
NavigationLink(destination: ArticleDetailUIView(article: articles[self.index], isBookmark: false) , isActive: $areYouGoingToArticleView)
{
Image(article.image)
.resizable()
.scaledToFill()
.accessibility(identifier: "articleImage")
}
}
}
.aspectRatio(4/3, contentMode: .fit)
.clipShape(RoundedRectangle(cornerRadius: 15))
After few browsing, the workaround was using the stepper to move around the slide show instead of scrolling the images. I added the below lines.
Stepper("Index: (index)", value: $index.animation(.easeInOut), in: 0...images.count-1)
.font(Font.body.monospacedDigit())
.labelsHidden()

Rounded corners in swiftui list sections

Here is a screen shot of iOS 13 Health app - User profile. I recently started with swiftui and wondering how to develop a screen like below. I tried list styles plain and grouped. But I couldn't get the look of below layout.
Can UI like this develop purely using swiftui-list?
I am specially looking for rounded sections and including a image inside the list.
As of iOS 14, you can use the code below. It works just perfectly, just like in UIKit.
List {
Section {
Text("Item 1")
Text("Item 2")
Text("Item 3")
}
Section {
Text("Item 4")
Text("Item 5")
Text("Item 6")
}
}.listStyle(InsetGroupedListStyle()) // this has been renamed in iOS 14.*, as mentioned by #Elijah Yap
.environment(\.horizontalSizeClass, .regular)
Thank you.
As of iOS 14.0, this list-style is .listStyle(InsetGroupedListStyle()) or .listStyle(.insetGrouped)
I run into the same problem and it looks like you can not achive this with list or section.
When you put cornerRadius modifier onto a section or list it is just applied to whatever the internals are making cells rounded while section itself being unchanged.
My solution was to avoid lists and sections entirely and build custom ones.
Something like
SectionView: View {
body: some View = {
VStack {
ForEach {
CellView()
}
}
.background(Color.white)
.cornerRadius(10)
.padding()
}
}
And put those sections over a Color view in some ZStack to have a gray background.
Take in account that the huge downside is that a ForEach will render all the cells right away, like if you have a thousand of cells then onAppear() will be called for each cell and strangely enough they will be called in backward order.
Hope it helps.
According to my findings and as of Xcode 11 GM seed 2 (11A420a), making this UI is not possible just from swiftui list.
There is a new list style available for storyboards named, insetGrouped. It has the exact same look.
https://developer.apple.com/documentation/uikit/uitableview/style

SwiftUI achieve Master and Detail structure in tvOS

The way to achieve Master and Detail structure, as of beta 4, it should be by using .navigationViewStyle(.doubleColumn). Works perfectly on iOS / iPadOS / macOS but not in tvOS... it's a bug or I'm missing something?
import SwiftUI
struct ContentView: View {
var arra = ["Margherita","Marinara","Calzone"]
var body: some View {
NavigationView {
List(arra, id: \.self) { pizza in
NavigationLink(destination: SecondView(pizza: pizza)) {
Text(pizza)
}
}.navigationBarTitle("MASTER")
SecondView(pizza: arra[0])
}
.navigationViewStyle(.doubleColumn)
}
}
struct SecondView: View {
var pizza : String
var body: some View {
Text(pizza)
.navigationBarTitle("DETAIL")
}
}
(as of GM release) The official doc tells something like: double column style will be stacked on tvOS, like it's an iPhone in portrait mode. So it's impossible to automatically achieve the "master and detail" look like it's an iPad in landscape, you have to build it for yourself. Even if you use one UISplitViewController in UIKit! With this behavior I think it's good to go only if the master it's a fullscreen CollectionView, sorta like Netflix App.

SwiftUI PresentationButton Stops Functioning After Single Use on watchOS

The PresentationButton presents the view properly, but upon hitting "Cancel" to return to parent view, the button refuses to present the child view again.
Here is my PresentationButton code:
struct ContentView : View {
var body: some View {
ZStack {
PresentationButton(Text("Click to show"), destination: SomeOtherView())
.transition(.slide)
}
}
}
This bug has existed in various forms until Xcode 11 beta 4, but note that the API has changed.
PresentationButton became PresentationLink and then was deprecated in Xcode 11 beta 4 in favor of .sheet.
See here for an example of how to use the new functionality: https://stackoverflow.com/a/57087399/3179416