Xcode 11 & iOS13, using UIKIT can't change background colour of UIViewController
and commented the code present in xxx scene delegate to not have the UIKit part
You mustn't do that. It is your code that needs to go in the right place. If you make a new project in Xcode 11, this code does nothing:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow()
window?.makeKeyAndVisible()
let controller = MainVC()
window?.rootViewController = controller
return true
}
The code runs, but the window property is not your app's window, so what you're doing is pointless. The window now belongs to the scene delegate. That is where you need to create the window and set its root view controller.
func scene(_ scene: UIScene,
willConnectTo session: UISceneSession,
options connectionOptions: UIScene.ConnectionOptions) {
if let windowScene = scene as? UIWindowScene {
self.window = UIWindow(windowScene: windowScene)
let vc = MainVC()
self.window!.rootViewController = vc
self.window!.makeKeyAndVisible()
}
}
Changing the Status Bar Color for specific ViewControllers using Swift in iOS8
After reading all the suggestions, and trying out a few things, I could get this to work for specific viewcontrollers using the following steps :
First Step:
Open your info.plist and insert a new key named "View controller-based status bar appearance" to NO
Second Step (Just an explanation, no need to implement this):
Normally we put the following code in the application(_:didFinishLaunchingWithOptions:)
method of the AppDelegate,
Swift 2
UIApplication.sharedApplication().statusBarStyle = .LightContent
Swift 3
UIApplication.shared.statusBarStyle = .lightContent
but that affects the statusBarStyle
of all the ViewControllers.
So, how to get this working for specific ViewControllers - Final Step:
Open the viewcontroller file where you want to change the statusBarStyle
and put the following code in viewWillAppear()
,
Swift 2
UIApplication.sharedApplication().statusBarStyle = .LightContent
Swift 3
UIApplication.shared.statusBarStyle = .lightContent
Also, implement the viewWillDisappear()
method for that specific viewController and put the following lines of code,
Swift 2
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.Default
}
Swift 3
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
UIApplication.shared.statusBarStyle = UIStatusBarStyle.default
}
This step will first change the statusBarStyle
for the specific viewcontroller and then change it back to default
when the specific viewcontroller disappears. Not implementing the viewWillDisappear()
will change the statusBarStyle
permanently to the new defined value of UIStatusBarStyle.LightContent
In iOS13 the status bar background colour is different from the navigation bar in large text mode
No hacks or funkiness required here. The key is defining the desired appearance and setting this value on BOTH the nav bar's standardAppearance
AND its scrollEdgeAppearance
. I have the following in the init for my base navigation controller subclass for my entire app:
if #available(iOS 13.0, *) {
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.configureWithOpaqueBackground()
navBarAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
navBarAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
navBarAppearance.backgroundColor = <insert your color here>
navigationBar.standardAppearance = navBarAppearance
navigationBar.scrollEdgeAppearance = navBarAppearance
}
Changing navigation bar color in Swift
Navigation Bar:
navigationController?.navigationBar.barTintColor = UIColor.green
Replace greenColor with whatever UIColor you want, you can use an RGB too if you prefer.
Navigation Bar Text:
navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.orange]
Replace orangeColor with whatever color you like.
Tab Bar:
tabBarController?.tabBar.barTintColor = UIColor.brown
Tab Bar Text:
tabBarController?.tabBar.tintColor = UIColor.yellow
On the last two, replace brownColor and yellowColor with the color of your choice.
How do I prevent iOS 13's Dark Mode from changing the text color in my app's status bar?
iOS 13 Solution(s)
UINavigationController
is a subclass of UIViewController
! (who knew )
Therefore, when presenting view controllers embedded in navigation controllers, you're not really presenting the embedded view controllers; you're presenting the navigation controllers! UINavigationController
, as a subclass of UIViewController
, inherits preferredStatusBarStyle
and childForStatusBarStyle
, which you can set as desired.
Any of the following methods should work:
Opt out of Dark Mode entirely
- In your
info.plist
, add the following property:- Key -
UIUserInterfaceStyle
(aka. "User Interface Style") - Value - Light
- Key -
- In your
Override
preferredStatusBarStyle
withinUINavigationController
preferredStatusBarStyle
(doc) - The preferred status bar style for the view controllerSubclass
or extendUINavigationController
class MyNavigationController: UINavigationController {
override var preferredStatusBarStyle: UIStatusBarStyle {
.lightContent
}
}ORextension UINavigationController {
open override var preferredStatusBarStyle: UIStatusBarStyle {
.lightContent
}
}
Override
childForStatusBarStyle
withinUINavigationController
childForStatusBarStyle
(doc) - Called when the system needs the view controller to use for determining status bar style- According to Apple's documentation,
"If your container view controller derives its status bar style from one of its child view controllers, [override this property] and return that child view controller. If you return nil or do not override this method, the status bar style for self is used. If the return value from this method changes, call the setNeedsStatusBarAppearanceUpdate() method."
In other words, if you don't implement solution 3 here, the system will fall back to solution 2 above.
Subclass
or extendUINavigationController
class MyNavigationController: UINavigationController {
override var childForStatusBarStyle: UIViewController? {
topViewController
}
}ORextension UINavigationController {
open override var childForStatusBarStyle: UIViewController? {
topViewController
}
}You can return any view controller you'd like above. I recommend one of the following:
topViewController
(ofUINavigationController
) (doc) - The view controller at the top of the navigation stackvisibleViewController
(ofUINavigationController
) (doc) - The view controller associated with the currently visible view in the navigation interface (hint: this can include "a view controller that was presented modally on top of the navigation controller itself")
Note: If you decide to subclass UINavigationController
, remember to apply that class to your nav controllers through the identity inspector in IB.
Edits: Strikethrough edits were made to remove extensions as a suggested answer. Other developers noted that they stopped working in Xcode 11.4 and Apple's documentation discourages the use of this ambiguous behavior.
P.S. My code uses Swift 5.1 syntax /p>
iOS: Modal ViewController with transparent background
This following code only works on the iPad.
self.view.backgroundColor = [UIColor clearColor];
self.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentModalViewController:modalVC animated:YES];
I would go with adding a sub view.
Here is a very good discussion. Look at the comments specifically. Not only the answer.
Modal View
If I were you I wouldn't do it. I would add a sub view and do it. It seems to give me a better control over things.
EDIT:
As mentioned by Paul Linsay, since iOS 8 all that's needed is UIModalPresentationOverFullScreen
for the modalPresentationStyle of the ViewController being presented. This would also cover of navigationBar and tabBar buttons.
new ViewController is not displaying when pushed in AppDelegate
Put it in
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let welcome = mainStoryboardIpad.instantiateViewController(withIdentifier: "Welcome") as! WelcomeScreen
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = welcome
self.window?.makeKeyAndVisible()
return true
}
UPDATE:
If it is not working try:
if let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main){
if let welcome = mainStoryboardIpad.instantiateViewController(withIdentifier: "Welcome") as! WelcomeScreen{
//Does it come here?
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = welcome
self.window?.makeKeyAndVisible()
}
}
Make sure you actually get in to the body if most inner if
which is if let welcome = mainStoryboardIpad.instantiateViewController(withIdentifier: "Welcome") as! WelcomeScreen
How to change background color of the text field in the UISearchController?
Here is a an example on how to set the textField
background.
class ViewController: UIViewController {
let searchController = UISearchController(searchResultsController: nil)
private lazy var searchTextField: UITextField? = { [unowned self] in
var textField: UITextField?
self.searchController.searchBar.subviews.forEach({ view in
view.subviews.forEach({ view in
if let view = view as? UITextField {
textField = view
}
})
})
return textField
}()
override func viewDidLoad() {
super.viewDidLoad()
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search Candies"
navigationItem.searchController = searchController
definesPresentationContext = true
if let bg = self.searchTextField?.subviews.first {
bg.backgroundColor = .green
bg.layer.cornerRadius = 10
bg.clipsToBounds = true
}
}
}
Result
Related Topics
What Is an Optional Value in Swift
How to Run a Terminal Command in a Swift Script? (E.G. Xcodebuild)
Is Key-Value Observation (Kvo) Available in Swift
How to Create a Global Variable
How to Hash Nsstring With Sha1 in Swift
Can Swift Convert a Class/Struct Data into Dictionary
Swift - Extra Argument in Call
What Is Difference Between Optional and Decodeifpresent When Using Decodable For Json Parsing
Swift - Validating Uitextfield
Get Nth Character of a String in Swift Programming Language
How to Cast Self to Unsafemutablepointer≪Void≫ Type in Swift
Swift 2 - Unsafemutablepointer≪Void≫ to Object
Swift Constants: Struct or Enum
Can You Execute an Applescript Script from a Swift Application
Changing Navigation Title Programmatically
Why Is 'Nil' Not Compatible With 'Unsafepointer≪Cgaffinetransform≫' in Swift 3