extension for UIViewController is not Working change UIStatusBarStyle In Swift - swift

I am Try to Create extension for UIViewController but Its not working . i am changing status bar color for using extension but i cant get success. But without extension is working. I am new in Extension Please help me.
My Aim is I DONT WANT TO CREATE FUNCTION ON EVERY VIEW CONTROLLER , REDUSE REUSABILITY
Create Extension Is Not Working
extension UIViewController {
func preferredStatusBarStyle_change() -> UIStatusBarStyle {
return UIStatusBarStyle.LightContent
}
}
//!!!!!!!!!view1
override func viewDidLoad() {
super.viewDidLoad()
//preferredStatusBarStyle();
self.preferredStatusBarStyle_change();
}
//!!!!!!!!!view2
override func viewDidLoad() {
super.viewDidLoad()
//preferredStatusBarStyle();
self.preferredStatusBarStyle_change();
}
Without Extension IS Working
View1
override func viewDidLoad() {
super.viewDidLoad()
preferredStatusBarStyle();
}
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return UIStatusBarStyle.LightContent
}
View2
override func viewDidLoad() {
super.viewDidLoad()
preferredStatusBarStyle();
}
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return UIStatusBarStyle.LightContent
}
Help me Try to understanding Reusability and extension

It not working because:
Viewcontroller will call preferredStatusBarStyle() for define about statusbar. Actually you remove preferredStatusBarStyle(); in viewDidload, your controller still work.
So in your case you can resolve like this:
You create a subclass of UIViewController maybe name it is: BaseViewController and it this class you put:
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return UIStatusBarStyle.LightContent
}
And now all your ViewController you inherited from BaseViewController status bar will always .LightContent you don't need put this code above every where.
Demo: Demo

Related

What is the correct way to manage statusBarStyle in Swift?

I've tried adding key UIViewControllerBasedStatusBarAppearance to true inside info.plist file and then added the below code inside UINavigationController class which holds several UIViewController classes.
class HomeNavigationController: UINavigationController {
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}
But, it did not work.
I've also tried setting the barStyle property of navigationBar to .black but that too didn't work either.
Also looked up to https://stackoverflow.com/a/58203998/9180494, but that did not help as well.
Please NOTE: For UIViewController classes not embedded inside any UINavigationController , if I use computed property preferredStatusBarStyle, then it works.
Try in viewDidAppear() of UINavigationController class:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
navigationController?.navigationBar.barStyle = .black
}
Also add (in the same class as above):
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
#Anuranjan Bose Try this on your view did load,
override func viewDidLoad() {
super.viewDidLoad()
setNeedsStatusBarAppearanceUpdate()
}

Why is windowDidMove() not being called? [duplicate]

I'm trying to know when a window closes, I implemented this code:
class ViewController: NSViewController, NSWindowDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let window: NSWindow? = view.window
window?.delegate = self
}
func windowWillClose(_ aNotification: Notification) {
print("windowWillClose")
}
}
Unfortunately nothing happens, what could I made wrong?
Documents: https://developer.apple.com/documentation/appkit/nswindow/1419400-willclosenotification
PS
I already read this question without to find a solution: Handle close event of the window in Swift
The problem there is that the window property will always return nil inside viewDidLoadMethod. You need to set the delegate inside viewWillAppear method:
class ViewController: NSViewController, NSWindowDelegate {
override func viewWillAppear() {
super.viewWillAppear()
view.window?.delegate = self
}
func windowWillClose(_ aNotification: Notification) {
print("windowWillClose")
}
}

Trying to know when a window closes in a macOS Document based application

I'm trying to know when a window closes, I implemented this code:
class ViewController: NSViewController, NSWindowDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let window: NSWindow? = view.window
window?.delegate = self
}
func windowWillClose(_ aNotification: Notification) {
print("windowWillClose")
}
}
Unfortunately nothing happens, what could I made wrong?
Documents: https://developer.apple.com/documentation/appkit/nswindow/1419400-willclosenotification
PS
I already read this question without to find a solution: Handle close event of the window in Swift
The problem there is that the window property will always return nil inside viewDidLoadMethod. You need to set the delegate inside viewWillAppear method:
class ViewController: NSViewController, NSWindowDelegate {
override func viewWillAppear() {
super.viewWillAppear()
view.window?.delegate = self
}
func windowWillClose(_ aNotification: Notification) {
print("windowWillClose")
}
}

Opening a side menu on click on a TAB bar in SWIFT

I want to make something like below snapshots.
When I click on profile tab bar instead of opening a new view controller it shows a side menu. Is it something that has been handled on click of tabbar ?
if you want to achieve something like your screenShot then you are using a wrong library, because when you show your right viewController the front viewController go to left by amount of width of your right viewController, but anyways here is the code for what you need to do
first you need to put your viewController as delegate of your TabBarViewController and in func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool you need to return false and call the method of SWRevealViewController to show right viewController rightRevealToggleAnimated(true)
class FirstViewController: UIViewController,SWRevealViewControllerDelegate,UITabBarControllerDelegate {
#IBOutlet weak var sliderControl: UISlider!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.revealViewController().delegate = self
self.tabBarController?.delegate = self
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
//self.view.removeGestureRecognizer(self.revealViewController().panGestureRecognizer())
//self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {
//checking for specific viewController
if(viewController is DesiredViewControllerClass) {
self.revealViewController().rightRevealToggleAnimated(true)
}
return false
}
}
I hope this helps you, regards
You can use the tab bar delegate:
extension ViewController: UITabBarDelegate {
func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
// Present hamburger menu
}
}

How to put override func before viewDidLoad()?

Open ViewController.swift for editing, and before viewDidLoad() put the code below the question into the default code of ViewController.Swift?
How do I put these 4 lines of code below using the directions above into the default code for ViewController.Swift?
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
Default code ViewController.Swift:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
I'm guessing you have a storyboard in which ViewController is defined. If so, you must not override loadView():
If you use Interface Builder to create your views and initialize the view controller, you must not override this method.
(from UIViewController class reference)
Instead edit the class of the view in the storyboard and set it to WKWebView. To get the webView reference, create it as an outlet:
#IBOutlet weak var webView: WKWebView!
And link it as a referencing outlet to the view in the storyboard.
(Or you could just put the WKWebView inside the default UIView; would be simpler. You could do this either programmatically in viewDidLoad() or via the storyboard.)
Or if you don't have a storyboard, then just copypaste the code into the editor inside the class but outside the existing functions?
import UIKit
class ViewController: UIViewController {
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
However, it matters not where the func goes in the class:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
}