Swiftui checking if UIHostingController is presented - swiftui

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

Related

Snapshotting a view that has not been rendered results in an empty snapshot when view presented modally - Swift 3

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.

swift 3, iOS - UIImageView for background

I currently have several labels and buttons in a UIView. I am using Storyboards and I want to add an image to the background. I tried it this way:
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor(patternImage: UIImage(named:"background.png")!)
But unfortunately, this does not look right in the larger screen sizes. I think that I need to add a UIImageView. When I added a UIImageView, I couldn't figure out how to set it to be in the background so my button and labels could still be seen.
let someImageView: UIImageView = {
let theImageView = UIImageView()
theImageView.image = UIImage(named: "yourImage.png")
return theImageView
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(someImageView) //This add it the view controller without constraints
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
someImageView.frame = view.bounds
}
In your storyboard:
1- Add a UIImageView (inside your main view and outside everything else) and set its image to the image you want.
2- setup its constraints properly to fill the screen (give it 0 from all sides).
3- From attribute inspector, set Content Mode property to Aspect Fit.
I solved this problem by going to the panel in Storyboards and moving the UIImageView above the container view that held the labels and buttons. The UIImageView remained inside of the View, but moved to the background (under the buttons and labels).

Using SideMenu Pod for navigation causes bugs when changing screens

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.

SWRevealViewController button not work after clicking Back button from another ViewController in Swift

I use SWRevealViewController for my project. My problem is that I can click on the SWRevealView Toggle button at first time and that button does not work after I click on Back button from another view controller and back to that view. Here is the screenshot of my project.
I click on NavigationLeft button from Service View Controller at first and left menu shows up. After that, I click on "Imageview" from that view controller to go to next page.
When I reached to detailed next page, I click on "Back button" and it goes to Service view controller.
At that time , I click on RevealView Toggle button , it does not work. I got error fatal error: unexpectedly found nil while unwrapping an Optional value.
My codes from Service View Controllers are;
override func viewDidLoad() {
super.viewDidLoad()
if self.revealViewController() != nil {
debugPrint("Menu Click")
btnBack.target = self.revealViewController()
btnBack.action = #selector(SWRevealViewController.revealToggle(_:))
self.revealViewController().panGestureRecognizer()
}
else
{
debugPrint("nil")
btnBack.target = self.revealViewController()
btnBack.action = #selector(SWRevealViewController.revealToggle(_:))
// self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
self.revealViewController().panGestureRecognizer()
}
}
When the project runs first time, the code passes to "self.revealViewController() != nil" and I go to detailed view controller and click on Back button. At that time the code passes to " debugPrint("nil")" and it does not show up menu or work.
Codes from Detailed view controller are;
#IBAction func btnBack(_ sender: UIBarButtonItem) {
debugPrint("BtnBack")
self.dismiss(animated: true, completion:nil)
}
Is my code is something wrong or logic wrong ? I have been trying to solve this problem since one week. Please help me ..
On click event of image view in serviceViewController,
let detailVC = self.storyboard?.instantiateViewController(withIdentifier: "Detail") as! DetailVc
self.navigationController?.pushViewController(detailVC, animated: true)
Attach a navigation controller to detail viewcontroller. No need for manual connection between service and detail.
Add a barbutton for goingback event.
self.navigationController?.popViewController(animated: true)
Also add self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer()) instead of ur code in the lastline of adding target for menubutton.
Check whether this works.

How to set initial focus to a button in a viewController from a UITabBarController?

I have a basic application with a UITabBarController which contains 2 UIViewControllers.
The first ViewController has a UIButton in it.
I'm trying to get the tabBar menu hidden and the button focused when the app launches.
I set the tabBar as hidden, and it does not show on launch as expected. Unfortunately, the button doesn't have the focus.
From what I understand from Apple doc, the "focus chain" engine will set focus on the first visible and focusable item in the window hierarchy.
Can anyone help me on this ?
Thank you.
Solution 1.
I think what you can do :
1.Subclass or Customize TabBarController and set
set first launch = true in viewdidload of tabController
> override weak var preferredFocusedView: UIView? { if
> (self.firstLaunch) {
> self.firstLaunch = false;
> return self.selectedViewController!.preferredFocusedView; } else {
> let view = super.preferredFocusedView
> return view; }
Hope it helps.It worked for me though
Your focus engine takes the rootView controller as TabBarController and then asks for preferred focus view which is returned as UITabBarItem we need to unfocus that subclassing the class and returning canBecomeFocus to NO.
if you want to change the focus of the element in the firtsViewController then you can overrirde func preferredFocusView and reutrn the view you want to be focussed else preferredFocusView
Solution 2. Just set the root View controller as the HomeViewController without the TabBarController and embed the TabBarController from the second page. This is because anyways you don't need to use TabBar on the first page why need to take care of its focus.
TabBar is set to hidden if you hide from the AppDelegate or the view which is loaded but it has the focus.