|
I'm using the new PageTabViewStyle for a TabView composed of views created from a ForEach array. The array is created with @State. When the view loads, it shows the views as expected. What I would like to do is append a new page to the TabView by appending an element to the @State array. When I try it, an index indicator is indeed added to the TabView, but I can't swipe to the new page. Tapping the index indicator throws an error of "Invalid IndexPath". The ultimate goal is to use this for a calendar to swipe between months, and on swipe, append the next month to the end of the TabView. Has anyone else been able to do something like this? Or is there just a better way to do this that I don't know?
|
|
Ok, so this captured my curiosity. I thought it would be a good idea to try a plain Jane tabView and see how that works with a dynamic data source. The following code was inspired by @twostraws with his article of a similar vein.How to create views in a loop using ForEach Essentially, the tabView behaves as expected and propagates tabs across the bottom of the view corresponding to the number of taps. Additionally, corresponding content text and tab labels are applied. When the .tabViewStyle(PageTabViewStyle()) is uncommented, haywire is a word that comes to mind - the content text disappears, but the "indexDisplay" increments happily as if it anxious to show its invisible content. In conclusion, this would appear to be a bug. But, perhaps there is some cryptic but well documented code out there that makes it all work like a dream...
|
|
Thanks for taking a look. And yes, haywire is a good word for it. You can tell by the index that it's trying to do it, and is partly successful, but then falls down somewhere along the line. I'm definitely hoping it's a bug and that it will end up being possible. It seems like a reasonable thing for SwiftUI to be able to do. |
|
No problem. I took the liberty to file a bug report and will update you if a reply is recieved. |
|
Just wanted to add my vote that any sort of dynamic behavior with PageTabViewStyle just doesn't seem to be working. The best way I can describe it is that the backing view doesn't seem to get updated properly with the number of subviews, causing all sorts of weird behavior. It's hard to point to one specific thing, because it's just all kind of broken. I also filed feedback. Crossing fingers for a fix in the next beta. |
|
It looks like adding an See this StackOverflow answer: https://stackoverflow.com/a/63500070/1751778 I think this behaviour might actually be intentional to prevent the You can probably use the |
|
@avario Adding the id totally worked. I added a UUID @State variable and had it recalculate using UUID() when the select page changes. It allows swiping, it adds and removes the views I want, and everything loads correctly. I should be able to use this as my infinite scroll for a calendar. Brilliant, thanks. |
|
I have a similar problem but unfortunately I did not succeed with your solution. In next simplified version, my TabView displays a single Tab to ask users to log in, and provides extra tabs when authenticated. Users can log as green user, in which case you get a set of green tabs, or as a blue user, who gets a set of blue tabs. The last tab allows each user to logoff and return to the login page. Same as @Will-1-Am, it works when .tabViewStyle(PageTabViewStyle()) is commented. However, when I uncomment the tabViewStyle modifier, the TabView does not move from the login page despite the tab indexes are being updated. I tried @avario suggestion setting a new UUID() as id on creating each tab when login/logoff but no results
|
|
The solutions presented here are good - but what about when the physical device rotates to landscape (or back to portrait) - then none of the solutions seem to hold. Maybe somebody has an answer to my question here (with a working example that shows proof of the device-rotation issue with TabView and tabViewStyle = .page) ??? |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's all new Paywall Editor allow you to remotely configure your paywall view without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.