iPad: UIModalPresentationFormSheet fails on landscape mode - objective-c

We've the next problem in our iPad version.
I've a NavigationController inside a UITabBar. I want to show a Form with a look&feel similar than the e-Mail form.
I use the same code to show a model centered:
// View to be displayed in the modal
AdhocViewController *controller = [[AdhocViewController alloc] initWithNibName:#"AdhocViewController" bundle:[NSBundle mainBundle]];
controller.caller = self;
// The form will need a navigation bar to cancel or save the form
UINavigationController *modalViewNavController = [[UINavigationController alloc]
initWithRootViewController:controller];
// Configurate the modal presentation and transition
modalViewNavController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
modalViewNavController.modalPresentationStyle = UIModalPresentationFormSheet;
// Show the new view
[self presentModalViewController:modalViewNavController animated:YES];
This code works perfectly on portrait mode, but on landscape the view appears partially out of the screen ... and I didn't found yet the way to solve it.
I test some of the solutions I found here ...
And try to add the next lines after preset the model view to resize it, but doesn't work it out
controller.view.superview.frame = CGRectMake(0, 0, 600, 700);
controller.view.superview.center = self.view.center;
Any suggestion?
Thanks,
Ivan
References in StackOverflow:
iPad modalPresentationStyle UIModalPresentationFormSheet orientation problem
UIModalPresentationFullScreen not working in iPad landscape mode?
UIModalPresentationFormSheet on iPad in landscape mode
How to resize a UIModalPresentationFormSheet?

With iOS7 the trick is to set the modalTransitionStyle to UIModalTransitionCrossDissolve.
UIViewController *viewController = [[UIViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
navigationController.modalPresentationStyle = UIModalPresentationFormSheet;
navigationController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:navigationController animated:YES completion:nil];
navigationController.view.superview.frame = CGRectMake(0, 0, 800, 544);
navigationController.view.superview.center = self.view.center;
https://coderwall.com/p/vebqaq

Finally the code was the next:
// Remove the modalTransitionStyle to enable the default behaviour and change to PageSheet
modalViewNavController.modalPresentationStyle = UIModalPresentationPageSheet;
// Present the modal view and adapt the center depending of the orientation
[self presentModalViewController:modalViewNavController animated:YES];
UIDeviceOrientation _orientation = [controller interfaceOrientation];
if (UIDeviceOrientationIsPortrait(_orientation)){
modalViewNavController.view.superview.center = CGPointMake(768/2, 1024/2 + 10);
} else {
modalViewNavController.view.superview.center = CGPointMake(768/2 - 10, 1024/2);
}
The +10 and -10 is because by deafult the NavigationController of the modal was out of the screen on top.
It is ... a crappy solution :SS but works ... Although if anybody have suggestions would be nice to know.
Seams that if I need to include the same center for both orientation, maybe the orientation of the superview is not the expected one.
In this solution, when I dissmiss the modal view on Portrait orientation, at least on the iPad simulator, it rotate to portrait mode automatically ...
The final solution was to execute the presentModalViewController over the main controller, the UITabBar, and update the dismiss method to be executed also over it.
[tabbar presentModalViewController:modalViewNavController animated:YES];
UIDeviceOrientation _orientation = [controller interfaceOrientation];
if (UIDeviceOrientationIsPortrait(_orientation)){
modalViewNavController.view.superview.center = CGPointMake(768/2, 1024/2 + 10);
} else {
modalViewNavController.view.superview.center = CGPointMake(1024/2, 768/2 + 10);
}
Finally!!!!
Thank you,
Iván

In iOS 7, to solve the problem of the modal view controller to appear to the left after apparition of the keyboard (problem that I have when I present an EKEventEditViewController in UIModalPresentationFormSheet, I do :
[self presentViewController:modalViewController animated:YES completion:^{
modalViewController.view.superview.center = self.view.center;
}];

Related

Translucent Viewcontroller over another Viewcontroller with different Orientation behaviour

For an app targeted at both iOS 7 and iOS 8, I'm using two ViewControllers which should have the following characteristics:
ViewController 1 (VC1) is in Portrait Mode
VC1 created ViewController 2 (VC2) which is in Landscape mode
VC2 has a translucent background so that VC1's contents are visible
I've referred to IOS 7: Transparent Viewcontroller Background over another Viewcontroller with different Orientation behaviour but the solution provided does not work. On iOS 8, I can either get landscape orientation or transparency for VC2 but not both
Here's what I'm doing:
self.view.backgroundColor = [UIColor clearColor];
UILabel* label = [[UILabel alloc] initWithFrame:self.view.frame];
label.text = #" I'M VC1";
[self.view addSubview:label];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
LandscapeViewController* lvc = [[LandscapeViewController alloc] init];
self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
//Uncomment this line for transparency but loses orientation
//lvc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
[self presentViewController:lvc animated:YES completion:nil];
});
Any idea what's wrong and how can I make it work?

Present UIImagePickerController inside a modal view or form sheet in iOS 7?

I'm not great at creating views in code yet so this is probably a simple question.
In my iPad app I have this image picker and I would like to present it in a 500x500 modal view centred on screen OR in a formsheet centre-screen:
_imagePicker = [[UIImagePickerController alloc] init];
_imagePicker.delegate = self;
_imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
_imagePicker.allowsEditing = NO;
_imagePicker.navigationBar.tintColor = [UIColor redColor];
_imagePicker.navigationBar.backgroundColor = [UIColor greenColor];
Now what do I do to both create the modal view or formsheet and present the image picker inside it?
(I've looked through many examples and Q&A and just can't find a simple answer. Thanks for your help!)
Form sheet is a modal presentation type. You change that by modifying the modalPresentationStyle property of a view controller before presentation.
_imagePicker.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:_imagePicker animated:YES completion:nil];
However, if you read the documentation of UIImagePickerController, you will see that Apple requires you to present the view controller inside a popover.
So:
if([_popoverController popoverVisible])
{
[_popoverController dismissPopoverAnimated:NO];
_popoverController = nil;
}
_popoverController = [[UIPopoverController alloc] initWithContentViewController:_imagePicker];
[_popoverController setDelegate:self];
[_popoverController presentPopoverFromBarButtonItem:_button permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

UIPopovercontroller doesnt remain on its position when iPad rotated

I'm creating a popover like this.
-(void)showPop{
if (self.popoverController2 == nil) {
RootViewController *iap =
[[RootViewController alloc]initWithNibName:#"RootViewController" bundle:[NSBundle mainBundle]];
[iap setDelegate:self];
UIPopoverController *popover =
[[UIPopoverController alloc] initWithContentViewController:iap];
popover.delegate = self;
popover.popoverContentSize = CGSizeMake(320, 300);
self.popoverController2 = popover;
}
CGRect popoverRect = [self.view convertRect:[shopButton frame] fromView:[shopButton superview]];
[self.popoverController2 presentPopoverFromRect:shopButton.bounds inView:shopButton
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
ShopButton is a button from which the popover pops up. Now the problem is, when I rotate my device, The popover remains on the position it was drawn while the rest of the view adjusts according to the rotation. In simple words, when device is rotated with the popover drawn (popping up from UIButton), the popover doesn't move along with the UIButton. If I close the popover and again draw it from the button, It draws correctly above the button. The problem only comes when rotating with the popover popped up.
Please help ASAP.
Thank you :)
You can hide popover before orientation will change and show it again with new coords right after.

Navbar height is too small after coming back from a landscape view

I have an app with both a tabbar and a navbar.
I load onto my view which has a menu of 3 views. 1 landscape and 2 portrait.
I go onto a portrait view and the navbar is perfect.
I go into my landscape view and it acts like i want it to.
When I go back to my portrait view the navbar is now to small.
When I enter my landscape view this code gets called:
-(void) viewWillAppear:(BOOL)animated
{
NSLog(#" viewWillAppear ");
[super viewWillAppear:animated];
appDelegate = (iGeo2AppDelegate *)[[UIApplication sharedApplication] delegate];
_originalTransform = [[appDelegate tabBarController].view transform];
_originalBounds = [[appDelegate tabBarController].view bounds];
_originalCenter = [[appDelegate tabBarController].view center];
CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(90));
landscapeTransform = CGAffineTransformTranslate (landscapeTransform, +80.0, +80.0);
[self.tabBarController.view setTransform:landscapeTransform];
self.tabBarController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.tabBarController.view.bounds = CGRectMake(0.0, 0.0, 480.0, 320.0);
self.tabBarController.view.center = CGPointMake (240.0, 160.0);
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeRight;
}
Then as I leave my landscape view this code gets called to reset everything:
-(void) viewWillDisappear:(BOOL)animated
{
NSLog(#" viewWillDisappear ");
self.navigationController.navigationBarHidden = NO;
[super viewWillDisappear:animated];
[[appDelegate tabBarController].view setTransform:_originalTransform];
[[appDelegate tabBarController].view setBounds:_originalBounds];
[[appDelegate tabBarController].view setCenter:_originalCenter];
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationPortrait;
}
Can anybody advise as to what I need to do to reset the navbar to the correct height?
Many Thanks,
-Code
I got the same Problem with hidden bars and custom buttons. It is not really an answer but a work around. As danipralea mentioned you have to hide them and show them again and it works. Maybe Apple is aware of this issue, but they haven't fixed it yet.
It's not the best but for me it worked…

Present formsheet modal ViewController using horizontal flip

I currently show a modal UIViewController in the formSheet style that was presented with the coverVertical animation. I am trying to present another modal UIViewController from it, using currentContext for the presentation style and flipHorizontal for the animation style. It does work, but there is a solid white white background behind the flip as it occurs. Any advice on how to successfully present another formsheet modal UIViewController using the flipHorizontal style from a pre-existing modal UIViewController in the formSheet style would be appreciated please! Thanks
Code:
// Loading of first modalVC
- (void)showModalVC {
Modal1VC *modal1VC = [[Modal1VC alloc] init];
modal1VC.modalPresentationStyle = UIModalPresentationFormSheet;
modal1VC.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:modal1VC animated:YES];
[modal1VC release];
modal1VC.view.superview.bounds = CGRectMake(0, 0, 400, 400);
}
// Loading of second modalVC in Modal1VC
- (void)buttonTapped:(id)sender {
UIViewController *modal2VC = [[UIViewController alloc] init];
modal2VC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
modal2VC.modalPresentationStyle = UIModalPresentationCurrentContext;
modal2VC.view.backgroundColor = [UIColor greenColor];
[self presentModalViewController:modal2VC animated:YES];
[modal2VC release];
modal2VC.view.superview.bounds = CGRectMake(0, 0, 400, 400);
}
UIViewController *presentingViewController = //allocate your VC
[modalViewController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
UIViewController *modalViewController = //your modal VC
[presentingViewController presentModalViewController:modalViewController animated:YES];
This is all the code you need ;)
It sounds like your transition is working, but the white background shown on the flip transition is the problem?
During the flip transition, the white background that is shown is actually the window. You can change the color that is shown by setting the backgroundColor property of window in the AppDelegate.
That would seem to be a limitation of presentModalViewController:animated:.
Instead of [self presentModalViewController:modal2VC animated:YES], you may be able to get the effect you want using UIView animations. Perhaps something like this:
[UIView transitionWithView:self.view.window
duration:0.3
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^{
[self presentModalViewController:modal2VC animated:NO];
} completion:NULL];