New view controller showing up modally instead of full screen on iPads - swift

As I want to have more control over actions of when someone could press a button for going to another view controller, I tried to step away from the navigation controller system.
At the moment, as I'm still just a starter in Xcode I'm struggling with opening a view controller and closing it.
I tried to open it with this code:
#IBAction func didTapGame(){
let vc = storyboard?.instantiateViewController(identifier: "Game_VC") as! Game_ViewController
present(vc, animated: true)
}
and on the iPhones it seemed legit, but on the iPad it appeared modally as a screen on top of the homescreen of the app and too small so you could just tap it away on the side.
How do you properly present a view controller in code in storyboard and how do you close it again with a backbutton as I tried the backbutton the same way, but it just added a new homescreen on top of everything which isn't desirable of course, Thanks!

It is difficult to answers without more context, but if you have a UINavigationController you can try this:
self?.navigationController?.pushViewController(viewController: vc, animated: true)

Related

Graphical artefact on navigation bar and toolbar when presenting UIPageViewController modally over a UITableViewController

My app is simply a UITableViewController embedded in a UINavigationController. In the main tableview controller viewDidLoad() method I check UserDefaults for a Boolean key to see if it is the first startup. If so, I modally present a UIPageViewController for a tutorial and onboarding.
self.present(tutorialViewController, animated: true, completion: nil)
However, underneath the tutorial screens, my main view controller’s navigationBar and toolBar change to a light grey colour until the tableview is interacted with, then they instantly change back to the default translucent appearance that I want. I cannot figure out what is causing this. I tried setting modalPresentationStyle to .overCurrentContext but this did not help.
I also get a warning that presenting view controllers on detached view controllers is discouraged. This warning goes away if I present it as follows:
self.parent?.present(tutorialViewController, animated: true, completion: nil)
This seems messy however, and the graphical artefact remains.
Am I presenting the tutorialViewController from the wrong place or in the wrong way? The tutorial screens seem to work as expected - it’s just the underlying navigation bar and toolbar briefly have the wrong colour. I need to present it over the tableview as the last page of the tutorial is transparent with a blur effect to help see how the app works.
I found the solution based on some discussion here.
I was presenting the modal view controller from the viewDidLoad() method when I should have done it in viewDidAppear().
I was also animating my tableview to fade in, so it was not visible before the tutorial screens. Instead, I’m now using a blocking view set to the same background colour as my tutorial view controller. In viewDidLoad() I do this:
let blockingView = UIView()
blockingView.frame = UIApplication.shared.keyWindow?.frame
UIApplication.shared.keyWindow?.addSubview(blockingView)
This means the blocking view appears in front of the navigation controller. I fade and remove the blockingView just before presenting the tutorial in viewDidAppear().

How to present view controller modally when pressed tab bar in Swift or Storyboard?

I want to show the view controller modally when I pressed the middle tab bar. I try to pick "over the screen" on presentation style, it work like modal but the transition style is not like the modal presentation.
Sorry I'm new in iOS. I need a custom tab bar tutorial that can show a view controller modally, I can't find it...
The layout similar like this:How to create tabbar with custom UI in swift 3.0
but can I do that without framework? because quite complicated for me..
For presenting viewController
self.present(ClassToPePresented(), animated: true, completion: nil)
For Pushing viewController
navigationController?.pushViewController(ClassToPePushed(), animated: true)
Modify class name as requirement

keeping presentingViewController alpha at 1 when presenting popover in iOS

I am modally presenting a popover on top of a label similar to the popover you see when you do a long press on a message in a chat application like whatsapp or messages. The popover does not take over the entire view and the presenting view is visible in the background.
I have used a popoverPresentationController
However, I don't want the original presentingView (the tableView for the chat) to fade when the popover is active.
Is there anyway to control this?
You can use a presentViewController instead of a popoverPresentationController, using controller.modalPresentationStyle and controller.modalTransitionStyle to make it look as a Pop Over. I'm not sure if this is what you want, but this way you will have alpha = 1 in the parent view controller.
yourViewController.modalPresentationStyle = .overCurrentContext
self.present(yourViewController, animated: false, completion: nil)

Changing views programmatically

I know there are a lot of posts about this but my problem is a bit different. I have a Tab Bar Controller with multiple views and one of the views is the Login View, if the user does Login it should change the view to present the User menu.
At the same time i need to keep the Tab Bar menu visible, so i can't use modal since it will loose the reference to the Tab Bar Controller.
So i ended up using a Navigation Controller where i just push the User Menu view into the stack (not the best way but it works).
The problem is, if the user double click on the Login Bar it will load the Login View, and i don't want this to happen.
On this example, if the user clicks on the Login button it will go to the Blue view, but if the user double click on the Feature Button on the Tab Bar Menu it will go to the red view.
Since my solution does not work how can i achieve what i want? If the user press the Login button it will ONLY show the Blue view until he press the Logout button.
[Edit]
Here is the code to show the blue view:
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let ViewController = storyBoard.instantiateViewController(withIdentifier: "userViewID") as! UserViewController
self.navigationController?.pushViewController(ViewController, animated: true)
Ok i ended up checking if the Navigation Controller had more than one child (for the red one) and removing the first child from the navigation array so it could dealloc it and show only the one i wanted.
override func viewWillAppear(_ animated: Bool) {
if((self.navigationController?.viewControllers.count)! > 1)
{
self.navigationController?.viewControllers.removeFirst()
}
}
The reason i ended up doing this was because clicking 2x on the Tab Bar item it would bring me to the red view even if i had already pushed the blue view to the Navigation array.

Simulating a UINavigationController Pop

I'm using a navigation controller and it works great. However, some custom buttons segue to new ViewControllers (not in the stack) and I don't like the push-type transition animation it uses.
GOAL:
I want to the transitions (described above) to mimic a nav-stack pop transition (where the current view slides to right, revealing the view underneath).
I've successfully simulated a nav-pop using the below code, but then after my buttons and nav-bar back button are super glitchy.
#IBAction func Page2_to_Page1_ButPush(sender: AnyObject) {
var curPage: UIViewController = self.storyboard!.instantiateViewControllerWithIdentifier("Page2_ID") as! UIViewController
var prevPage: UIViewController = self.storyboard!.instantiateViewControllerWithIdentifier("Page1_ID") as! UIViewController
self.navigationController?.pushViewController(prevPage, animated: false)
self.navigationController?.pushViewController(curPage, animated: false)
self.navigationController?.popViewControllerAnimated(true)
}
Can anyone supply a simple way to make my segues simulate a nav-pop?
Details:
X-Code 6.4
Swift
Storyboard
Here's the solution:
iOS Segue - Left to Right -
Simple, easy, works perfect. Add the "SegueFromLeft" class code shown in the link, select the segue you want to present from left, select custom, pointed it at "SegueFromLeft", and the animation is dead on.
Bonus: Even better, it syncs up with the navigation controller, so the nav "Back" button still works.