I am using this pod: https://github.com/jonkykong/SideMenu for my Side bar in my app. I set the side bar navigation through Storyboard. My app design is that I have a main screen, and a side bar with buttons that take me to different screens. The different screens also have the same side bar. My bug is in this situation: I go into the side bar from main screen -> Then I go into screen2 through the side bar -> I click on the side bar from the screen 2 -> Side bar appears from the bottom and takes the whole screen.
For fixing this bug I found a workaround that does NOT satisfy me. I understood I need to dismiss the side bar but then if I want to go into another screen I first see the screen I came from (because I dismiss) and then it only moves to the second screen. I hope this makes sense. I want to achieve switching to the second screen without first seeing the main screen. This is the code that helps me dismiss and then change screens:
dismiss(animated: true, completion: {
goIncident()
})
func goIncident(){
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let window = appDelegate.window
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let rootController = mainStoryboard.instantiateViewController(withIdentifier: "log") as! UINavigationController
window?.rootViewController = rootController
}
Edit: The added photo shows the problem when I only use the function goIncident() without dismissing. If I do dissmiss, I see the home screen and only after the home screen is presented when it calls the new screen to appear.
So I used a very ugly method to solve this, but I believe this is the only possible way, since this Pod is not very flexible, unless I missed something. What I did is this:
In SideBarViewController:
func goLog(){
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "changeLog"), object: self)
}
In MainViewController:
override func viewDidLoad(){
NotificationCenter.default.addObserver(self, selector: #selector(goLog), name: NSNotification.Name(rawValue: "changeLog"), object: nil)
}
func goLog(){
let logViewController = self.storyboard?.instantiateViewController(withIdentifier: "IncidentLogViewController") as! IncidentLogViewController
self.navigationController?.viewControllers = [logViewController]
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "dismissSideBar"), object: self)
}
So the Side bar tells the navigation controller to replace its view controller, and then after its replaced the navigation controller send back a notification to dismiss the the side bar. Pretty ugly but actually works smoothly.
Related
I have 2 screens with which I am navigating between them. One screen is for clicking a button to navigate to the other screen and the other screen is for presenting some other information.
Screen one
I have the following piece of swift code since screen 2 uses third part library
let host = UIHostingController(rootView: AnyView(vc))
let scenes = UIApplication.shared.connectedScenes
let windowScene = scenes.first as? UIWindowScene
let application = windowScene?.windows.first
ScanGuideContainer(title: self.viewModel.defaultLocalizer.stringForKey(key: ""), icon: icon,action: {
application?.rootViewController?.present(host, animated: true, completion: {
})
})
.environmentObject(viewModel)
This automatically launches a modal pop which then navigates to Screen2.
Screen 2
This screen has two options either to go back to the modal by pressing the back button or automatically go back once done processing some information which when it does so it returns a new value which then I can use to dismiss the host automatically since its a boolean value.
I can access the second screen viewController via
secondScreen?.viewController
How can i dismiss the modal from first screen when user presses back button of second screen?
What I tried
.onChange(of: self.ViewModel.dismissModal){ newvalue in
if(newvalue) {
host.dismiss(animated: true)
}
}
I set self.ViewModel.dismissModal value from second screen on but it does not work. please help
I am showing a UIViewController via a SwiftUI view, like so:
#available(iOS 13, *)
struct WelcomeNavView: UIViewControllerRepresentable {
func makeUIViewController(context: UIViewControllerRepresentableContext<WelcomeNavView>) -> UINavigationController {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let navVc = storyboard.instantiateViewController(withIdentifier: "welcomeNav") as! UINavigationController
return navVc
}
func updateUIViewController(_ uiViewController: UINavigationController, context: UIViewControllerRepresentableContext<WelcomeNavView>) {
}
}
I then present it from a ViewController like so:
self.viewController?.present(style: .fullScreen) {
WelcomeNavView()
}
However, it does not occupy the entire screen and the UIHostViewController color is showing at the top and bottom:
How can I change the color of the UIHostingViewController's view.. Or expand the View it is holding to occupy the entire screen?
Another simple and quick solution is, you can ignore safe area of your WelcomeNavView() while presenting.
example
iOS 13:
WelcomeNavView().edgesIgnoringSafeArea(.all)
iOS 14 and above:
WelcomeNavView().ignoresSafeArea()
Here is the solution,
I ran into similar problem, wherein I had to set hosting view controller's background clear.
Apparently, rootView (probably our SwiftUI View) of hostingVC and hostingVC.view are different.
so this would allow you to change to background color.
hostingVC.view.backgroundColor = .clear
Of course use any color in place of ".clear".
Just keep in mind to change color of SwiftUI view passed as rootView to hostingVC accordingly, mostly make that clear, so it won't be shown against above set color.
Your_SwiftUI_View.background(Color.clear)
I am using the following code snippet:
let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "secondViewController") as! secondViewController
self.navigationController.pushViewController(secondViewController, animated: true)
I want to execute this code in the viewDidLoad. I will be using something like this in a series of if statements.
I used some code I found here that utilized a label. It did not work. Can a program move from one viewController without using a button? Almost every example, youtube video uses a button. I will be using a button when it is appropriate
You can not push view controllers before your current view controller loads. You should call your code in your viewDidAppear.
If you plan on directing to a particular view when the app launches I suggest you moving your logic to the AppDelegate in the method: didFinishLaunching. This way you don't need to unnecessarily load views.
Example of a ViewController being pushed in the viewDidAppear:
///
/// FUNCTION - VIEW DID APPEAR
///
override func viewDidAppear(_ animated: Bool) {
// GET VIEW CONTROLLER
let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "secondViewController") as! secondViewController
// PUSH VIEW ONTOP OF CURRENT NAVIGATION CONTROLLER
self.navigationController.pushViewController(secondViewController, animated: true)
} // END - FUNCTION
I have a Table View Controller as a tab. In it, there's a button that takes you to a Facebook profile:
func didTapFacebook() {
let url = URL(string: "http://www.facebook.com/" + myFacebookId)
if UIApplication.shared.canOpenURL(url!) {
UIApplication.shared.open(url!, options: [:], completionHandler: nil)
}
}
Works fine every time you press the button.
From another tab, there's a button that presents modally a navigation controller with Table View Controller as its root:
func segueToTable(_ sender: UIViewController, tvc: MyTableViewController, completion: #escaping ((_ done: Bool) -> Void)) {
let nc = MyNavigationController(rootViewController: tvc)
sender.present(nc, animated: true, completion: {
completion(true)
})
}
…
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let tvc = storyboard.instantiateViewController(withIdentifier: "TVC") as! MyTableViewController
segueToTable(self, tvc: tvc, completion: { done in
print(“segue complete”)
})
Now once you tap the same button to go to Facebook profile (or Twitter button, basically anything that causes the app to go to background), this warning occurs (there is no keyboard displayed on the screen at this time):
Cannot snapshot view (>) with afterScreenUpdates:NO, because the view is not in a window. Use afterScreenUpdates:YES.
Returning to the app and pressing the button again causes this warning to appear (and every attempt after that):
Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates.
I realize this is related to snapshotting a view that automatically occurs prior to entering the background. It appears for some reason it doesn't like doing that within a view that was presented modally. I've tried a variety of things based on other posts but cannot get the warnings to go away.
Any help is appreciated.
The navigation controller is not the initial view controller. It is being instantiated in the initial VC and supplied with a root controller, the second VC. When I call present view controller function I cannot change the way in which this new VC in being presented, it always appears from the bottom. I did not find how to make it come from right to left. I tried to change who gets the call to modalPresentaionStyle from the second VC to the nav con and back but no change.
Code listing below. Thank you for your help.
#objc fileprivate func showRegisterScreen() {
let registerAccountVC = RegisterAccountVC()
let navigationController = UINavigationController(rootViewController: registerAccountVC)
navigationController.modalPresentationStyle = .pageSheet
present(navigationController, animated: true, completion: nil)
}
This will add a custom animation from right to left.
let transition = CATransition.init()
transition.duration = 0.4
transition.timingFunction = CAMediaTimingFunction.init(name: kCAMediaTimingFunctionEaseInEaseOut)
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromRight
view.window?.layer.add(transition, forKey: nil)
present(navigationController, animated: false, completion: nil)