SwiftUI: Apply the same modifier to every Stack element - swiftui

Is it possible to create a stack, for example a VStack, and then apply a view modifier to every view inside of this stack without applying it manually to every view element? Is there a way to create a custom view and then iterate over all elements? List or Section do the same... or is this SwiftUI internal magic?

Related

Is there a better way to find out which view blocks hittest in swiftui?

I have a few screens with lots of Stack view(Called View B) hierarchy, as well as overlaid by UIViewController (called View A).
after overriding the hittest() in UNViewController, and tapping the same view like UILable on View A, the tap gesture can just be passed through View B in part of screens.
is there a easy way to detect which one blocks hittest() instead of adding test code for each Stack view separately.
thanks.

How do I prevent SwiftUI List from scrolling when new items are added?

SwiftUI's default List behaviour for changing sizes of existing List items is different to inserting new ones (no scrolling vs scrolling visible items).
I want them to both not scroll. In my app, there are legitimately a large number of updates that happen almost immediately in the List View, and I don't want the user to lose their place, which happens if updates trigger the currently viewable list of items to move down.
This simple example shows the two behaviours.
How to use example:
Add some items
Scroll down.
Resize some items. It resizes items from the top item first.
Notice that the view you see is unaffected until the items on the screen are changed ("not scrolling"). It appears to be offsetting the position of the List such that the currently visible portion is unchanged.
Now add some items. Notice that inserting the items pushes the current items down ("scrolling"). In this case it does not seem to attempt to offset the position of the List, and so the items, with new content, are moved down.
I'd like to customise the List behaviour (iOS16+ is ok) so that the insertion behaviour is the same as the update behaviour.
Does anyone know the best way to do this?
Do I have to rebuild using UICollectionView (which is I believe how List is implemented under iOS16…?)

When exactly is onAppear called in SwiftUI?

Can anyone explain exactly when onAppear is called in SwiftUI. Docs say A view that triggers action when this view appears.
When the view with onAppear changes and is thus rendered again is probably clear. But what about:
When one of its child views changes?
When one of its siblings changes?
When its parent changes?
When navigating back in a NavigationView?
When changing back to a tab view that was previously on the screen, but has not otherwise changed?
Any time it appears on the screen?
When app returns from background?
...

UIViewRepresentable not sizing correctly in ScrollView

I followed Apple's guide on using UIPageViewController with SwiftUI. Their guide works fine. However I ran into an issue, where if I have my PageView inside a ScrollView - the height of the PageView is no longer respected and I have to set the frame manually. Not ideal, as the content inside PageView is dynamic and thus, heights will vary.
This seems to be an issue with ScrollView; as I have another view - UIViewRepresentable - that is a simple wrapper around WKWebView - with the same issue.
Is there a way to have these views size themselves, inside a SwiftUI ScrollView?
If I place these views outside of a ScrollView and into a simple VStack for example, they size themselves correctly.

How to adjust UIView height according to the Collection View Content Height in swift 3?

I have a problem of adjusting the UIView height in my project. I illustrated the project.
Let me explain about my project image.Each Orange UIView has trailing, leading , top and bottom constraints and vertical spacing to each UIView. But, I add "Height" to each UIView because if I don't add, it gives error and add the "Height" warning.
In UIView2, I add 3 Container Views. Each Container Views has 3 View Controllers. (Data 1 View, Data 2 View, Data 3 View)
I embed Collection View inside in each views.
The problem is that in Data 3 View has many collection View cell data. So, the height of Orange UIView should be increase according to the collection view content.
Currently, I just add fixed height constant (height:800) if users click on the Data 3 segmented control button. But, I don't want to set fixed height like this.
I've been trying to solve this problem since last week. I could not solve this till now.
Can anyone help me explain in step by step please? Because I have no idea of what should I do.
Since UITableView or UICollectionView inherit from UIScrollView they basically do not have an intrinsicContentSize.
You have two ways to make the same height as the content size (the space that you can scroll):
You can subclass the collection view and override intrinsicContentSize method to return the contentSize and the sizeThatFits: method as well
Create a height constraint in the interface and set it as equal to the content size of the collection view.