swiftui

SwiftUI: loading images with .fileImporter

前提是你 提交于 2021-01-29 00:03:39
问题 Goal is to load 2 different images (image 1 and 2) with the new .fileImporter modifier. Problem is I get the same image loaded to both thumbnails (image 1 and 2). Have anyone managed to do that with .fileImporter modifier? import SwiftUI struct ContentView: View { @State var openFile = false @State var img1 = UIImage() @State var img2 = UIImage() @State var fileName = "" var body: some View { Form { //image 1 Button(action: { self.openFile.toggle() }){ Image(uiImage: self.img1) .renderingMode

SwiftUI: loading images with .fileImporter

这一生的挚爱 提交于 2021-01-28 23:53:00
问题 Goal is to load 2 different images (image 1 and 2) with the new .fileImporter modifier. Problem is I get the same image loaded to both thumbnails (image 1 and 2). Have anyone managed to do that with .fileImporter modifier? import SwiftUI struct ContentView: View { @State var openFile = false @State var img1 = UIImage() @State var img2 = UIImage() @State var fileName = "" var body: some View { Form { //image 1 Button(action: { self.openFile.toggle() }){ Image(uiImage: self.img1) .renderingMode

SWIFTUI: Take out a gray rectangle on top of the TabBar

纵饮孤独 提交于 2021-01-28 20:43:09
问题 I have a problem related on my List view. The question is simple: how can I get rid of that wierd gray rectangle showing on top of the TabBar? I didn't code that, I just implemented a controller with a List and NavigationBar and then it showed that thing. For more clear explanation I post the images: ItemRow.swift code: import SwiftUI struct ItemRow: View { static let colors: [String: Color] = ["D": .purple, "G": .orange, "N": .red, "S": .yellow, "V": .pink] var item: MenuItem var body: some

Loading image from remote URL asynchronously in SwiftUI Image using combine's Publisher

人走茶凉 提交于 2021-01-28 20:03:05
问题 I was looking for good solutions for loading images asynchronously from a remote server image URL. There were many solutions online. It's a shame Apple doesn't provide one natively for something that is so common. Anyways, I found Sundell's blog really interesting and took the good bits from it to create my own ImageLoader, as shown below: import Combine class ImageLoader { private let urlSession: URLSession private let cache: NSCache<NSURL, UIImage> init(urlSession: URLSession = .shared,

Infinite vertical scrollview both ways (add items dynamically at top/bottom) that doesn’t interfere with scroll position when you add to list start

岁酱吖の 提交于 2021-01-28 19:37:02
问题 I’m after a vertical scrollview that’s infinite both ways: scrolling up to the top or down to the bottom results in more items being added dynamically. Almost all help I’ve encountered is only concerned with the bottom side being infinite in scope. I did come across this relevant answer but it’s not what I’m specifically looking for (it’s adding items automatically based on time duration, and requires interaction with direction buttons to specify which way to scroll). This less relevant

SwiftUI Path .closeSubpath() not working the same way as UIBezierPath .close()

寵の児 提交于 2021-01-28 19:19:57
问题 I'm trying to draw the following shape with SwiftUI: Before SwiftUI I just had to create a UIBezierPath, add the corners with addArc and then finally call close(), but I don't get the same result when calling closeSubpath on SwiftUI Path. Here's my code: Path { path in let width: CGFloat = 23 let height: CGFloat = 24 let arrowWidth = height / 2.0 let cornerRadius = height / 7.5 path.addArc(center: CGPoint(x: width - cornerRadius, y: cornerRadius), radius: cornerRadius, startAngle: Angle

Why can't swiftui distinguish 2 different environment objects?

↘锁芯ラ 提交于 2021-01-28 19:07:51
问题 I have this code and would expected a b as Text. Result: a a -> see screenshot. What am I doing wrong? import SwiftUI class PublishString : ObservableObject { init(string: String) { self.string = string print(self.string) } @Published var string : String = "a" } struct ContentView: View { @EnvironmentObject var text1 : PublishString @EnvironmentObject var text2 : PublishString var body: some View { VStack { Text(text1.string) Text(text2.string) } } } struct ContentView_Previews:

SwiftUI - Passing a variable to Observable Object

随声附和 提交于 2021-01-28 19:03:40
问题 I have a view that's receiving an ID value from the previous view. I need to also take that ID value and pass it into an ObservableObject so that I can run a Firestore query on that ID. I'm at a loss as to how to grab that ID for my ObservableObject. struct ItemView: View { var itemName: String var itemID: String @ObservedObject var items = getItemsData() // ..... etc } class getItemsData() { @Published var data = [items]() init() { let db = Firestore.firestore() db.collection("items")

Downsize but not upsize a SwiftUI image

我的未来我决定 提交于 2021-01-28 18:51:11
问题 I have a list of profile images (of various sizes) that I want each one to downsize to fit into its view, but I don't want them to be upsized and be pixilated. Instead, I want small images to stay at the resolution they are at. How do I can achieve this? This is what I've been using so far (but it resizes up): VStack { Image(...) .resizable() .scaledToFill() }.frame(width:200, height:200) 回答1: I did not find simple solution in API either, so here is a placeholder that looks appropriate for me

How to select an item in ScrollView with SwiftUI?

[亡魂溺海] 提交于 2021-01-28 18:34:55
问题 what I am trying to accomplish is have a loop of items where I am able to tap one and it gets bigger programmatically once tapped here is my code and my results so far: struct ContentView: View { @State var emojisArray = ["📚", "🎹", "🎯", "💻"] @State var selectedIndex = 0 var body: some View { VStack { ScrollView(.horizontal) { HStack { ForEach(0..<emojisArray.count) { item in emojiView(emoji: self.emojisArray[item], isSelected: item == self.selectedIndex ? true : false) .onTapGesture { print