Post

Replies

Boosts

Views

Activity

TabView Reloads all Tab
I wish to ask suggestion on how to avoid this. i dont want every tab switch to reload the component..worse, fetch from url s this where mvvm comes in? so that even if component is reloaded, the mvvm data will be used? i think it is overkill though. my first and 3rd tab fetches data and loads markers in a map. my 2nd tab is a wkwebview that loads a url i noticed that whej i switch to the 3rd tab, the 1st tab gets reloaded still. thoughts on how to avood reloading? also tried setting tag to each tab item and setting a state variable to tab view to keep track of tab switching. to no avail. thoughts?
4
0
1.9k
Nov ’22
Why AVPlayer always nil
I do not understand why AVPlayer is always nil  Unexpectedly found nil while unwrapping an Optional value struct SatelliteVideoView: View {       init() {     player = AVPlayer(url: URL(string: "https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-mp4-file.mp4")!) }       var body: some View {     VideoPlayer(player: player!) } The documentation uses this code.
7
0
1.3k
Nov ’22
How To Open New Window When GMSMarker Info Window is Clicked in SwiftUI
I have managed to do tasks when an info window of a GMSMarker is clicked. This is where the magic happens func mapView(_ mapView: GMSMapView, didTapInfoWindowOf marker: GMSMarker) However, this time, I wish to open a custom view as a new window. I have not seen any helpful posts so I decided to try and ask the question here if anyone has experienced doing so. My goal is when the info window is clicked, i wish to open my custom view as a new window. Currently, I only know NavigationView and NavigationLink when opening up new windows but I am not sure how to use this in a GMSMarker info window click event. Thoughts?
0
0
400
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
Stepper is still buggy?
Anyone experienced this. My goal is to use AppStorage to save the value in the stepper but the behavior is whaked. Buttons get disabled even when theyre not at both ends of the array if i use State, it works a bit better but the buttons only get disabled after the 2nd or 3rd try when the index is either 0 or at the end of the array. @AppStorage("index") private var opacity = 0 let options = [40, 50, 70, 80, 90, 100]         Stepper {           Text("\("Opacity") \(options[opacity].description)%")         } onIncrement: {           opacity = opacity + 1           if opacity >= opacity.count {             opacity = opacity.count - 1           }           print("onincrement="+String(opacity))         } onDecrement: {           opacity = opacity - 1           if opacity < 0 {             opacity = 0           }           print("ondecrement="+String(opacity))         } The sample code looks simple. I am not sure what else I could be missing?
2
0
583
Nov ’22
Is it possible to rebuild overlay only and not the body?
Hi. I am not sure how to do this case scenario I am having. Please see sample code @State private var tileLayers: [GMSURLTileLayer]?    @State private var isShowingLegend = true     var body: some View {     MapView(       tileLayers: tileLayers     )     .task {       setupMenuItems()               if load {         load = false         processRainWatcher()       }     }     .overlay(       isShowingLegend ?         HStack {           RainWatcherLegendView()           Spacer()         }         .transition(.move(edge: isShowingLegend ? .leading : .trailing))       : nil,       alignment: .bottom     ) } This is not a working code. But i wish to ask suggestion. because the overlay shows a legend view. If isShowingLegend.toggle() is triggered, it animates sliding in and out of the screen. Problem mis, everything gets rebuilt. so the tile layer in the MapView() gets re-rendered again. is there a way not to let this happen, but still let the legend animate sliding in and out? or perhaps pass a binding variable to the legend view? and do the animation in that view? so that the parent view does not get rebuilt? thoughts?
2
0
751
Dec ’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
581
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
Can A Property In An ObservableObject listen to Another Property? How?
Hi. this is my observable object final class ToolbarItemProperties: ObservableObject {   var visible = false   var disabled = false   var icon = "questionmark"   var action: () -> Void = { }   var opacity: Double = 1 } the opacity property value should either be 0.5 or 1 depending on the value of the property disabled. How to listen to it? The purpose for this is because if i set a button to disabled, it does not render any way to the user to let them know it is disabled. So i thought of changing the opacity instead, hence the opacity property. So if i do something like this item.disabled = true the opacity property value should become 0.5. if disabled is set to false, the opacity value will be 1.
5
0
1.3k
Dec ’22
Anyone Worked With AsyncView By Ralf Ebert, Doesnt Work In TabView
struct Test { var body: some View {     AsyncView(       operation: { try await getData("https://localhost") },       content: { json         Text("data retrieved")       }     )   } func getData(_ url: String) async throws -> String {     return try await getRequest(url)   } } static func getRequest(_ sourceData: String?, _ useWindowsChar1252: Bool = false) async throws -> String {     guard let sourceData else {       return ""     }           var request = URLRequest(url: URL(string: sourceData.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)!)     request.httpMethod = "get"           let (data, _) = try await URLSession(configuration: .ephemeral).data(for: request)     return useWindowsChar1252 ?       String(data: data, encoding: .windowsCP1252)! :       String(decoding: data, as: UTF8.self)   } You can just have one tab item in the TabView and the http request keeps on looping. any idea? struct MyTabView: View {       var body: some View { TabView { Test()         .tabItem {           Label("test", systemImage: "leaf")         } } } } Trying to understand the cause. Because If I put the AsyncView, say inside a ZStack, it works ok. Thoughts?
0
0
426
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
Suggestion How To Avoid Rebuilding View When Using EnvironmentObject
Hi, i wish to ask for suggestions. I am in a dilemna using an environment object. Please see code. struct ContentView: View {       @State var show = false   @StateObject var appData = AppData()   @StateObject var toolbarData = ToolbarData()   var body: some View {     NavigationStack(path: $appData.path) {       ZStack(alignment: .leading) {         VStack(spacing: 0) {           HStack {             Button(action: {               withAnimation(.default) {                 show.toggle()               }             }) {               Image(systemName: "line.3.horizontal")             }             Text("Home")             Spacer()             ToolbarView()             OverflowMenu()               .padding([.leading], Default.instance.PADDING_BETWEEN_MENU_ITEM)           }           .padding()           .foregroundColor(.primary)           .overlay(Rectangle().stroke(Color.primary.opacity(0.1), lineWidth: 1).shadow(radius: 3).edgesIgnoringSafeArea(.top))           MainContent(navigationManager: navigationManager)             .frame(maxWidth: .infinity, maxHeight: .infinity)         }          .environmentObject(toolbarData)         .background(Color.primary.opacity(self.show ? (self.dark ? 0.05 : 0.2) : 0))       }       .navigationBarHidden(true)     }     .navigationViewStyle(.stack)     .environmentObject(appData)   } } struct ContentView_Previews: PreviewProvider {   static var previews: some View {     ContentView()   } } struct ToolbarView: View {       @Binding var toolbarData: ToolbarData       var body: some View {     HStack {       if let item = toolbarData.toolbarItem, item.visible {         Button {           item.action()         } label: {           if let iconLink = item.iconLink {             NavigationLink(destination: iconLink) {               Image(systemName: item.icon)             }           }           else {             Image(systemName: item.icon)           }         }         .disabled(item.disabled)         .opacity(item.opacity)         .padding([.leading, .trailing], Default.instance.PADDING_BETWEEN_MENU_ITEM)       }   } } final class ToolbarData: ObservableObject {   @Published var toolbarItem: ToolbarItemProperties? } final class ToolbarItemProperties {   var disabled = false } In the MainContent , my code runs this toolbarData.toolbarItemProperties.disabled = false I do not want MainContent to be rebuilt. Inside MainContent, I modify toolbarItemProperties (from an environment object). And i call objectWillChange.send(). Problem is, it will rebuild the ContentView widget including MainContent. What can be the correct approach to having to eb able to change the value of toolbarItemProperties and rebuild it but ignoring MainContent to rebuild again. Thoughts?
0
0
459
Dec ’22