Swift - Without storyboard , how do I make a modal popup? - ios

I didn't use a storyboard and I'm having trouble finding resources on programmatically making a modal popup ( it'll be a form with white background that takes up the bottom half of the screen) after you press a button. The previous view would be disabled until you exit out of this modal popup.
Would this modal popup have its own view and view controller? How would I make the popup appear and after you exit, you give the control back to the original view controller?
Thanks?

let rateViewController = RatePopupVC()
rateViewController.modalTransitionStyle = .crossDissolve
rateViewController.modalPresentationStyle = .overCurrentContext
rateViewController.item = item // In case you want to transfer some data
self.present(rateViewController, animated: true, completion: nil)

Would this modal popup have its own view and view controller
Yes. Configure your view controller and its view, and present it with present and dismiss it with dismiss. You can add custom transition animation and custom positioning of the view.

Related

Display a popup with "swipable slides" in xamarin.ios?

I have done major refactoring of my app and to help my users align with the new behaviour, when starting my app I want to display a few info slides that the user can read before starting to use the app (with the normal "don't show on startup"-button).
I'm using storyboard and have implemented it as using InstantiateViewController from a programmatically instantiated storyboard and just presenting it as UIModalPresentationStyle.Popover in the calling viewcontroller but it doesn't give me the "popup" effect that I'm after. The popup vc is displayed "over" the calling viewcontroller but the calling view "shrinks" as it goes into the background.
Is there another way to keep the design in storyboard but displaying the uiviewcontroller as an alert dialog or similar to get the more of a popup/popover/alert style?
{
var storyboard = UIStoryboard.FromName("MyStoryboard", null);
var popup = storyboard.InstantiateViewController("PopupController");
popup.ModalPresentationStyle = UIModalPresentationStyle.Popover;
// Present the view controller (in a popover).
PresentViewController(popup, true, null);
}

TabBarController disappears when I segue back to the view from another view

I have a viewController that is embedded in a nav controller. This nav controller is then embedded in a tab bar controller.
I have another viewController that is not supposed to be accessible from the tabBarController. It should only be accessible from the first viewController using a button. From the secondViewController, I made a UIBarButtonItem to move back to the original view. From the first view to the second view and vice versa, I used a Storyboard reference to move to and from from the views.
However, when I move from the first view to the second view, the tab bar controller disappears (like it should). When I move back to the first view, the tab bar controller disappears and I cannot move between tabs anymore.
I tried including:
self.hidesBottomBarWhenPushed = false
on the first view and
self.hidesBottomBarWhenPushed = true
on the second view
and nothing seems to work. The tab bar controller disappears every time i move from the second view to the first view.
You are following a wrong hierarchy. You are actually using seagues to go back and forth. This creates a new instance every time you try to come back to the first controller.
Let's make it clear:
You need to follow the below approach:
1 You have two controllers A and B.
2 Use self.hidesBottomBarWhenPushed = true in viewDidLoad or viewWillAppear of controller A.
3 Controller A is embedded in a navigation controller which is further embedded in a UITabBarController.
Tapping a button in controller A, you need to push to controller B. So you can use segue for this or you can do it programatically like:
let controllerB = B()
A.navigationController?.pushViewController(controllerB, animated: true)
4 Go Back to Controller A on the tap UIBarButtonItem. So your code in the action of UIBarButtonItem should be something like:
self.navigationController?.popViewController(animated: true)
Remember you should not should segue to go back to the previous controller.
you should use
override func viewWillDisappear(_ animated: Bool) {
self.tabBarController?.tabBar.isHidden = false
}
in that controller where the back button is placed, I am using the exact scenario in one of the my app.

Better method to make pop up view

Let's assume we want to show some popup view on our screen. When user clicks a button our view pops up. In popup view we have Close button that hide/remove view itself.
Which method should I use to do that kind of thing:
1.) After click on the button add popup view to my main view. Close button removes popup view from superview.
or
2.) Make popup view with alpha = 0 and after click a button change alpha = 1. Close button changes alpha to 0.
When I was adding and removing view by multiple clicking in buttons I have noticed that application started to slow down.
If you know good solution/method to do that kind of things (show view/views in another view) it would be very usefull.
Actually UIKit treats very-low alpha elements as hidden but hidden elements still participate in autoresizing and other layout operations associated with the view hierarchy. To save your performance i'd recommend to use addSubview/removeFromSuperview methods when show/hide your view.
You can also use ViewControllers with standard animation like:
let presentedVC: ViewController = self.storyboard?.instantiateViewControllerWithIdentifier("presentedVC") as ViewController
presentedVC.modalPresentationStyle = .OverCurrentContext
presentedVC.view.backgroundColor = UIColor.clearColor()
self.presentViewController(presentedVC, animated: true, completion: nil)
and when you tap Close inside your presentedVC use:
self.dismiss(animated: true, completion: nil)

Present Modal View in Detail View in UISplitViewController

I want to make a behavior like contacts app in iPad with landscape mode.
I'm expecting that a Modal shows in a Detail view when I click upper right add button.
but now if I click upper right add button, the Modal shows in all screen.
what method should I use? showDetailViewController? or presentViewController? I don’t know how to show Modal in only Detail View.
Firstly you need to set detail view controller's property definesPresentationContext = true. So now it defines presentation context. By default view controllers doesn't pay attention to the current context when they are presented therefore you must do viewController.modalPresentationStyle = .CurrentContext
That's how complete method looks like
func adaptivePresentViewController(viewController: UIViewController) {
let detailVC = splitViewController!.viewControllers[1]
detailVC.definesPresentationContext = true
viewController.modalPresentationStyle = .CurrentContext
detailVC.presentViewController(viewController, animated: true, completion: nil)
}

Edit button not displayed in UITabBarController's MoreNavigationController

A UITabBarController is being pushed onto the stack:
let presenter = presentingViewController as! UINavigationController
let tabvc = UITabBarController()
tabvc.viewControllers = vcs
tabvc.customizableViewControllers = vcs
presenter.pushViewController(tabvc, animated: true)
Once presented the more tab button correctly shows, but the edit button to rearrange the tab bars does not. According to the docs on the MoreNavigationController:
The interface for the standard More item includes an Edit button that
allows the user to reconfigure the tab bar. By default, the user is
allowed to rearrange all items on the tab bar. If you do not want the
user to modify some items, though, you can remove the appropriate view
controllers from the array in the customizableViewControllers
property.
My guess is that the tab bar is not happy being in a navigation controller. Any ideas on bringing the edit button back?
You can have both a UINavigationController and a UITabBarController ; using Storyboard helps understand the issue better, any of these solutions will work:
Start out with a UITabBarController as initial view controller
Use presentViewController instead of pushViewController
Use a modal Storyboard segue to perform a modal presentation
Swap out the rootViewController dynamically
Initial View Controller Design
When the Tab Bar Controller is initial View Controller, the Edit button is displayed normally.
Pushed Design
Another Navigation Controller is initial View Controller, using one of 5 adaptive Action Segue:
Show
Custom
-> No Edit button, since it is in direct conflict with the parent UITableViewController.
Show Detail
Present Modally
Popover Presentation
-> Edit button displayed as expected.
Code
1. Program Modal
Using the exact code presented in the question, change the last line:
let presenter = presentingViewController as! UINavigationController
let tabvc = UITabBarController()
tabvc.viewControllers = vcs
tabvc.customizableViewControllers = vcs
presenter.presentViewController(tabvc, animated: true, completion: nil)
2. Storyboard Modal
keeping with the Storyboard theme, create a segue of the correct type, assign an identifier (i.e. presentModallySegue) and the 5 lines above become this single line:
self.performSegueWithIdentifier("presentModallySegue", sender: self)
3. root Swap
A more drastic solution involves swapping out the root view controller at the window level:
let tabvc = UITabBarController()
tabvc.viewControllers = vcs
tabvc.customizableViewControllers = vcs
self.view.window!.rootViewController = tabvc
Conclusion
Either change your design to adopt the Tab Bar Controller as the initial View Controller, or present the Tab Bar Controller modally.
The reason is that navigation bar of your presenter overlaps with the navigation bar of More section.
If you don't show the navigation bar for you navigation controller, you will be able to see the Edit button again when you tap on the More tab.

Resources