Thursday, February 29, 2024
HomeiOS Developmentios - Change Coloration Scheme of SwiftUI View not working when it's...

ios – Change Coloration Scheme of SwiftUI View not working when it’s inside UIHostingController


Hi All I’ve a easy SwiftUI View like this

struct SettingsSUView: View {
  @State personal var isPresented = false
  @State personal var isDarkMode = true
  var physique: some View {
    Button("Present Sheet") {
      isPresented = true
    }
    .sheet(isPresented: $isPresented) {
      Listing {
        Toggle("Darkish Mode", isOn: $isDarkMode)
      }
      .preferredColorScheme(isDarkMode ? .darkish : .gentle)
    }
  }
}

#Preview {
  SettingsSUView()
}

The colour scheme adjustments like regular when it runs in Preview and run as a standalone SwiftUIView, however when it was put in a UIHostingController coloration scheme stopped altering

    let contentHostingController = UIHostingController(rootView: SettingsSUIView())
    addChild(contentHostingController)
    view.addSubview(contentHostingController.view)
    
    contentHostingController.view.snp.makeConstraints { make in
      make.edges.equalToSuperview()
    }
    
    contentHostingController.didMove(toParent: self)

I’ve tried many issues like utilizing contentHostingController.overrideUserInterfaceStyle = .darkish however most of them can solely be modified in 1 method and never updating the swiftUI View coloration scheme to match with the adjustments of the Toggle

I might actually admire you guys assistance on this. Thanks



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments