Force ViewController light mode but keep WKWebView mode adaptable - ios

I'm using the newest version of Xcode for my iOS app.
I'm stuck with one small task since hours:
I want to force my View Controller to always stay in light mode, also if device is in dark mode. I'm using this code:
overrideUserInterfaceStyle = .light
This is working very well.
But there's a WKWebView in the View Controller. I want this WKWebView to stay adaptable to the color mode, so I can check via CSS if the device is in light mode or dark mode.
I tried so much but nothing worked.
Is it even possible? How?

I suggest changing UIInterfaceStyle just right before invoking WKWebView and setting it back before WKWebView dismisses. You can use WKNavigationDelegate for detecting WKWebView dismissal.

Related

Is it possible to overwrite the value of UIUserInterfaceStyle at any given point?

I'm working on a react-native app on which I want to detect how many people have dark mode and how many people have light mode but I want to keep light mode as UIUserInterfaceStyle.
So since I'm just detecting I tried a couple of solutions but the above problem statement is not being satisfied.
The project was created using CLI and not expo.
I tried using useColorScheme
import { StyleSheet, Text, Appearance } from 'react-native';
// ... other code and imports
<Text>Mode is: {Appearance.getColorScheme()}</Text>
//... other code
The above works as it'll give me light or dark as expected. But if I set UIUserInterfaceStyle to Light in Info.plist it always gives light and I'm unable to detect if user's phone is in dark mode or light.
I found that there is [overrideUserInterfaceStyle][1] which is provided for iOS by Apple so it's swift based.
Basically, I know the ways around which means to create two themes, detect which mode is on, and launch the light theme always. I'm looking for a different solution, is it's possible to override the value of UIUserInterfaceStyle from react-native ios?
OR
Is there a way I can change the Appearance of a specific screen? For example, if the user has a dark mode, I detect that in react-native and override the appearance from dark to light for that specific screen.
[1] https://developer.apple.com/documentation/uikit/uiviewcontroller/3238087-overrideuserinterfacestyle

Dark mode don't update instantly

I have implemented dark mode in my iOS app, and while it works, the changes don't occur immediately. I think this has to do with the views not recreating themselves all the time like they do in android.
For instance, it doesn't work to go out from the app, then to settings and change theme, then back in the app again. The changes have only then occurred to Apple-related framework like alertControllers. To get dark mode to work I have to click around to a new view, then go back to the old view again.
I have tried adding dark mode code in the delegate, as I thought this would run every time you reenter your app, but it didn't.
So, any ideas on how to make this work? Is there for instance a delegate that is run every time I reenter the app, or can I have an observer to dark mode changes?
You could implement this method to "listen" to TraitCollection changes:
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
// do whatever you want to do
}
Also, as Gergely mentioned, try to use xcassets for colors and images so they dynamically change depending on the current TraitCollection.
Alert view controllers update because they use responsive system colors for their text and background, which have versions for both light and dark mode by default and update automatically once the mode changes.
You can do the same with custom views if you use color and image assets. this is a nice overview of how to do things: Medium Article.
The official Apple documentation also has a nice overview of how to support dark mode: Supporting Dark Mode in Your Interface.

My IOS orientation acting very strange and won't listen to any programing-code?

As the title describes I am having a big "what the * is this" at my app atm. It seems I can't get the control over the orientation at all in the different slides.
I can only manage the orientation in one way, via the info-plist file. The problem is, info-plist file sets the orientation for the whole app and I am not interested in that.In some slides I want to allow Landscape left/right and others only Portrait and this is not doable vie info-plist?
I have tried my best to understand the problem but I can not say I have gained any bigger "aha moment" so far. I am using UINavigatorbar and Tabbar in my IOS-app which may occur the problem. How can I make the app to start listening to the code in each-file so I can manage the orientation localy ?
Are you using iOS 6? If so, the -shouldAutorotateToInterfaceOrientation method was deprecated.
You now have to override -supportedInterfaceOrientations and -preferredInterfaceOrientationForPresentation methods in order to manage screen orientation. You can do this globally or within individual view controllers.
See the UIViewController class reference for more details.

UIImagePickerController has two 'camera' buttons when started as front-facing

Since iOS6, whenever I show present a UIImagePickerController from inside a UIPopoverController, I am getting two "take picture" buttons:
This only happens when starting out in front-facing mode. If I start with the rear-camera and then switch after the popover appears, it's okay. Likewise, starting in front-facing and switching to rear will keep the second button there.
Even worse, the 'in picture' button doesn't work. It just tries to focus the camera at that point.
Anyone else seeing this or know of a solution? It doesn't happen when presented full screen, and I saw in Apple's docs that the popover is no longer the recommended way to present the image picker, but that it's also not necessarily bad, either. Unfortunately due to some external requirements, I need to keep it in a popover, and can't do full-screen.
Thanks!
I have exactly the same issue on iOS6. Odd behavior is that it appears only the first time I open the image picker. When closed and opened again, "in-picture" button disappears. Looks like a bug in UIImagePickerController.
To overcome this issue, you can hide image controls by setting showsCameraControls property to NO and use custom overlay view with own controls. Disadvantage of this is that you have to provide all controls then and code action handlers for them.
Unfortunately I didn't find a better way so far.

Mutltiple UISplitviewControllers in TabBar are not all notified of rotations events

I have an application with a tabBar containing 3 UISplitiViewControllers. Everything works well except for one scenario:
I have the ipad in landscape position on tab1. I click on tab3, then I rotate the ipad portrait mode. I click on tab1, it is well displayed in portrait mode, I'm happy.
I now come back to landscape position.
I click again on tab3 and there, the splitView controller is still displayed in "portrait mode". the delegate of my splitviewController in tab3 was never called.
Is there a way to force the rotation of this splitViewController in the "viewWillAppear"?
Your might find this git useful.
It is just a simple subclass of UISplitViewController that is notified of rotation changes.
Alternatively, this is a more robust replacement that adds lots of features.
Hope it helps.
BTW I have gotten apps approved using these classes.
After a deep analysis of event bubbling, it appears that rotation events are not forwarded to hidden splitViews...
There is no way to forward these events without using hidden apis.
Lots a developers have the same problem and this implementation of event management in splitView has been reported as a bug to apple, even though guidelines are to use only one splitView added to the root view of application.

Resources