-
Notifications
You must be signed in to change notification settings - Fork 418
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix issues with SwiftUI views being reset when updating
The current implementation calls reloadData on each update, which re-creates the entire content view and resets any state (like scrolling). To prevent this, we update the existing UIHostingController if the current page is the same. This requires us to have stable identifiers, to know if the current page is the same as before or a new one. For title-based pages, we just use the title as the identifier. For pages with custom SwiftUI headers, we default to using the index of the view as the identifier. This is the same behaviour that we have today, although it will only work when having static pages. In order to support dynamic pages with custom SwiftUI headers, we introduce a new initializer that allows specifying the identifier.
- Loading branch information
1 parent
ecbfc1a
commit 279c935
Showing
7 changed files
with
164 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import Parchment | ||
import SwiftUI | ||
import UIKit | ||
|
||
struct ScrollingView: View { | ||
var body: some View { | ||
PageView { | ||
Page("First") { | ||
ScrollingContentView() | ||
} | ||
Page("Second") { | ||
ScrollingContentView() | ||
} | ||
Page("Third") { | ||
ScrollingContentView() | ||
} | ||
} | ||
} | ||
} | ||
|
||
struct ScrollingContentView: View { | ||
var body: some View { | ||
List { | ||
ForEach(0...50 , id: \.self) { item in | ||
NavigationLink(destination: Text("\(item)")) { | ||
Text("\(item)") | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters