天道酬勤,学无止境

swiftui

can I get the tap position over a Text View?

a Text view is like: Text("hello world") .onTapGesture{ ...} can I get the tab position ,so that to infer which word is tapped.

2021-09-24 08:25:36    分类:问答    swiftui

How to change navigationBar background color locally

I tried this method but it is global which is undesired. struct ExperienceView: View { init() { UINavigationBar.appearance().barTintColor = #colorLiteral(red: 0.1764705882, green: 0.2196078431, blue: 0.3098039216, alpha: 1) UINavigationBar.appearance().isTranslucent = false UINavigationBar.appearance().shadowImage = UIImage() } } And I tried this method but it is not working, I don't know why. I even tried copying original code on SwiftUI update navigation bar title color, still not working. struct ContentView: View { var body: some View { NavigationView { ScrollView { Text("Don't use

2021-09-24 04:15:47    分类:问答    swiftui   navigation

swiftui list not working when put into ZStack

I use ZStack to combine a list and Color, after doing it, List will not scroll and there's no output when clicking the text. Does anyone know how to fix it? Thanks struct ContentView: View { var body: some View { ZStack{ List{ ForEach(1...30, id: \.self){ i in Text("ROW \(i)") .font(.system(size: 40)) .onTapGesture { print("clicked \(i)") } } } Color.black.opacity(0.2) } } }

2021-09-24 03:34:39    分类:问答    list   swiftui

How to return different views based on a certain tag value?

I am trying to return a view within a .sheet modifier, from an array of view objects. I am having trouble getting the SwiftUI logic for setting tags for different views. This is probably something very simple but I am unable the figure it out. I have already tried an if statement, and a function with switch/case, but I am unable to return a specific view. I believe since I've added a tag manually to the object, once the condition is met, it returns only a single view (Destination View1) in all the buttons. This is my ForEach loop for an array of covers: var covers = coverData ForEach(covers) {

2021-09-24 03:15:35    分类:问答    swiftui

Using GeometryReader with ScrollView

I'm trying to use GeometryReader to assign a maxHeight to a list in a Scrollview. For the purpose of this question, I created the following project : GeometryReaderTesting. It sets up a ContentView with a : - Text. - List. - Text. I extracted the List and last Text into their own view, using @ViewBuilder and I want to set the maxHeight of the List to .5 the height of my user's screen. The problem is that the app won't build with the following errors, and the GeometryReader doesn't seem to be computing the correct height: Here is my ContentView code, if anyone has an idea of what I'm doing

2021-09-24 00:27:30    分类:问答    swiftui

How to create Toggle binding from array of objects?

There are several useful questions on this topic, but all I've found either use deprecated syntax from earlier betas (e.g., BindableObject) or do not require a binding be passed (e.g., to a Toggle). I want to create a list of Toggles that are bound to elements in an array. I've tried many methods, but the syntax is never corrrect. The version below most closely matches the answers to an existing question. struct Item: Identifiable { var id: String { self.name } var name: String var enabled: Bool } final class ItemSet: ObservableObject { @Published var items: [Item] init() { items = [ Item(name

2021-09-23 19:55:38    分类:问答    swift   swiftui

SwiftUI reduce spacing of rows in a list to null

I want to reduce the linespacing in a list to null. My tries with reducing the padding did not work. Setting ´.environment(.defaultMinListRowHeight, 0)´ helped a lot. struct ContentView: View { @State var data : [String] = ["first","second","3rd","4th","5th","6th"] var body: some View { VStack { List { ForEach(data, id: \.self) { item in Text("\(item)") .padding(0) //.frame(height: 60) .background(Color.yellow) } //.frame(height: 60) .padding(0) .background(Color.blue) } .environment(\.defaultMinListRowHeight, 0) .onAppear { UITableView.appearance().separatorStyle = .none } .onDisappear {

2021-09-23 18:53:57    分类:问答    swiftui   swiftui-list

SwiftUI Grid Layout

I'm trying to achieve the following grid layout with SwiftUI but not quite sure on the best approach. My code is below and it's not quite getting what I want and also seems hacky to have many nested stacks VStack { VStack { HStack { VStack { Text("Text Here") Text("336.851") } .padding(20) .background(ColorManager.orange) .cornerRadius(10) VStack { Text("Text Here") Text("336.851") } .padding(20) .background(ColorManager.red) .cornerRadius(10) } HStack { VStack { Text("Text Here") Text("336.851") } .padding(20) .background(ColorManager.green) .cornerRadius(10) VStack { Text("Text Here") Text(

2021-09-23 13:42:15    分类:问答    swift   swiftui   vstack   hstack

Using ARKit anchor to place RealityKit Scene

I am currently struggling to add an RealityKit Scene to an ARKit anchor. As my previous question, it is not possible to directly add the RealityKit Scene to an ARKit Anchor Node. But how can I use the ARKit Anchor to place my Scene? For example, I want to find an object in the room, and depending on which object I found, I want to use different scenes and anchor them in the room depending on the recognised objects position. Thanks for the help. Current try: func makeUIView(context: Context) -> ARView { let arView = ARView(frame: .zero) // Load the "Box" scene from the "Experience" Reality File

2021-09-09 05:00:17    分类:问答    swift   swiftui   augmented-reality   arkit   realitykit

How to set relative width in a HStack embedded in a ForEach in SwiftUI?

I wanted to create a list (without using List view) of attributes. Each attribute is a HStack which contains two texts, name and value. I want the name text to have always 30% of the width of the whole HStack and the value text to use the rest of the horizontal space. The height of each attribute depends on the content. I try to accomplish it by having a following view: struct FatherList: View { let attributes: Attributes init(_ attributes: Attributes) { self.attributes = attributes } var body: some View { VStack(spacing: CGFloat.spacing.medium) { ForEach( attributes, id: \.name, content

2021-09-09 02:34:03    分类:问答    ios   swift   swiftui   ios14