Default screen when I focus out - iOS - swift

Always when I focus out (click on the "Home" button), Sign In page is displayed.
How to change the default picture when the app is out of focus?

Add following code in applicationWillResignActive function in Appdelegate
let storyboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let navigationController:UINavigationController = storyboard.instantiateInitialViewController() as! UINavigationController
let rootViewController:UIViewController = storyboard.instantiateViewController(withIdentifier: "You_Signin_Page") as UIViewController
navigationController.viewControllers = [rootViewController]
self.window?.rootViewController = navigationController
This will set "Your_Signin_Page" as yourrootViewController everytime you press the home button

Related

Why pushing to a ViewController displays it as popup?

I have been trying to figure this for a while now. When I'm pushing to a ViewController it displays it as a popup instead of the fullscreen.
I have UINavigationViewController embedded in MainPageViewController. Inside MainPageViewController, I have a "Sign in" button that once clicked it should display the HomePageViewController. The question is why it is displayed as a popup instead of fullscreen?
Below is the code that I'm using to push to the HomePageViewController, inside the Sign In button action method:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destinationViewController = storyboard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
let destinationNavigationController = storyboard.instantiateViewController(withIdentifier: "NavigationViewController") as! UINavigationController
You have to give ViewController Presentation as a Full Screen
If you need to do it by code, you can set presentation and transition style like this:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destinationViewController = storyboard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
destinationViewController.modalPresentationStyle = .fullScreen
destinationViewController.modalTransitionStyle = .coverVertical
And then push from your navigation controller

Navigation bar missing after tapping on remote notification

I have a side menu in app so on clicking the notification I navigate to view controller but cannot navigate to other VC's as it's not showing the navigation bar.
I've done this in my App Delegate and also have a navigation controller in my storyboard.
let sb = UIStoryboard(name: "Main", bundle: nil)
let otherVC = sb.instantiateViewController(withIdentifier: "messageview") as! MessageViewController
window?.rootViewController = otherVC;
func application(_ application: UIApplication,didReceiveRemoteNotification userInfo: [AnyHashable: Any],fetchCompletionHandler completionHandler:#escaping (UIBackgroundFetchResult) -> Void) {
let state = application.applicationState
if state == .inactive || state == .background {
let sb = UIStoryboard(name: "Main", bundle: nil)
let otherVC = sb.instantiateViewController(withIdentifier: "messageview") as! MessageViewController
window?.rootViewController = otherVC
print("background")
} else {
print("foreground")
}
}
Of course the navigation bar is removed; the view controller needs to be embedded in a navigation controller.
Try this:
let sb = UIStoryboard(name: "Main", bundle: nil)
let otherVC = sb.instantiateViewController(withIdentifier: "messageview") as! MessageViewController
// (actually, you don't need to cast to MessageViewController here;
// the returned reference already is guaranteed to at least be a
// UIViewController instance; you don't need more specificity in this
// case --i.e, embed in navigation)
let navigation = UINavigationController(rootViewController: otherVC)
window?.rootViewController = navigation
Note: This will present a fresh navigation with your view controller as the root. You will not be able to "navigate back" to any other screens that normally preceed it when navigating manually from your app's initial screen.
If you need to recreate a specific location within your navigation hierarchy, you will need to instantiate all previous view controllers up to the one you want to show, and set them instantly as the contents of the navigation stack.
Dummy example:
let first = UIStoryboard(name: "First", bundle: nil).instantiateInitialViewController()
let second = UIStoryboard(name: "Second", bundle: nil).instantiateInitialViewController()
let top = UIStoryboard(name: "Mail", bundle: nil).instantiateInitialViewController()
let vcs = [first, second, top]
let navigation = UINavigationController()
navigation.setViewController(vcs, animated: false)
window?.rootViewController = navigation
(this example assumes each VC lives in a separate storyboard, but you can also use instantiateViewController(withIdentifier:_) if that fits your setup)
Now, you can go back to "Second" and "First" using the navigation controller's back button.

Push to ViewController from AppDelegate with stack and navigation Bar

I'm developing a messaging app and I'm having trouble implementing push notification behaviour.
What I want is that when a user taps on the notification, the conversation of that group opens. But there has to be a Navigation bar to go back to the list of contacts.
I already know how to extract information from the notification to get the contact name. I'm trying this in the function didReceive on AppDelegate.
So far what I've tried is:
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ChatViewController") as! ChatViewController
vc.contactName = name as String
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = vc
self.window?.makeKeyandVisible()
This open the right conversation and the messages appear, but UI elements are no responsive (can't write when tapping textfield, cant navigate to gallery when tapping button, etc.) and also there is no Navigation bar.
So after doing some research, I've tried this:
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ChatViewController") as! ChatViewController
vc.contactName = name as String
let rootViewController = self.window?rootViewController as! UINavigationController
rootViewController. pushViewController(vc, animated: true)
This doesn't work. There is no error message but the screen is just white.
This is my Storyboard:
I'm using SWRevealViewController for the side menu and the messaging part has a tabBarController, with the contact list on the 2ยบ tab.
EDIT: With the second method, there is an error.
Could not cast value of type 'SWRevealViewController to UINavigationController'
I think you should go with NotificationCenter to do this.
When the user click on the notification you should post a NotificationCenter in the didReceive like this:
NotificationCenter.default.post(name: .onMessageRecieved, object: notificationObject)
And based on which view controller you're in you should add an observer there in the viewDidLoad() func so it understands a message has been recieved like this:
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(MyViewController.onMessageRecieved(notification:)), name: .onMessageRecieved, object: nil)
}
And you should have a func (onMessageRecieved) in the view controller to do the navigation like this:
func onMessageRecieved(notification: NSNotification){
// open the conversation
let vc = self.storyboard?.instantiateViewController(withIdentifier: "ChatViewController") as! ChatViewController
vc.contactName = name as String
self.navigationController?.pushViewController(vc, animated: true)
}
This way you are doing the navigation from the view controller so the navigation will be there as well.
Could not cast the value of type 'SWRevealViewController to UINavigationController'
This issue is because of the "SWRevealViewController" is not a subclass of UINavigationController. So you can't cast it this way.
To solve the chat ViewController issue, you should modify your code like this.
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ChatViewController") as! ChatViewController
let nav = vc.navigationController
vc.contactName = name as String
let rootViewController = self.window?rootViewController as! UIViewController
rootViewController.presentViewController(nav, animated: true, completion: nil)
But the chat VC will be in the presenter mode. You should handle the dismiss.
You just try this and let me know is it working or not.

show a ViewController from a Detail View Controller in a UISplitViewController

My aim is: To show a popUpViewController in my project in which the user can add a "lesson" (adding a tableViewCell to a tableView) and then save it.
So when the user presses a button inside the DetailViewController of a UISplitViewController , the popUpViewController should show.
I also want the pop-up to be shown as fullscreen with just a small window in the middle and the rest should be black with 0.5 alpha, transparent.
My problem is: I can't find out how I have to show the popUp.
What I've tried and my results:
Tried: Showing it with present(Viewcontroller, animated:, completion: )
Result: It shows fullscreen but the other VC isnt visible in the background anymore.
Tried: The following snippet:
let popOverVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "popUpVC") as! PopUpViewController
self.addChildViewController(popOverVC)
self.view.addSubview(popOverVC.view)
popOverVC.didMove(toParentViewController: self)
Result: The view isnt fullscreen but only the size of the detail View Controller (doesnt cover the part of the masterViewController on the left.
Thanks for your help.
You might try again with present(Viewcontroller, animated:, completion: )
and using modalPresentationStyle with formSheet eg:
let popOverVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "popUpVC") as! PopUpViewController
popOverVC.modalPresentationStyle = .formSheet
self.present(popOverVC, animated: true, completion: nil)

Attempt to present * whose view is not in the window hierarchy

I am using the following code in AppDelegate to display a popup window within the app when a button is selected, i will eventually move this to a label hyperlink but just testing currently.
let storyboard = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("test") as! ViewController
let popOverVC = UIStoryboard(name:"Main", bundle: nil).instantiateViewControllerWithIdentifier("sbPopUpID") as! PopUpViewController
storyboard.addChildViewController(popOverVC)
popOverVC.view.frame = storyboard.view.frame
storyboard.view.addSubview(popOverVC.view)
popOverVC.didMoveToParentViewController(storyboard)
self.window?.rootViewController?.presentViewController(storyboard, animated: true, completion: nil)
The first time i select a button this works correctly, however on all subsequent button presses the following error is displayed.
2016-10-28 11:27:40.551 testfordeeplinks[20496:104536] Warning:
Attempt to present
on whose view is not
in the window hierarchy!
For anyone who stumbles across this in the future, this is how i resolved;
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.window?.rootViewController = storyboard
rootViewController doesn't necessarily refer to the view controller that is currently visible. For example, if you utilise a UINavigationController, rootViewController will hold a reference to that navigation controller which in itself is buried deep in the view hierarchy and hence when you try to present a view controller on it get an error. What I would recommend doing is grabbing the last view controller in navController.viewControllers and presenting your popup on that. See the following code:
let appDel = UIApplication.sharedApplication().delegate as! AppDelegate
if let navController = appDel.keyWindow?.rootViewController as? UINavigationController {
if let visibleVC = navController.viewControllers.last {
visibleVC.presentViewController(storyboard, animated: true, completion: nil)
}
}
Give that a try and let me know. :)