Search results for

swiftui

16,633 results found

Post

Replies

Boosts

Views

Activity

Reply to SwiftUI Selection in List
I tried following the example in the article about NavigationView:// // SomeObject.swift // SimpleMacApp // // Created by Eric Mader on 9/2/19. // Copyright © 2019 Eric Mader. All rights reserved. // import Foundation import SwiftUI struct SomeObject : Identifiable, Hashable { let id = UUID() let name: String let otherStuff: [String] } struct SomeObjectView : View { var someObjects: [SomeObject] @Binding var selectedObject: SomeObject? var body: some View { NavigationView { List(someObjects) { someObject in NavigationLink(destination: OtherStuff(selectedObject: someObject)) { Text(someObject.name) } } .navigationBarTitle(Text(Objects)) } } }This doesn't compile. It highlights the n in someObject.name and says Type of expression is ambiguous without more context. It compiles fine if I remove the NavigationView and NavigationLink.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’19
Getting and setting ScrollView scroll offset in SwiftUI
Trying to find a way how to get scroll view's scroll offset using GeometryReader. The view hierarchy I am after is Scrollview with custom view. Custom view (TileCollectionView) defines its size itself (it is custom collection/tile view with fixed number of columns).struct ContentView: View { var body: some View { VStack { Text(Scoll location) ScrollView([.horizontal, .vertical]) { GeometryReader { geometry -> TileCollectionView in print(geometry.frame(in: .global)) return TileCollectionView() } } } } } struct TileCollectionView: View { var body: some View { VStack(spacing: 1) { ForEach(0..<30) { seriesIndex in HStack(spacing: 1) { ForEach(0..<8) { columnIndex in TileView() } } } } } } struct TileView: View { var body: some View { Color.blue.frame(width: 128, height: 128, alignment: .center).fixedSize() } }What happens with this code is GeometryReader making the view size matching with ScollView size and it is not possible to scroll from one edge to another edge of the TileCollectionView. Wondering ho
6
0
13k
Sep ’19
How to have a complication to simply display the app name?
I've created an standalone watch app using SwiftUI. I want a complication that just show the app name. I thought the default should work do this? I've not made any change to the ComplitionController: func getLocalizableSampleTemplate(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTemplate?) -> Void) { // This method will be called once per supported complication, and the results will be cached handler(nil) }I can see my app name in the watch face complication selection list. But it's blank in the watch face itself. Touching it opens my app.What to do?Initially. the complication selection show my app as MyApp WatchKit App, on the watch face it's blank but touching the complication shows MyApp WatchKit App briefly and opens my app. I have no idea where the app name settings is in Xcode. I have set all the Display Name fields in Xcode as MyApp. When I changed the Identity and Type field from MyApp WatchKit App to MyApp, the compication configure selection list now s
1
0
976
Sep ’19
Possible to drive included views with new @FetchRequest property wrapper?
I want to drive SwiftUI views with the new @FetchRequestproperty wrapper from the results of a fetch request. The first level, so just using a Text in the same struct where the fetch request is used, works fine and the view is updated if core data changes. If I integrate a LandmarkRow(landmark: landmark)this will not be updated anymore (but if copied the exact same code to the main view, it works)Essentially I want to include views with the details as in the landmarks tutorial:ForEach(userData.landmarks) { landmark in if !self.userData.showFavoritesOnly || landmark.isFavorite { NavigationLink(destination: LandmarkDetail(landmark: landmark)) { LandmarkRow(landmark: landmark) } } }Maybe there is a tutorial or some more information on how to use @FetchRequst and inherited views correctly?Any idea on how to proceed?
0
0
563
Sep ’19
Reply to Getting and setting ScrollView scroll offset in SwiftUI
I've got a MacOS alternative implementation of SwiftUI ScrollView which adds support for a scrollToVisible(_ shouldScroll: Bool) style view modifier that does the trick. I'll post later this evening when I'm back at my machine. It should be reasonably easy to adapt for iOS UIScrollView if that's what you need instead.The idea behind it is that this NSViewRepresentable observes a ScrollToVisibleRects preference key I define, which is set for subviews wrapped using a ScrollToVisible view modifier.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’19
popToRootViewController in SwiftUI
What is the best practice to achieve popToRootViewController like functionality in SwiftUI? There are couple of ways to do it and one of them is described nicely here (https://stackoverflow.com/questions/57334455/swiftui-how-to-pop-to-root-view) via publish event but it losses the animation while popping to root. Please suggest.
10
0
6.8k
Sep ’19
SwiftUI crash deleting List element / conditional rendering
Using Xcode 11 beta 7, I'm getting an unexpected EXC_BAD_INSTRUCTION crash with this very simple view. It manages a list where you can add and delete items. The list starts off empty, and the crash occurs after you add any items and delete them so that the list is empty again.The conditional rendering of something different when the list is empty (if items.count == 0...) seems to have something to do, because if I unconditionally render a List there is no crash. Yet I see no issue with the code as it is. Note that right before crashing, Text(empty) is correctly rendered.Any ideas...?struct TestView: View { @State var items: [Int] = [] var body: some View { VStack { Button(action: { self.add() }) { Text(Add) } if items.count == 0 { Text(empty) } else { List { ForEach(items, id: .self) { item in Text(item #(item)) } .onDelete(perform: { self.delete(offset: $0.first! )}) } } } } func delete(offset: Int) { DispatchQueue.main.async { self.items.remove(at: offset) } } func add() { DispatchQueue.main.async { self.it
1
0
2.0k
Sep ’19
How to retrieve data from CloudKit using SwiftUI
I am using SwiftUI to make an app and storing the data on ICloud. Because all the code I can find relates to Swift and viewDidLoad and TableView, however this do not apply. I have written code and seems to retrieve it but will not return it to the ObservableObject to be able to display in the SwiftUIThe ObservableObject file:-import SwiftUI import Combine final class ObservedData: ObservableObject { @Published var venues = venuesData }The query to retrieve dataimport SwiftUI import CloudKit var venuesData: [Venue] = loadVenues() func loadVenues() -> [Venue] { var data = [Venue]() let pred = NSPredicate(value: true) let sort = NSSortDescriptor(key: id, ascending: true) let query = CKQuery(recordType: DeptfordHopData, predicate: pred) query.sortDescriptors = [sort] let operation = CKQueryOperation(query: query) operation.desiredKeys = [id,name, address] operation.resultsLimit = 50 var newVenues = [Venue]() operation.recordFetchedBlock = { record in let venue = Venue() venue.racordI
2
0
3.4k
Sep ’19
SwiftUI - how to keep Button on top?
I have the following SwiftUI view:struct Flashcard : View { @State var cardText = newArray[randomNum].kana var body: some View { let stack = VStack{ Text(cardText) .color(.red) .font(.title) .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: 200) .tapAction { self.flipCard() } HStack { Button(action: { print(button pressed) self.incorrect() }) { Image(systemName: xmark.circle) .font(.title) } .opacity(0.4) Button(action: { print(button pressed) self.correct() }) { Image(systemName: checkmark.circle) .font(.title) } .opacity(0.4) } } //.background(Color.blue) .cornerRadius(25) return stack }I'm trying to make the background colour of the VStack blue, but when I do that, I realise that the Buttons are no longer pressable. Is there a way to make them always on top? Should I be using a ZStack as well to make the Buttons the last view that is added to the stack? I tried using a ZStack but getting a lot of strange outcomes.
0
0
1.1k
Sep ’19
Reply to Getting and setting ScrollView scroll offset in SwiftUI
// // ScrollView.swift // Components // // Created by Mark Onyschuk on 2019-09-02. // Copyright © 2019 Mark Onyschuk. All rights reserved. // import SwiftUI // MARK: - ScrollView /// A `SwiftUI.ScrollView` replacement with support for a new`View.scrollToVisible(_:)` content modifier. public struct ScrollView<Content>: NSViewRepresentable where Content: View { private var axes: Axis.Set private var showsIndicators: Bool private var content: Content /// Initializes the ScrollView. /// - Parameter axes: scrolling axes /// - Parameter showsIndicators: `true` if scrolling indicators should be shown /// - Parameter content: scrollable content public init(_ axes: Axis.Set = .vertical, showsIndicators: Bool = true, @ViewBuilder content: () -> Content) { self.axes = axes self.showsIndicators = showsIndicators self.content = content() } // NSViewRepresentable public typealias NSViewType = NSScrollView public func makeNSView(context: NSViewRepresentableContext<ScrollView>) -> NSScrollView { le
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’19
Reply to How to retrieve data from CloudKit using SwiftUI
Curiously, this issue has nothing to do with SwiftUI. Rather, the problem is here:var venuesData: [Venue] = loadVenues()Here you initialise venuesData with the result from loadVenues. But loadVenues is an async routine (because it relies on CloudKit, and CloudKit always operates asynchronously). Try setting a breakpoint on line 40 and line 44. You’ll find that line 44 runs first, because the line 40 is called asynchronously in response to a CloudKit operation completing. Moreover, at line 44 the data value will be empty, because you haven’t yet executed line 35. To get this to work you’ll have to find a way to push the results of the async CloudKit operation into your venues property on ObservedData, and then set up your SwiftUI code to update based on that. Share and Enjoy — Quinn “The Eskimo!” Apple Developer Relations, Developer Technical Support, Core OS/Hardware let myEmail = eskimo + 1 + @apple.com
Topic: Programming Languages SubTopic: Swift Tags:
Sep ’19