Presented Modal view controller not displayed like a Form - ios

I am new to iOS programming. I am learning How to use Modal in iOS. I have a button. Inside button tap handler. I want to show a UIViewController like Modal form.
Here is my code:
#IBAction func userEditClick(sender: AnyObject) {
let userCtrl = UserEditViewController(nibName: "UserEditViewController", bundle: nil)
userCtrl.callback = self
userCtrl.modalPresentationStyle = UIModalPresentationStyle.FormSheet
//userCtrl.preferredContentSize = CGSizeMake(400, 800);
self.presentViewController(userCtrl, animated: true, completion: nil)
}
UserEditViewController is showed BUT the screen look like Normal UIViewController -> This is not what I expected ---> Form Modal
Thanks!

You just call presentViewController. That will do the job. The default animation of the presentViewController is modal. Also when you see the documentation it says that the setting will adapt to the environment. So I think when you are on the iPhone the FormSheet have to cover the whole screen. I think you may stick to the apple's default behavior.
In a horizontally regular environment, a presentation style that
displays the content centered in the screen. The width and height of
the content area are smaller than the screen size and a dimming view
is placed underneath the content. If the device is in a landscape
orientation and the keyboard is visible, the position of the view is
adjusted upward so that the view remains visible. All uncovered areas
are dimmed to prevent the user from interacting with them.
In a horizontally compact environment, this option behaves the same as
UIModalPresentationFullScreen.
Available in iOS 3.2 and later.
Ok so if you want to show like form sheet on the iPhone or iPad you need to calculate the size and show them with the animation you make and load it on your view controller.
Or you can check this repository https://github.com/m1entus/MZFormSheetController
They did the hard parts :)

Related

What is the technical name of the window/view that slides up with the 'directions' button when I touch a pin on my iPhone map?

I want to program a MapKit Annotation so that when a pin is touched on the screen a view that slides from the bottom of the screen comes up about 1/4 of the screen and offers the directions button along with description, photos, phone number, website and such. Just like it happens with the default iPhone maps application.
What is the name of this view that comes up from the bottom? And, are there any tutorials to program it for an app? It took me awhile to learn that the pins are actually named MKPointAnnotations on the iOS SDK, if someone can tell me what the name of this view is, or how to set it, that would be great.
Thanks.
it's called bottom sheet in Android, to mimic that same behaviour in iOS you can check this link
How can I mimic the bottom sheet from the Maps app?
I hope this will help you.
Approach 1:
It's a view, literally, just add your 1/4 view with the desired design.
Approach 2:
It can be a view controller, the main thing is how you present the new controller and it's background color. You have to present it with modalPresentationStyle = .overCurrentContext
let vc = <#YourViewController#>
vc.modalPresentationStyle = .overCurrentContext
self.present(vc, animated: true)
On presented view controller: self.view.backgroundColor = UIColor.clear, or any transparent color, and then you just add your 1/4 view with the desired design.
Note:
For handling moving the view as the user touches it, you can use UIPanGestureRecognizer
You will also want to animate it using UIView.animate, if you wanna do it without user interaction for some reason.

Showing a view controller in Modal Dialog programmatically is appearing as full screen

I have 2 swift View controllers and i want the second one to show as a modal dialog ( like bootstrap modal in web) on top of the first view when i click a button. I used the below code to achieve this, but the second view controller is still appearing full screen!
This is the code i execute to show the second view controller
var addCtrl = self.storyboard?.instantiateViewControllerWithIdentifier("mCtrl")
as? ModalContentViewController
addCtrl!.modalPresentationStyle = .Popover
presentViewController(addCtrl!, animated: true, completion: nil)
mCtrl is the storyboard id of the second viewcontroller.
What am i missing here ? My objective is to show the second view controller in a modal dialog.
EDIT : I am trying to make it work on iPhone.
You don't mention whether you're on iPhone or iPad. Assuming you're on iPad, if you want something like a modal dialog, then you'd be better off using UIModalPresentationFormSheet, which in swift I guess would be .FormSheet. If you really want to use a popover, you'll need to tell it where to popover from. You'd do this by setting the sourceView and sourceRect, or the barButtonItem properties of the controller's popoverPresentationController, after the call to presentViewController.
If you're on the iPhone, you'd still get a full screen presentation in both these cases. However, you can get the iPad style presentations if you set the presenting controller to be the presented controller's presentationController's delegate, before the call to presentViewController, and then implement adaptivePresentationStyleForPresentationController:traitCollection to always return UIModalPresentationNone.
Based on Apple's documentation, you try setting preferredContentSize explicitly for the popover view. Something like
addCtrl!.preferredContentSize = CGSizeMake(200, 100)
The value in this property is used primarily when displaying the view controller’s content in a popover but may also be used in other situations. Changing the value of this property while the view controller is being displayed in a popover animates the size change; however, the change is not animated if you specify a width or height of 0.0.

Display Modal ViewController in PageViewController

I have a master page in my application that is based on PageViewController. On that master page I have a side bar that provides options to the user. One of the options is to display "About" information regarding the application. When the user click the About button in the side bar, I want to display my AboutViewController as modal. It appears all my linking up is correct however when I click the About menu option in the side bar, the whole screen simply goes black (which is the background color or my AboutViewController) and none of the content in my AboutViewController is visible.
Here is the code for my PageViewController. The highlighted code is the code that displays the modal view controller.
Here is a screen show of About View Controller Scene that I am trying to display.
Where is a screen shot of what the app looks like before I click the About button:
This is how a click the About button:
This is what I get after I click the about button:
Any suggestions as to what I am doing wrong?
UPDATE: Per the comment provided I updated my code as follows and I am still having the same problem.
gobj_sideBar.showSideBar(false)
iobj_AboutViewController = AboutViewController()
iobj_AboutViewController?.iobj_CloseAboutInfoDelegate = self
iobj_AboutViewController!.modalTransitionStyle = UIModalTransitionStyle.CoverVertical
// Cover Vertical is necessary for CurrentContext
iobj_AboutViewController!.modalPresentationStyle = .CurrentContext
// Display on top of current UIView
self.presentViewController(iobj_AboutViewController!, animated: true, completion: nil)
Also - I don;t believe a blank page is being displayed as in the code for my About View Controller I do have the following line of code which turns off the status bar and the status bar is not visible on the scene displayed when I click the about button.
override func prefersStatusBarHidden() -> Bool {
return true;
}
OK So I switched to using Modal Segues as suggest and that mostly works. The only challenge I have now is that I want the About View Controller to display as a small popup type view but instead it is taking up the full screen as seen below:
These are the properties I have set for my About view controller. Any idea how I should change it so the view comes up as a popup type window where the background is grayed out but the About view does not take up the entire screen?
I am not experienced enough to tell what's wrong but I am spotting some mistakes in your code :
modalTransitionStyle and modalPresentationStyle are to be set on the presented view controller not on the presenting.
You instantiate an AboutViewController, save a reference to it and set self as its delegate but in the call to presentViewController(...) you instantiate another AboutViewController
Also consider that the black is not the background color of your About scene but instead the color of a blank screen.
If I were you, I'd use a segue for that about scene as well, it's less error prone. And instead of delegation to dismiss the About view controller, try an unwind segue.

iPad popover view

I am having an issue with the UIPopoverController. I am trying to make a registration form appear on the screen when the app starts (and a login screen periodically thereafter). However, as the form is a little long I also require a toolbar that sits above the keyboard, with next, prev, done buttons (similar to those found in safari).
How can I make the toolbar appear on top of the popover view, while still maintaining a full width of the screen, the iPad is constantly in landscape mode.
I did have this working, however the buttons would only be active when they appeared in the same rectangle as the popover.
Any help here would be appreciated
Set your toolbar to be the inputAccessoryView for each of your UITextFields.
There are many examples of how to do this on SO and other sites.
Create a Class for your Previous,next Toolbar.
Make a method which return a Toolbar as below.
-(UIToolbar*)createToolBarForView:(UIView *)keyBoardView
{
//Add segment Controller and Selector for your segmentController
}
In the selector for your segment controller Use Delegates to notify the implementing Class that segment controller is getting fired.
After Making all these things set the inputAccessoryType for all the textFields for which you want that toolbar to appear
{
YourTextField.inputAccessoryView = [OBjectOfYourCustomView createToolBarForView:self.view];
}

iPad FormSheet/PageSheet modal view, when rotated fill entire screen.T

I have a iPad application, which has a UITabBarController as the root controller. From one of the tabs, I have a UIToolBar which contains a UIBarButtonItem, when clicked launches a Modal View.
The modal view appears in the correct size when first launched (in both landscape and portrait) however if you rotate the device, the modal view will then expand to fill the screen - regardless of how much you rotate the device from that point onwards.
I'm launching the modal view from the tab bar and I've set the ModalPresentationStyle in the view to be presented.
This affects the ModalView regardless of whether I set it to FormSheet or PageSheet.
Has anyone experienced similar behaviour, if so, how did you solve it?
Many thanks
EDIT
I've also noticed that since 4.2 when using a Modal Style Page/FormSheet that the view behind the Modal View isn't dimmed out anymore - am I presenting the view wrong or is this just standard on 4.2?
EDIT 2
Code:
In my default UIViewController I simply present the Modal view as follows:
void HandleNewMsgButtonTouch(object sender, EventArgs e)
{
PresentModalViewController(new ComposeMsgView(), true);
}
In the modal view itself, I've specified in the LoadView override:
public override void LoadView()
{
base.LoadView ();
...
// initialser code related to the view
...
View.AutoresizingMask = UIViewAutoresizing.None;
ModalPresentationStyle = UIModalPresentationStyle.PageSheet;
ModalTransitionStyle = UIModalTransitionStyle.CrossDissolve;
}
I've overriden the ShouldAutoRotateToInterfaceOrientation to always return true as well.
Regardless of whether I use Formsheet / Pagesheet, they both appear the correct width when first launched (whether in landscape or portrait - I'm aware PageSheet will fill the portrait view) however on rotation, the modal view will then fill the screen (as if I had presented the ModalView with FullScreen).
As stated in the comment, try this:
void HandleNewMsgButtonTouch(object sender, EventArgs e)
{
UIViewController oController = new ComposeMsgView();
oController.ModalPresentationStyle = UIModalPresentationStyle.PageSheet;
PresentModalViewController(oController, true);
}
LoadView() is too late to set the properties.
Previous content:
Please post some code. The view is definitely dimmed, also in 4.2.
I assume that you are setting the presentation style on the wrong controller, because "PageSheet" is transformed to fullscreen when in portrait mode.
You have to set the presentation style on the controller you want to show in the moda view and not on the one which IS showing. So if your controller to show is "oToShow" and the one you are showing from is "oCurrent", you would have to set the style for "oToShow".
Try setting it to FormSheet which will always be on top and never goes fullscreen.
René
you can work around it by using a different style, like
UIModalPresentationFullScreen: This is the default style and as the name suggests causes the modal view to be presented full screen exactly as with the iPhone
UIModalPresentationPageSheet: The modal view occupies the full screen height but the width is set to the width of the screen in portrait mode. This means that when the device is in portrait mode it still occupies the full screen. However in landscape mode it leaves some of the underlying master view to still be visible, but dimmed as the user cannot interact with the uncovered master view controller.
UIModalPresentationFormSheet: Both the width and height of the modal view are set to be smaller than the screen size. The modal view is centered on the screen and the uncovered areas of the master view controller are dimmed to prevent user interaction. If the modal view makes used of the keyboard the modal view is moved upwards to make room.
UIModalPresentationCurrentContext: The modal view is displayed using the same style as the parent master view controller.
also remember to adjust size and position because it would otherwise always be shown in PageFormSheet's default size.
hope this helps
Alex
View.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;

Resources