I'm working on a project, and I have to control the screen brightness.
I use this to control:
UIScreen.main.brightness = CGFloat(0.80)
But, once I lock the screen and unlock, the screen brightness will change back to the system brightness, which could not go back to the brightness which I set before.
If there is any func I can use to change the screen brightness when users unlock the screen?
Thank you!
There is a method called applicationWillEnterForeground in UIApplicationDelegate which already conforms to your AppDelegate class.
Implement applicationWillEnterForeground by typing name of this method inside your AppDelegate class. like:
you can set the brightness whenever app goes to foreground
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
return true
}
func applicationWillEnterForeground(_ application: UIApplication) {
UIScreen.main.brightness = CGFloat(0.80)
}
}
Related
I'm building an iOS app with the newest version of Xcode.
If I set
overrideUserInterfaceStyle = .light
to a View Controller, it will get set to light mode, also if dark mode is enabled.
But If I put this code in AppDelegate like this:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window?.overrideUserInterfaceStyle = .light
return true
}
it doesn't work.
There are no errors. The app simply is in light mode or dark mode depending on the mode the device is in.
Why doesn't overrideUserInterfaceStyle work in AppDelegate? Is it because I'm using a Tab Bar Controller for my app? I don't think so.
It's too early to call overrideUserInterfaceStyle in AppDelegate, window is not yet created. You can use SceneDelegate willConnectTo function.
I have UIViewController that i created programmatically. This is the code. Even after setting my SignUpViewController background to white. I am getting black screen on launch
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
startWindowLoad()
return true
}
//extension with the starter function
extension AppDelegate {
func startWindowLoad () {
let startView = SignUpViewController()
let navView = UINavigationController()
navView.pushViewController(startView, animated: true )
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = startView
}
}
In a modern app, the app delegate window is not used. You need to use the scene delegate window. Move all the code into the scene delegate.
This is probably because your app is starting with LaunchScreen
Goto Info.plist and
change Launch screen interface file base name to Main
In my AppDelegate, I set the global tint color. How can I be notified when the user enables/disables dark mode in order to reset global tint?
In my AppDelegate, I have:
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate
{
var window: UIWindow? // To conform with UIApplicationDelegate
func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [ UIApplication.LaunchOptionsKey : Any ]? ) -> Bool
{
window?.tintColor = UIColor.someColor
...
}
}
I need to update the global tint color when dark mode is enabled/disabled. How can I detect this change?
Define the color for light and dark appearance in the Asset Catalog
Set the tint color in AppDelegate with the UIColor(named: API. The color will change automatically.
I am using Swift 3 and I researched various methods to set the backgroundColor color of a UITabBar. The most simple method was to change the property in the didFinishLaunchingWithOptions of the AppDelegate. When I tried to do this, the UITabBar launches with the standard color, which is not my intended result.
However, when I go to the next tab, the UITabBar color gets changed to my intended color.
Following this, I tried to subclass my UITabBarController and I tried to set the background colors in both the viewDidLoad and in the viewDidLayoutSubViews. All three of these attempts exhibited the exact same behavior - which was the UITabBar launched with the standard color, and only changed colors when I tabbed to the next tab.
Here is my code:
App Delegate
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?)
-> Bool {
UITabBar.appearance().backgroundColor = UIColor.red
return true
}
viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
self.tabBar.backgroundColor = UIColor.red
}
viewDidLayoutSubviews
override func viewDidLayoutSubviews() {
self.tabBar.backgroundColor = UIColor.red
}
My questions are the following:
1) Can I eliminate this as being a constraint issue? I didn't modify or set any constraints for the UITabBar
2) I have a launch screen where the user selects acknowledges something and then they are segued to the UITabBarController. Could this be part of the issue?
The basic way to change to a tab bar's color is not to set its backgroundColor as you are doing, but rather to set its barTintColor. (For more sophisticated control, use a colored backgroundImage.)
Putting this in my app delegate gets me a tab bar that's tinted red.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
let tabBarVC = UITabBarController()
tabBarVC.viewControllers = [FirstViewController(), SecondViewController()]
tabBarVC.tabBar.backgroundColor = UIColor.red
window?.rootViewController = tabBarVC
window?.makeKeyAndVisible()
return true
}
Somehow XCode starts partially draw non-retina images on both retina iPhone and Simulator. You can see image with standard searchbar below, it is even not my graphics, but as you can see magnifier is pixelated, while text is retina. Some of both my and system graphics are still retina, some not. I'm trying to clean project and restart XCode.
Turns out it's because I initialized my ViewController before application: didFinishLaunchingWithOptions:
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
let viewController = UITableViewController()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// add viewController to view hierarchy
}
}