Setting up a ViewController via url and show it as a popup - ios

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.

Related

Navigation Bar disappears when going back to Home VC from a Details VC from Push Notification

If I click on the push notification, it takes me to the details page but when I click on the programatically created back button, it takes me back to HomeVC but with the top nav bar gone. Here is my app delegate function where I present the details VC :
private func gotoDetailsVC(value : String)
{
guard let window = UIApplication.shared.connectedScenes.filter({$0.activationState == .foregroundActive
|| $0.activationState == .background || $0.activationState == .foregroundInactive}).compactMap({$0 as? UIWindowScene}).first?.windows.filter({$0.isKeyWindow}).first
else { return }
let storyboard = UIStoryboard(name: "Home", bundle: nil)
let detailsVC = storyboard.instantiateViewController(identifier: "viewDeviceDataController") as viewDeviceDataController
detailsVC.deviceID = value
detailsVC.didComeFromPN = true
let navController = UINavigationController(rootViewController: detailsVC)
navController.modalPresentationStyle = .popover
// you can assign your vc directly or push it in navigation stack as follows:
window.rootViewController = navController
window.makeKeyAndVisible()
}
And here is my back button on the DetailsVC :
#objc func backAction() {
let storyboard = UIStoryboard.init(name: "Home", bundle: nil)
let homeVC = storyboard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
homeVC.modalPresentationStyle = .automatic
present(homeVC, animated: true, completion: nil)
}
How can I resolve this issue? Please help.
Resolved by navigating to root Navigation Controller.
#objc func backAction() {
let storyboard = UIStoryboard.init(name: "Home", bundle: nil)
let homeVC = storyboard.instantiateViewController(withIdentifier: "navControllerHome") as! UINavigationController
homeVC.modalPresentationStyle = .fullScreen
present(homeVC, animated: true, completion: nil)
}
Usually back action would just return to a previous state, but here it seems you are recreating the view.
I would not make private func gotoDetailsVC(value : String) replace rootViewController and instead make it be presented by current navigationController then on your back action you would not create a new home but dismiss your detail screen.

Present view controller not working on real device

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 )

Presenting NavigationController with TabBarViewController can not select index programmatically

I am trying to present NavigationController and select second tab in the completion block. My code is:
let chatViewController = UITabBarController()
let navigationController = UINavigationController(rootViewController: chatViewController)
present(navigationController, animated: true, completion: {
self.visibleViewController = navigationController
self.visibleViewController?.tabBarController?.selectedIndex = 1
})
Second attempt:
let chatViewController = UITabBarController()
let navigationController = UINavigationController(rootViewController: chatViewController)
navigationController.tabBarController?.selectedIndex = 1
present(navigationController, animated: true, completion: {
self.visibleViewController = navigationController
})
In both cases tabBarController is nil. How can I switch to different tab?
You are trying to access UITabBarController of UINavigationController, but you need to access the very first controller from UINavigationController and from there you need to make your UITabBar selected like this way:
func showTabBarControllerr() {
let chatViewController = UITabBarController()
//Note: Make sure you have already added the Controllers in Tabbarcontroller
chatViewController.viewControllers = [DemoOne(), DemoTwo()]
let navigationController = UINavigationController(rootViewController: chatViewController)
present(navigationController, animated: true, completion: {
if let tabBarController = navigationController.viewControllers.first as? UITabBarController {
tabBarController.selectedIndex = 1
}
})
}
Let me know this helps or not!
Instead of calling
present(navigationController, animated: true, completion: { })
in viewDidLoad, try to call it in viewWillAppear or viewDidAppear
Try this (Swift 5):
Set an identifier to your UITabBarController in the Storyboard, something like MainTabBar
Into a function or IBAction, use this code:
let tbc = storyboard?.instantiateViewController(withIdentifier: "MainTabBar") as! UITabBarController
tbc.selectedIndex = 1 // This is the index of your controller
present(tbc, animated: false, completion: nil)

Back to root view controller in swift

Is there any way to dismiss all the view controllers ( in which some controllers are pushed through navigation) and go back to the root view controller. I saw lots of examples but they didn't work in my application. I am using swift 4
This is code in appdelegate
func setNavigationToRootViews(){
storyBoard = UIStoryboard(name: "Main", bundle: nil)
nav = storyBoard?.instantiateViewController(withIdentifier: "mainNavigation") as! UINavigationController?
let accessToken: String? = KeychainWrapper.standard.string(forKey: "token")
print(accessToken as Any)
if accessToken != nil {
let homeVc = storyBoard?.instantiateViewController(withIdentifier: "Home-VC") as! HomeViewController
nav?.pushViewController(homeVc, animated: false)
}else{
let welcomVc = storyBoard?.instantiateViewController(withIdentifier: "login-VC") as! LoginViewController
nav?.pushViewController(welcomVc, animated: false)
}
let leftMenuVC = storyBoard?.instantiateViewController(withIdentifier: "menu-VC") as! MenuViewController
container = MFSideMenuContainerViewController.container(withCenter: nav, leftMenuViewController: leftMenuVC, rightMenuViewController: nil)
container?.panMode = MFSideMenuPanModeNone
window?.rootViewController = container
window?.makeKeyAndVisible()
}
and this in my last View controller
#IBAction func okayBtnTapped(_ sender: Any) {
_ = self.navigationController?.popToRootViewController(animated:
true)
dismiss(animated: true, completion: nil)
}
Try this one:
self.navigationController?.popToRootViewController(animated: true)
You can just set a new RootController like this:
let storyboard = UIStoryboard(name: "sName", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "<YOUR ROOT CONTROLLER>")
self.window?.rootViewController = viewController
If you don't have a window at self.window you need to instanciate one based on your AppDelegate.
If you're within a NavigationController you can also use the answer of #Anshul Bhatheja
Check you navigation controller, and view controllers inside that navigation controller. It seems debugging issue.
this method popToRootViewController(animated:) is the method should work
If you are working with simple push navigation controller and you want to go back to root view controller then you simply have to write
_ = self.navigationController?.popToRootViewController(animated: true)
extension UINavigationController {
func popToViewController(ofClass: AnyClass, animated: Bool = true) {
if let vc = viewControllers.filter({$0.isKind(of: ofClass)}).last {
popToViewController(vc, animated: animated)
}
}
func popViewControllers(viewsToPop: Int, animated: Bool = true) {
if viewControllers.count > viewsToPop {
let vc = viewControllers[viewControllers.count - viewsToPop - 1]
popToViewController(vc, animated: animated)
}
}
}
pop to SomeViewController class
navigationController?.popToViewController(ofClass: SomeViewController.self)
pop 2 view controllers
navigationController?.popViewControllers(viewsToPop: 2)

Show BlurEffect on manual segue

I have a question regarding blureffects on a view controller. I program all of my segues manually for several reasons. But when I go to a viewcontroller that has a blureffect background, I only see a dark gray background. How can I solve this that the view controller blur effect lays on top of another viewcontroller where the content of the previous viewcontroller can be seen through?
The blureffects are now applied by storyboard with transparant background views.
My code for seque is:
Action:
#IBAction func ToUserSections(_ sender: Any) {
let controller =
Navigation.getDestinationViewControllerWith("User",
controllerName: "UserSectionsVC")
present(controller, animated: false, completion: nil)
}
Class:
class Navigation{
static func getDestinationViewControllerWith(_
storyboardName:String, controllerName:String)-> UIViewController{
let storyboard = UIStoryboard(name: storyboardName, bundle: nil)
let controller =
storyboard.instantiateViewController(withIdentifier:
controllerName) as UIViewController
return controller
}
}
Hope you can help me with this because it will maximize aesthetics for the app :D
try this.
let viewController : CustomViewControlller = UIStoryboard.getMainStoryboard().instantiateViewController(withIdentifier: "Identifier") as! CustomViewControlller
viewController.modalPresentationStyle = .overCurrentContext
viewController.modalTransitionStyle = .crossDissolve
self.present(viewController, animated: true, completion: { _ in })
Alright for others to know, what worked for me was the following:
At IBAction: adding the following line before presenting
controller.modalPresentationStyle = UIModalPresentationStyle.overFullScreen
Like this:
#IBAction func ToUserSections(_ sender: Any) {
let controller = Navigation.getDestinationViewControllerWith("User", controllerName: "UserSectionsVC")
controller.modalPresentationStyle = UIModalPresentationStyle.overFullScreen
present(controller, animated: false, completion: nil)
}

Resources