Following is my code present safari view controller
if let url = URL(string: "https://www.ggogle.com.com/") {
let config = SFSafariViewController.Configuration()
config.entersReaderIfAvailable = true
let vc = SFSafariViewController(url: url, configuration: config)
present(vc, animated: false)
}
Above code calls viewWillDisappear but does not call viewWillAppear when presented view controller is dismissed.
Also found that after view is dismissed viewWillDisappear is not called at all for any other pushViewController
How to fix this?
Update on this:
Tried navigation controller and set its delegate and implemented following methods
navigationController:willShowViewController:animated:
navigationController:didShowViewController:animated:
This is not working as well.
Update on this tried following code
vc.modalPresentationStyle = .currentContext
But using this tab bar does not hides and controller not appearing full screen and there is no option like hide bottom bar on present.
Please try this
if let url = URL(string: "https://www.google.com/") {
let config = SFSafariViewController.Configuration()
config.entersReaderIfAvailable = true
let vc = SFSafariViewController(url: url, configuration: config)
vc.modalPresentationStyle = .currentContext
present(vc, animated: false)
}
UPDATE
vc.modalPresentationStyle = .currentContext
tabBarController?.present(viewController, animated: false, completion: nil)
Related
I currently have a method that I use to setup a new ViewController
func setRootView(viewController vc: UIViewController){
UIApplication.shared.windows.first?.rootViewController = vc
UIApplication.shared.windows.first?.makeKeyAndVisible()
}
How can I modify the presentation that the ViewController will show over the other screen like presentation style not modally ?
That is the function that gets called after the button is pressed and the view should change:
func moveToNextVC(isLogin: Bool){
if self.checkNetworkConnection(){
let vc = HomeVC(nibName: "HomeVC", bundle: nil)
if isLogin{
vc.modalPresentationStyle = .automatic
vc.url = loginLink
}else{
vc.modalPresentationStyle = .automatic
vc.url = signupLink
}
setRootView(viewController: vc)
}
else{
showAlert("Alert!", message: "Please check your internet connection, then try again.")
}
}
Per Shawn Frank's comment:
Instead of doing this UIApplication.shared.windows.first?.rootViewController = vc, I would do present(vc, animated: true)
This solved the problem.
If I present a ViewController like so:
let authViewController = authUI!.authViewController()
authViewController.modalPresentationStyle = .overCurrentContext
self.present(authViewController, animated: true, completion: nil)
I would like to know when the ViewController has been dismissed. I have tried the following:
let authViewController = authUI!.authViewController()
authViewController.modalPresentationStyle = .overCurrentContext
self.present(authViewController, animated: true, completion: {
print("View Dismissed")
})
but that only lets me know if the view was presented successfully. This ViewController was not created by me so I can't change the viewWillDissapear method.
Whole answer is predicated on an assumption that OP doesnt have access to authViewController code
If you dont have access to authViewController code, lousy solution would be to use viewWillAppear of your view controller to find when auth view controller is dismissed.
Basically when you present/push any viewController over your existing view controller, your view controller's viewWillDisappear will be called similarly when presented/pushed view controller is dismissed, or popped out viewWillAppear will be called.
Because viewWillAppear might get called for other reasons as well and you wouldnt wanna confuse it as authViewController dismiss, use a boolean
private var shouldMonitorAuthViewControllerDismiss = false //declared a instance property
Set the boolean to true when you actually present the authViewController
let authViewController = authUI!.authViewController()
authViewController.modalPresentationStyle = .overCurrentContext
self.present(authViewController, animated: true, completion: {
shouldMonitorAuthViewControllerDismiss = true
})
Finally in your viewWillAppear
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
if shouldMonitorAuthViewControllerDismiss {
//auth view controller is dismissed
}
shouldMonitorAuthViewControllerDismiss = false
}
I have an application that will direct user to another viewController once it is logged in.
DispatchQueue.main.async {
let controller = HomeViewController()
controller.isLoggedIn = self.loggedIn
controller.userRole = self.userRole
controller.username = self.username
let navigationController = UINavigationController(rootViewController: controller)
if #available(iOS 13.0, *) {
navigationController.isModalInPresentation = true
navigationController.modalPresentationStyle = .overFullScreen
} else {
// Fallback on earlier versions
}
print("should present here")
self.present(navigationController, animated: true)
}
Above is the "redirecting" part. This code works well in the simulator, but on the real device it does not work. Any idea how I can solve it? Thanks!
Try this also Set home screen identifier in storyboard "HomeViewController"
In code:
let controller = storyboard?.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
Using .fullScreen as modalPresentationStyle should fix your issue and gives the default style used on iOS 12 or lower.
if #available(iOS 13.0, *) {
navigationController.modalPresentationStyle = .fullScreen
} else {
// Fallback on earlier versions
}
You need to use storyBoard reference for presenting a viewController. Which you can do by using example of the below attached code:-
let VC1 = self.storyboard.instantiateViewController(withIdentifier:"MyViewController") as! ViewController
`let navController = UINavigationController(rootViewController: VC1)` // Creating a navigation controller with VC1 at the root of the navigation stack.
self.present(navController, animated:true, completion: nil)
thanks for the response but I fixed it by
self.dismiss(animated: true) {
self.present(navigationController, animated: true)
}
apparently it didnt work as there was a error saying there was another view controller being presented ( i missed this error previously )
I am trying to present a view that is showd from the bottom to the top. This is my code
let myPageViewController = storyboard?.instantiateViewController(withIdentifier: "myPageViewControllerID") as! MyPageViewController
myPageViewController.modalPresentationStyle = .fullScreen
let navController = UINavigationController(rootViewController: myPageViewController)
navigationController?.present(navController, animated: true, completion: nil)
Despite I am using .fullScreen, the view is not presented on full screen.
I tried using peformSegue to show the view with this code
self.performSegue(withIdentifier: "myPageViewSegue", sender: nil)
the page is presented on fullscreen but from the left to the right and not from the bottom to the top.
The third code I tried is this one
let detailVC = MyPageViewController()
let navigationController = UINavigationController(rootViewController: detailVC)
navigationController.modalPresentationStyle = .fullScreen
present(detailVC, animated: true)
here I get an error Application tried to present modally an active controller. I tried to add self.dismiss when disappearing MyPageViewController but it didn't helped.
The problem maybe is you are not presenting navigationController you are presenting detail vc, Use this
'let detailVC = MyPageViewController()
let navigationController = UINavigationController(rootViewController: detailVC)
navigationController.modalPresentationStyle = .fullScreen
present(navigationController, animated: true)'
if problem still exists use
navigationController.modalPresentationStyle = .overCurrentContext
Try this one:
let detailVC = MyPageViewController()
detailVC.modalPresentationStyle = .fullScreen
present(detailVC, animated: true)
You could try this, worked for me:
Setup up your segue:
performSegue(withIdentifier: "myPageViewSegue", sender: self)
Then use the prepare for segue method:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destVC = segue.destination as! MyPageViewSegue
destVC.modalPresentationStyle = .fullScreen
}
I ran into this problem and was stumped. Then I realized that overriding dismiss also overrides .fullscreen
// This breaks .fullscreen
override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
super.dismiss(animated: flag, completion: completion)
// do something
}
It's clear that you don't need this if .fullscreen is being used, but leaving it in place caused problems.
You need to change your code with below lines and then Booom work like a charm
let vc = MyPageViewController()
vc.modalPresentationStyle = .fullScreen
present(vc, animated: true)
let detailVC = MyPageViewController()
let navigationController = UINavigationController(rootViewController: detailVC)
navigationController.modalPresentationStyle = .overCurrentContext
present(detailVC, animated: true, completion: nil)
Explanation:
Its very simple. Its better to explain it line by line. Line 1: Declare the viewcontroller. Line 2: Add Navigation Controller (its optional. If you don't need, then you may skip navigation bar). Line 3: We have defined the modal presentation style link as overCurrentContext. and present the navigationController (it will come up with navigation bar and viewcontroller) or only the viewcontroller as per your need. In this piece of code, you will get the viewcontroller without navigation bar.
I am setting up the side menu by following SideMenu
When the app is loaded first the side menu controller is working fine (.menuslide)
but when I come back to the same controller from the side menu to the same view controller or any other controller or any other page it is not following (.menuslide)
I have tried by assigning the navigation
let menu = SideMenuNavigationController(rootViewController: YourViewController)
let rightMenuNavigationController = SideMenuNavigationController(rootViewController: YourViewController)
SideMenuManager.default.rightMenuNavigationController = rightMenuNavigationController
present(menu, animated: true, completion: nil)
through code and also
From the story board
I am navigating from every controller to side menu like this
let navigationController = storyboard!.instantiateViewController(withIdentifier: "sidemenunavigation") as! SideMenuNavigationController present(navigationController, animated: true, completion: nil)
for the first time after loading it is navigating properly (.menuslide)
but after navigating to other controller and get the side menu it is not following (.menuslide)
you may use the following RESideMenu for side menu quite easy to use
RESideMenu for side menu
please check this code is working fine for navigation another view controller for sidemenu
let storyBoard = UIStoryboard(name:"Main", bundle: nil)
if let conVC = storyBoard.instantiateViewController(withIdentifier: "RoutingAddViewController") as? RoutingAddViewController,
let navController = UIApplication.shared.keyWindow?.rootViewController as? UINavigationController {
navController.pushViewController(conVC, animated: true)
}