简体   繁体   English

从暗模式切换到亮模式时如何修复搜索栏 swift

[英]How to fix searchbar when switch from dark mode to light mode swift

Hi I have a problem with searchbar.嗨,我的搜索栏有问题。 I use this function to switch my app between dark and light mode我使用这个 function 在暗模式和亮模式之间切换我的应用程序

func changeStyle(dark: Bool){
    let window = UIApplication.shared.windows.first { $0.isKeyWindow }
      if dark == true {
          window?.overrideUserInterfaceStyle = .dark 
      }else{
          window?.overrideUserInterfaceStyle = .light
      }
}

All works fine but only search bar have a problem.一切正常,但只有搜索栏有问题。 Searchbar remains in dark mode when I switch to light mode.当我切换到浅色模式时,搜索栏仍处于深色模式。 See image below How can I fix it?见下图 我该如何解决?

应用切换到灯光模式

Try listening for the change and set manually using traitCollectionDidChange :尝试使用traitCollectionDidChange监听更改并手动设置:

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
    super.traitCollectionDidChange(previousTraitCollection)

    if #available(iOS 13.0, *) {
        if traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection) {
        //Set colours here
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM