Search results for

swiftui

16,623 results found

Post

Replies

Boosts

Views

Activity

Snapshot of a SwiftUI View
I'm wondering if anyone has figured out how to convert a SwiftUI View into an image?I've tried encapsulating a SwiftUI View into a UIHostingController and using the following code:import SwiftUI extension UIView { func image() -> UIImage { let renderer = UIGraphicsImageRenderer(size: self.bounds.size) let image = renderer.image { context in self.drawHierarchy(in: self.bounds, afterScreenUpdates: true) } return image } } extension UIHostingController { func image() -> Image? { guard let view = self.view else { return nil } let image = view.image() return Image(uiImage: image) } } struct RasterizedView<Content: View>: View { var controller: UIHostingController<Content> init(_ content: Content) { self.controller = UIHostingController(rootView: content) } var body: some View { controller.image() } } #if DEBUG struct RasterizedViewTest_Previews: PreviewProvider { static var previews: some View { RasterizedView(Text(TEST)) } } #endifThis code displays nothing. I went int
10
0
12k
Aug ’19
why the complex types?
I lot of the frustration I'm having with trying to do simple things in SwiftUI seems to have to do with the complexity of the types of values involved. Type inference failing, and I'm unable to come up with the right types myself. Error messages not localized to where the actual mistake is!Sure, one can blame me for not completely understanding this, but this is not user friendly.Why does View need to be a generic protocol? Why all the complexity? Why the hard-to-understand error messages?Mind you: I've been coding in swift for several years now, so while I'm not an expert, I'm no novice either.
1
0
656
Aug ’19
Xcode 11 Beta 5 Building Lists and Navigation
In the Building Lists and Navigation tutorial, Section 2, we are supposed to add the line landmark.image(forSize: 50)to the body of LandmarkRow. Unfortunately, this method is not defined in the Landmark structure of the file to be downloaded. It is defined in the Landmark.swift file in the documentation, but when I tried to include that definition in the downloaded file, I was told that the size parameter is extra in the call:ImageStore.shared.image(name: imageName, size: size)Looking at the ImageStore class, it seems that the image method is indeed defined to take a single parameter: the name. I know this is more in the line of reporting something rather than asking a question, but it would seem that removing the tutorial altogether would be preferable to removing a function from a structure definition and leaving the learner wanting. So, to the point... is there a way to scale this image? What is the preferred way to constrain an image to a particular point size in SwiftUI? Thanks in advance for an
2
0
1.4k
Aug ’19
Conditionally compiling SwiftUI views at preview time
Is it possible to detect when Xcode is building my SwiftUI code for a live preview?I am working with Core Data managed objects as the data source for SwiftUI views, but they need to be created within a managed object context. At preview time, it's not obvious how to get at the app's persistent container or an existing managed object context. Ideally, I'd prefer to create a temporary store in memory, anyway, both for speed and to not pollute the data used when I run the app.I'm hoping there is either a compiler flag (like the existing DEBUG one) or something in the process environment that signals that the code is either being built for a preview or is running in preview mode.
4
0
9.4k
Aug ’19
Reply to Conditionally compiling SwiftUI views at preview time
Following my usual pattern of figuring things out soon after posting a question publicly, I discovered a few things this afternoon.There is an environment variable set when Xcode runs a build for previews: ENABLE_PREVIEWS = YES. This doesn't help my question above, but it does let me skip my SwiftLint build phase. I've long had issues with the use of @IBDesignables in a storyboard and SwiftLint rewriting files (through the autocorrect feature) fighting over a file's newness and getting lots of pop-ups about reverting or keeping Xcode's version. I don't know yet if this environment variable will help with that.To my original question, yes, Xcode does indeed set an environment variable in the process when it's running code for the purpose of generating a preview: XCODE_RUNNING_FOR_PREVIEWS = 1.I'm still getting a blank preview canvas after setting up a different managed object context and seeding it with some data. If I don't reference the objects in the SwiftUI view (by instead using static text), I'l
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Aug ’19
iOS13 beta 7: Landmarks tutorial and dyld: Symbol not found: _$s7SwiftUI7BindingVyxGAA0C11ConvertibleAAMc
When I install the Landmarks demo I getdyld: Symbol not found: _$s7SwiftUI7BindingVyxGAA0C11ConvertibleAAMc Referenced from: /var/containers/Bundle/Application/C33D60FD-1755-4C71-BE6D-539910E65F6E/Landmarks.app/Landmarks Expected in: /System/Library/Frameworks/SwiftUI.framework/SwiftUI in /var/containers/Bundle/Application/C33D60FD-1755-4C71-BE6D-539910E65F6E/Landmarks.app/LandmarksI use Xcode 11 beta 5, tested on iOS 13 beta 6 and 7 on real device iPhone SE. Using the simulator is working fine.In my own project I have the same issue, I got some warnings from Xcode 11 beta 5 likesubscript(_:)' is deprecated: See Release Notes for migration pathAfter rewriting the code as in the release notes and the code segments in other forums, the message is gone, but the issue still exists.Anyone haven the same issue with the Landmarks demo, too?
1
0
736
Aug ’19
Reply to ObservedObject throws EXC_BAD_ACCESS in beta 6
Yes! Same issue here (Xcode 11 Beta 5, iOS 13 Beta 7, iPhone XR). It took me forever to figure out it was ObservedObject that was causing the crash. Ended up creaing a virtually blank Xcode project to replicate and sure enough adding any ObservedObject to a SwiftUI view causes a crash every time on a real device. I'm working with NFC, so I need to use an actual device rather than the simulator, but obviously have hit a wall.
Topic: App & System Services SubTopic: Core OS Tags:
Aug ’19
Dynamic ordering of HStack with SwiftUI?
Hello,I'm trying to craft a really specific beavior with SwiftUI: implementing a dynamic ordering of HStack/VStack.That's something really easy to do with UIKit but I can't figure out how I can do that with SwiftUI.The context is simple, I'm writing a navigation app for pilots, and the goal is to make the app usable with one hand in a bumpy environment. That mean fixed iPad on the dash, and all active conrols available from a unique lower corner (so the palm can be maintained by the corner itself).And the goal is to have one settings to set the app to be used from the lower left or right concern depending of the dash setup.Any idea how I can do that with SwiftUI?
1
0
3.1k
Aug ’19
Reply to Crash with Stepper on MacOS
Clever hack! It turns out that you can just do this, and it seems to work around the crash as well:public extension NSStepper { override func mouseEntered(with event: NSEvent) { // prevents crash in SwiftUI via NSHostingView.hoverRegionIdentifier } override func mouseExited(with event: NSEvent) { // prevents crash in SwiftUI via NSHostingView.hoverRegionIdentifier } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Aug ’19
SwiftUI: detect force click and modifier key pressed
I’m using SceneKit to render some spheres and cylinders so I’m using NSViewRepresentable protocol to import the SCNView to SwiftUI.Then I’m using the view as any SwiftUI view.I need to detect some gestures in order to move the camera and make my 3D objects rotate.For instance, I’m using the DragGesture to rotate the camera.What I was used to do with Storyboards is, in the NSViewControler, to detect force click and also flagsChanged to detect the press of modifier keys such as shift or control.I’m using that together with NSPanGestureRecognizer in order for instance to translate the objects instead to rotate them (if no modifier key is pressed).So my questions are:1) Is there a way to detect force click with the trackpad in SwiftUI? I haven’t found anything in the Gesture. I can use LongPressGesture but it's not the same.2) How to detect key pressed, in particular modifier keys during a Gesture is SwiftUI?Best regardsVincent
0
0
1k
Aug ’19
catalina + xcode 11 very very slow on 2013 iMac
Ran fine under Mojave and Xcode 10. WIth Catalina beta and Xcode beta (both latest versions although it's always been like this) the whole machine just crawls when Xcode 11 is open (not running) on a simple SwiftUI project. Just now had to open the scanner for my HP printer and it took a good minute before being ready and was again slow when trying to do a scan. Worked ok, but probably 10x slower then under Mojave.I figure there's two possibilities:1) something in my installation is wrong (although Activity Monitor shows nothing unusual). Machine has only 8 GB of ram and it is definitely being mostly used although I only have Xcode and Safari open.2) my iMac is simply overpowered.Neither of these really makes sense. Any ideas/comments?Thanks, Tom
2
0
1.5k
Aug ’19