Post

Replies

Boosts

Views

Activity

NavigationSplitView No Toggle Button
I went back to square one since the NavigationLink isActive is deprecated, this is when I found out about NavigationSplitView which removes the hassle of creating a 2 column app. However, I can never figure out why the toggle button does not show. I copied this sample code (already tried 2 tutorials, copy pasted them but still no toggle button) Anyone know why? import SwiftUI enum DemoScreen: String, Codable {   case first, second, third   var title: String {     rawValue.capitalized   } } struct ContentView: View {   @State   private var selection: DemoScreen? = .first   var body: some View {     NavigationSplitView {       sidebarContent     } detail: {       detailContent     }   } } extension ContentView {   var sidebarContent: some View {     List {       link(to: .first)       link(to: .second)       link(to: .third)     }   }   func link(to page: DemoScreen) -> some View {     NavigationLink(value: page) {       Text(page.title)     }   } } extension ContentView {   @ViewBuilder   var detailContent: some View {     if let selection = selection {       detailContent(for: selection)         .buttonStyle(.bordered)     } else {       Text("No selection")     }   }   @ViewBuilder   func detailContent(for screen: DemoScreen) -> some View {     switch screen {     case .first: Button("First button") {}     case .second: Button("Second button") {}     case .third: Button("Second button") {}     }   } } Why is there no toggle button above? And why is the menu always showing? I cant see the detiail view anymore
1
0
909
Nov ’22
Why is this URL nil?
I am testing this in playground. I have no clue why this url does not work? nil is returned. print(URL(string: "http://202.90.159.177/geoserver/wms?SERVICE=WMS&VERSION=1.3.0&REQUEST=GetMap&FORMAT=image/png&TRANSPARENT=true&LAYERS=Boundary:Boundary_Province&TILED=true&WIDTH=256&HEIGHT=256&CRS=EPSG:3857&STYLES&BBOX=%f,%f,%f,%f")) The cause is &BBOX=%f,%f,%f,%f but i do not understand why that full url doesnt work if that is included. Going to use this in GMSURLTileLayer in ios maps utils.
1
0
672
Nov ’22
How To Use Objective C Library In Swift Project
Hi all. I need help with this as i struggled to actually use the objective c library. https://github.com/ngageoint/simple-features-geojson-ios It says install from cocoapods. check. I got it. My issue is with the bridging header. I do not understand the posts and answers that I came across in stackoverflow. do i not need to install the library from cocoapods? instead copying the .h and .m to my swift project? And thereby generating that bridging header file? So i did just that and set #import "sf-geojson-ios-Bridging-Header.h" in the bridging header file. But when i build it, i keep getting the error message Build input file cannot be found: project-Bridging-Header.h'. Did you forget to declare this file as an output of a script phase or custom build rule which produces it? what is the correct way to do this? am i missing something?
1
0
989
Nov ’22
Slide View Does Not Animate. But Fade Works.
Hi. I am having problems with animating my view sliding in and out and hiding if not in view. The thing is, fading in and out works. but moving it does not. What could be wrong or what Am i missing? import SwiftUI struct MyView: View {       @Binding var isShowing: Bool       var body: some View {     VStack(spacing: 0) {       Group {         row("#ff0000", "color1")         row("#ff0000", "color2")         row("#ff0000", "color3")         row("#ff0000", "color4")         row("#ff0000", "color5")         row("#ff0000", "color6")       }       Group {         row("#ff0000", "color7")         row("#ff0000", "color8")         row("#ff0000", "color9")         row("#ff0000", "color10")         row("#ff0000", "color11")         row("#ff0000", "color12")       }     }     .frame(maxWidth: 130, maxHeight: 250)     .background(.white.opacity(0.7))     .padding() //    .transition(.move(edge: isShowing ? .leading : .trailing))     .opacity(isShowing ? 1 : 0)     .animation(isShowing ? .easeIn(duration: 0.7) : .easeOut(duration: 0.7), value: isShowing)   }       private func row(_ color: String, _ labelKey: String) -> some View {     return HStack {       Rectangle()         .fill(Color(UIColor(hex: color)))         .frame(width: 15, height: 15)         .padding(.leading, 3)       Text(labelKey.uppercased())         .foregroundColor(.black)       Spacer()     }   } } and this is how to use the view var body: some View { @State private var isShowing = true HStack {     Button("click me", action: { isShowing.toggle() }) .padding() } .frame(maxWidth: .infinity, maxHeight: .infinity)     .overlay(       MyView(isShowing: $isShowing),       alignment: .bottom     ) }
1
0
580
Dec ’22
Place Text on top of lines separating the colors
Hi, this is my custom view struct MyView: View {   var body: some View {     GeometryReader { geometryReader in       ZStack {         VStack(spacing: 0) {           Rectangle()             .fill(.red)             .frame(height: geometryReader.size.height * 0.25)                       Rectangle()             .fill(.orange)             .frame(height: geometryReader.size.height * 0.5)                       Rectangle()             .fill(.yellow)             .frame(height: geometryReader.size.height * 0.25)         }                   VStack {           Spacer()           Image(systemName: "figure.wave")             .resizable()             .aspectRatio(contentMode: .fit)             .frame(height: geometryReader.size.height * 0.8)         }       }     }     .frame(maxWidth: 100, maxHeight: 200)     .background(.white.opacity(0.7))     .cornerRadius(5)     .shadow(color: Color.gray.opacity(0.7), radius: 8, x: 0, y: 0)     .padding()   } } struct MyView_Previews: PreviewProvider {   static var previews: some View {     MyView()   } } I am not sure what the best approach for this is. I want to place some text (please see image). I was thinking adding another VStack at the bottom but i am not sure how i can position it correctly. Thoughts?
1
0
624
Dec ’22
How To Remove Extra Space Around Text
I have an image and text side by side in an HStack set to top alignment. I have no clue what this extra space is. The square shape and the text are not top aligned. I tried to set padding(0) to Text() to no avail. Any idea what this is? This is the code for the HStack. HStack(alignment: .top) {       Rectangle()         .fill(.red)         .frame(width: 15, height: 15)         .padding(.leading, 3)       Text("Heyasfd asdfsafds af awf s".uppercased())         .foregroundColor(.black)       Spacer()     } Thoughts?
1
0
4.2k
Dec ’22
Aspect Ratio Of Image is Whacked
Hi. I am using Kingfisher library to load images. My problem is that when i resize the width, the calculated height does not follow. the height of the image when displayed is still short. What could possibly be wrong in my code? import SwiftUI import Kingfisher struct MyView: View {       var geometryProxy: GeometryProxy       var body: some View {     HStack {       HStack {         KFImage.url(URL(string: "https://volcano.si.edu/gallery/photos/GVP-11076.jpg"))           .cancelOnDisappear(true)           .fade(duration: 0.25)           .resizable()           .scaledToFit()       }       .frame(maxWidth: geometryProxy.size.width * 0.4, maxHeight: getHeight(Float(geometryProxy.size.width) * 0.4, Float(geometryProxy.size.height), Float(geometryProxy.size.width)))       Text("Text 1 Label")         .frame(maxWidth: geometryProxy.size.width * 0.65, alignment: .topLeading)         .padding(.leading, 10)         .padding(.top, 3)       Image(systemName: "chevron.right")         .frame(maxWidth: geometryProxy.size.width * 0.05)         .padding(.leading, 0)     }     .contentShape(Rectangle())   }       private func getHeight(_ imageWidth: Float, _ imageHeight: Float, _ screenWidth: Float) -> CGFloat { //    let ratio = Float(placeholder!.size.height / placeholder!.size.width) //    print(ratio) //    print(ratio * width) //    print("______") //    return CGFloat(ratio * width)     let ratio = screenWidth / imageWidth     return CGFloat(imageHeight * ratio)   } } The Image should expand since i set the width of the image to at least 40% of the screen width. Or could my calculation for getting the height may be wrong? Thougts?
1
0
964
Dec ’22
Why Can't I do var test: some View?
I want to have a property in a struct/class where it is of type custom View e.g. var view: some View? So many issues here, I ended up doing var view: AnyView? But i got stuck here because whenever I assign a value to this, the app crashes. saying Could not cast value of type '__NSCFNumber' (0x7fdcdf9c3480) to 'NSString' (0x1192bac50). This i do not understand view should be some view so it si neither number nor string. any idea why? also. if i do this var view: (some View)? it is ok. but when i set a value to that property it says Cannot assign value of type 'DailyCityReportView' to type 'some View' My goal is to pass a custom view to a property.
1
0
804
Dec ’22
How Can WithAnimation Code be placed inside the view to be animated
Hi. My view gets animated with a call frmo a block of code withAnimation.{ } how can i place this inside the view so that the binding variable will be the one to trigger the animation. Same as how sheets work. THis is my sample view import SwiftUI struct GradientLegendView: View {       @Binding var isShowingLegend: Bool       var labels: [String]?   var colors: [Color]?   var width: Float   var height: Float   var viewRect = Rectangle()       var body: some View {     if isShowingLegend {       HStack {         VStack {           if let labels {             ForEach(labels, id:\.self) { label in               Text(label)                 .frame(maxWidth: .infinity, alignment: .leading)                 .font(.system(size: 14))                 .foregroundColor(.black)                 .backgroundStyle(.red)                 .padding(0)                               if label != labels.last {                 Spacer()               }             }           }         }         .frame(maxHeight: .infinity)         .padding(8)         Rectangle()           .foregroundColor(.clear)           .background(LinearGradient(gradient: Gradient(colors: colors ?? []), startPoint: .top, endPoint: .bottom))           .padding(8)           .frame(width: 30)       }       .frame(width: CGFloat(width), height: CGFloat(height))       .background(.white.opacity(0.7))       .cornerRadius(5)       .shadow(color: Color.gray.opacity(0.7), radius: 8, x: 0, y: 0)       .padding()       .transition(.move(edge: isShowingLegend ? .leading : .trailing))     }   } } struct GradientLegendView_Previews: PreviewProvider {       static var previews: some View {     GradientLegendView(       isShowingLegend: .constant(true),       labels: SampleData.createHeatmapLegendLabelArray(),       colors: SampleData.createHeatmapLegendColorArray(),       width: 120,       height: 200     )   } } struct Sample { static func createHeatmapLegendLabelArray() -> [String] {     return ["Great", "Major", "Strong", "Moderate", "Small"]   }        func createHeatmapLegendColorArray() -> [Color] {     return [.red, .purple, .orange, .green]   } } And i use it here and run the animation using withAnimation { } @State private var isShowingLegend = true var body: some View {     Text("Hey") .frame(maxWidth: .infinity, maxHeight: .infinity)     .edgesIgnoringSafeArea(.all)     .task {       setupMenuItems()     }     .overlay(       HStack {           GradientLegendView(             isShowingLegend: $isShowingLegend,             labels: SampleData.createHeatmapLegendLabelArray(),             colors: SampleData.createHeatmapLegendColorArray(),             width: 120,             height: 200)         Spacer()       },       alignment: .bottom     ) } Now, say i also have a button instead of Text("hey") where the action code is withAnimation(isShowingLegend ? .easeIn(duration: 0.5) : .easeOut(duration: 0.5)) {         isShowingLegend.toggle()       } Is it possible that this withAnimatoin be placed inslide GradientLegendView and will execute if the isShowingLegend value is change?
1
0
1.1k
Dec ’22
Will 2 environmentObject work?
Ive read posts where you can pass 2 .environmentObject() but what i noticed is that if i call object1.objectWillChange.send() , object2 also is affected is this normal? or supposedly only published properties of object1 only gets affected? discussion quwstion. thoughts?
1
0
425
Dec ’22
How To Fix CoreData: error: Failed to load mode model named ...
Hi. I have no clue what I am doing wrong or am i missing something else. I keep getting the error message CoreData: error: Failed to load model named DMTideCity Please see attached image for data model. My goal is just to load this but I dont know why I keep getting that error message. struct TideDataController {       static let shared = TideDataController()   let cityContainer: NSPersistentContainer       init() {     print("init")     cityContainer = NSPersistentContainer(name: "DMTideCity")     print(cityContainer)     cityContainer.loadPersistentStores { description, error in       if let error {         print("Core Data Citys failed to load: \(error.localizedDescription)")       }       else {         print("Core Data Citys loaded")       }     }   } } In my view, i declare a variable like this let tideDataController = TideDataController.shared So it never reaches the print(cityContainer) because of that error message. Thoughts?
1
0
1.8k
Dec ’22
Why Doesnt this Wrap Correctly?
struct TideForecastInfoView: View {       var geometryProxy: GeometryProxy       var body: some View {     VStack {       getHeaders(geometryProxy)               TideForecastEntryView(geometryProxy: geometryProxy)           .padding(2)     }     .background(.white)     .clipShape(RoundedRectangle(cornerRadius: 14))     .shadow(radius: 8)   }       @ViewBuilder   func getHeaders(_ geometryProxy: GeometryProxy) -> some View {     HStack {       HStack {         Text("tide")           .frame(maxWidth: geometryProxy.size.width * 0.25)         Text("time")           .frame(maxWidth: geometryProxy.size.width * 0.5)         Text("height")           .frame(maxWidth: geometryProxy.size.width * 0.25)       }       .padding(8)     }     .frame(maxWidth: .infinity)     .background(.gray)   }   } struct TideForecastEntryView: View {       var geometryProxy: GeometryProxy       var body: some View {     HStack {       Text("High Tide")         .frame(maxWidth: geometryProxy.size.width * 0.25)       Text("1:08 AM (Tue 03) January")         .frame(maxWidth: geometryProxy.size.width * 0.5, alignment: .leading)       Text("1.31 m (4.3 ft)")         .frame(maxWidth: geometryProxy.size.width * 0.25)     }   } } Result looks like this 03 should be on the first line and maybe parts of the January word. is there something else missing in the Text view option that i need to declare? Also how to vertical align top for text High Tide. it is always vertically centered. I tried to set alignment: .top in the frame but doesnt do anything. Thoughts?
1
0
794
Jan ’23
How To Create Data Json Object from Empty String
This is what I tried to do. I am not sure what is wrong. anyone got ideas? let data: Data? = try? JSONSerialization.data(withJSONObject: "") and it says reason: '*** +[NSJSONSerialization dataWithJSONObject:options:error:]: Invalid top-level type in JSON write' even if i put in "{}" error is the same. thoughts? Purpose for this is i want set this to a URLRequest.httpBody Thoughts?
1
0
1.2k
Jan ’23
Possible to set body view's background to transparent?
Please see screenshot. Currently the one in white has an animation in the middle. i wish to set the background of the body's view to transparent so that only the animation from my app is visible (it's like the home screen but only my animation is shown when the app is run. Is that possible? I tried to set VStack's background color opacity to 0 but nothing happpens. Or is it something else that needs to be modified? Thoughts?
1
0
710
Jan ’23