swiftui

SwiftUI - Getting TextField to array in ForEach

爷,独闯天下 提交于 2021-01-29 07:36:29
问题 The goal is to have a TextField that takes characters as they're being entered and display them below the TextField in a 'pile'. By pile I mean, each new character is displayed on top of the last one. Piling the letters up is easy enough - I use ForEach to loop over an array inside a ZStack . To get the characters in the array, I used a custom binding. It works, but the problem is that the characters get repeated each time a new character is typed. For example, if the user types CAT, first

how to deal with swiftui @State optional unwrap

依然范特西╮ 提交于 2021-01-29 07:34:52
问题 in swiftui, I have a state variable count ,which is optional, in the sheet present ,I unwrap the optional and show Detailview, but it seems never hit there. any idea how why not hit there? it seems never hit DetailView(count: num) import SwiftUI struct ContentView: View { @State var showDetailView = false @State var count : Int? var testArr = [1,2,3,4,5] var body: some View { NavigationView { List(testArr.indices){ indice in Text("row num \(indice)") .onTapGesture{ self.showDetailView = true

swift - ForEach loop with state

元气小坏坏 提交于 2021-01-29 07:10:48
问题 If I have this code: @State var n = 3 var body: some View { VStack { Button(action:{ if self.n == 3 { self.n = 5 } else { self.n = 3 } }) { Text("Click me") } ForEach(1..<self.n+1) { i in Text(String(i)) } } } I expect this code to toggle between 1 2 3 and 1 2 3 4 5 on the screen when you click the button, however it only shows 1 2 3 . How would I fix this? 回答1: The form of ForEach that you have used is fine if the number of items is constant, but if the Views might change, then you need to

SwiftUI boundingRect doesn't work fine by UIViewRepresentable updateUIView

流过昼夜 提交于 2021-01-29 06:58:40
问题 I want to highlight texts in UITextView. Here is the CustomTextView I made. struct CustomTextView: UIViewRepresentable { @ObservedObject var tmpTextVM: TmpTextViewModel func makeUIView(context: UIViewRepresentableContext<CustomTextView>) -> UITextView { let textView = UITextView() textView.backgroundColor = UIColor.clear textView.isScrollEnabled = false textView.text = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" textView.font = UIFont(name: "ArialMT", size: 20) textView

How to make ZStack as top index even on the modal sheet in SwiftUI?

情到浓时终转凉″ 提交于 2021-01-29 06:52:49
问题 While I'm trying to make incoming call view with ZStack, I saw that It looks under modal sheet presented View. Can I make this incoming call view top even above modal view? Or How Can I dissmiss all modal sheet presentations at a time when a call arrived? AppView.swift import SwiftUI struct AppView: View { @ObservedObject var callVM = CallViewModel() var body: some View { ZStack { TabView { TabOne() .tabItem { Image(systemName: "list.dash") Text("Menu") } TabTwo() .tabItem { Image(systemName:

Delay in For Loop Swift

女生的网名这么多〃 提交于 2021-01-29 06:51:25
问题 I am creating a simple card game (Set) in SwiftUI. I have a button that will deal X new cards when tapped. Currently, it makes all cards show up at once. I was wondering how I could make them come out one at a time. Deal works by appending a new card to a Deck array in the model. ContentView displays each card in the grid. This is what I currently have after looking online. Displays first card then next all at once func deal(_ numberOfCards: Int) { withAnimation(Animation.easeInOut(duration:

List Sections do not work correctly if .navigationBarItems is present

故事扮演 提交于 2021-01-29 06:36:40
问题 How to have both a button in navigation bar and a list with sections? Here is a code with .navigationBarItems : struct AllMatchesView: View { @Environment(\.managedObjectContext) var moc @State var events = EventData() var body: some View { NavigationView { List{ ForEach(events.sections) { section in Section(header: Text(section.title)) { ForEach(section.matches) { match in Text("Row") } } .navigationBarTitle("Title") .navigationBarItems(trailing: NavigationLink(destination: AddMatchView()

SwiftUI UIViewRepresentable and Custom Delegate

人盡茶涼 提交于 2021-01-29 06:34:04
问题 When creating a UIViewControllerRepresentable for SwiftUI, how do you create a Coordinator so that it can access the delegate of a third party library? In this case, I am trying to access BBMetal, a photo-filtering library. This is a truncated version of the code we are trying to 'bridge' to SwiftUI: class CameraPhotoFilterVC: UIViewController { private var camera: BBMetalCamera! private var metalView: BBMetalView! private var faceView: UIView! override func viewDidLoad() { super.viewDidLoad(

SwiftUI, how to make the background color animate multiple times

好久不见. 提交于 2021-01-29 06:20:34
问题 I have a SwiftUI project that includes a Text object with a .onTapGesture working that, when triggered, should cause the button's background color to pop to another color then quickly fade back to the original color. In my code, I'm able to trigger the color pop, but it stays that way and won't fade back. I'm kind of at a loss on what to do, any help is appreciated...here is the code I'm using: @State var buttonFlash = false var body: some View { Text("Hello") .font(.system(size: 20)) .frame

SwiftUI: loading images with .fileImporter

主宰稳场 提交于 2021-01-29 00:09:28
问题 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