SwiftUI TabView PageTabViewStyle prevent changing tab?

The solution from mentioned reference works, just the swipe is blocked not by gesture(nil), but by gesture(DragGesture()). And view should be full-tab-content-view-wide, like TabView { ForEach(0..<5) { idx in Text(“Cell: \(idx)”) .frame(maxWidth: .infinity, maxHeight: .infinity) .contentShape(Rectangle()) .gesture(DragGesture()) // this blocks swipe } } .tabViewStyle(PageTabViewStyle()) Tested with Xcode 12.1 / iOS 14.1 * and, of course, … Read more

macOS SwiftUI Navigation for a Single View

Here is a simple demo of possible approach for custom navigation-like solution. Tested with Xcode 11.4 / macOS 10.15.4 Note: background colors are used for better visibility. struct ContentView: View { @State private var show = false var body: some View { VStack{ if !show { RootView(show: $show) .frame(maxWidth: .infinity, maxHeight: .infinity) .background(Color.blue) .transition(AnyTransition.move(edge: .leading)).animation(.default) … Read more

SwiftUI tappable subtext

Update for iOS 15 and higher: There is a new Markdown formatting support for Text, such as: Text(“Some text [clickable subtext](some url) *italic ending* “) you may check WWDC session with a timecode for details The old answer for iOS 13 and 14: Unfortunately there is nothing that resembles NSAttributedString in SwiftUI. And you have … Read more

How to configure ContextMenu buttons for delete and disabled in SwiftUI?

All of the asked situations are now supported in iOS 15 Destructive: (works from iOS 15) Set .destructive as the role argument of the button: Button(role: .destructive) { // 👈 This argument // delete something } label: { Label(“Delete”, systemImage: “trash”) } Disabled: (works from iOS 14.2) Add .disabled modifier to the button. Button { … Read more

Proportional height (or width) in SwiftUI

UPDATE If your deployment target at least iOS 16, macOS 13, tvOS 16, or watchOS 9, you can write a custom Layout. For example: import SwiftUI struct MyLayout: Layout { func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize { return proposal.replacingUnspecifiedDimensions() } func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) … Read more

SwiftUI View – viewDidLoad()?

We can achieve this using view modifier. Create ViewModifier: struct ViewDidLoadModifier: ViewModifier { @State private var didLoad = false private let action: (() -> Void)? init(perform action: (() -> Void)? = nil) { self.action = action } func body(content: Content) -> some View { content.onAppear { if didLoad == false { didLoad = true action?() … Read more

How to properly group a list fetched from CoreData by date?

Update for iOS 15 SwiftUI now has built-in support for Sectioned Fetch Requests in a List via the @SectionedFetchRequest property wrapper. This wrapper reduces the amount of boilerplate required to group Core Data lists. Example code @Environment(\.managedObjectContext) var moc @State private var date = Date() @SectionedFetchRequest( // Here we use SectionedFetchRequest entity: Todo.entity(), sectionIdentifier: \.dateString … Read more