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
Related
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
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.
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.
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)
I want to have a button on my navigation bar "Reset" and I would like this to be connected to an IBAction to sort of "restart" the controller.
I have some segues from another controller that changes some aspects of the viewcontroller (that has a collectionview) and I want the user to be able to start over. Does anyone have any suggestions as to how to proceed?
If you embed navigation controller to your view controller then you can use this code:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("ViewController")
let viewcontrollers = self.navigationController.viewControllers
viewcontrollers.removeLast()
viewcontrollers.append(vc)
self.navigationControllers?.setViewControllers(viewcontrollers, animate: true)
You can change rootViewController of your application:
UIApplication.shared.keyWindow?.rootViewController = UIViewController()
This is how I do it in Swift 2.1 with my Home view inside a UINavigationController:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let homeVC = storyboard.instantiateViewControllerWithIdentifier("HomeViewController")
self.navigationController?.presentViewController(homeVC, animated: false, completion: nil)
self.navigationController?.dismissViewControllerAnimated(false, completion: nil)