UIPopovercontroller doesnt remain on its position when iPad rotated - objective-c

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.

Related

How to move scrollView down 44x to make room for NavBar

I am using the following code to create my scrollview. I would like to move the scrollView down 44px to make room for my nav bar.
scroll = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
scroll.backgroundColor = [UIColor clearColor];
scroll.delegate = self;
image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Menu.png"]];
scroll.contentSize = image.frame.size;
[scroll addSubview:image];
scroll.minimumZoomScale = scroll.frame.size.width / image.frame.size.width;
scroll.maximumZoomScale = 2.0;
[scroll setZoomScale:scroll.minimumZoomScale];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleDoubleTap:)];
[doubleTap setNumberOfTapsRequired:2];
[scroll addGestureRecognizer:doubleTap];
self.view = scroll;
any help is appreciated.
Your code is correct, to manually create a frame do:
scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0,44,320,480)];
:)
This is for iPod and iphone View but this will let the scroll start at 44px
Cant you just add a scroll view in in interface builder as an outlet, place it underneath your nav bar.
Declare scroll view in .h
in .m under ViewDidLoad
[scroll setScrollEnabled:YES];
[scroll setContentSize: CGSizeMake(320, 1000)]; ////or whatever size you want here
You probably want to init it with a different frame... but it depends on where/how this nav bar is being created. If you have a view controller, and these are all subviews of the view controller's view, then you should be creating the objects in viewDidLoad, then just create the nav bar first, using self.view.bounds to obtain the initialization width. I assume you'll want to put UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin as the autoResizingMask here. Then if the scrollview is the rest of the view below the nav bar, you can create the frame for it using
CGRect scrollFrame = CGRectInset(self.view.bounds, 0, navbar.bounds.size.height)
Put an autoresizingMask of UIViewAutoresizingFlexibleWidth | UIViewAutoResizingFlexibleHeight on the scrollview.
If you are creating the views in a different place/way, then some of that might need modification. I was assuming your view is a nav bar at the top, x pixels tall (44 in this case but it doesn't and shouldn't matter in the context of setting the scrollview frame). and then a scrollview that fills the rest of the view.

iPad: UIModalPresentationFormSheet fails on landscape mode

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;
}];

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];

ipad: popover is not appearing at appropriate place

I have a situation here please help me out in this,
1) I have a Table with custom cells
2) Each cell has 2 search bars and 2 lables.
what I was attempting is suppose a user beginEditing a searchBar a popover should appear pointing that search bar.
I have implemented this but popover is not appearing over the desired searchBar moreover height of popover is also too long sometime
if (searchBar.tag==10) {
NSLog(#"Display date popover");
CGRect pickerFrame = CGRectMake(0,0,300,200);
UIViewController *tempDateViewController=[[UIViewController alloc] init];
UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:pickerFrame];
[datePicker addTarget:self action:#selector(pickerChanged:) forControlEvents:UIControlEventValueChanged];
[tempDateViewController.view addSubview:datePicker];
if(!currentPopover)
{
currentPopover=[[UIPopoverController alloc] initWithContentViewController:tempDateViewController];
}
else {
[currentPopover setContentViewController:tempDateViewController animated:YES];
}
tempDateViewController.contentSizeForViewInPopover=CGSizeMake(320, 300);
[datePicker release];
[tempDateViewController release];
[currentPopover presentPopoverFromRect:searchBar.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
Please help me to solve this one.
Thanx in advance.
The searchBar is inside a cell which is in a table view (which may be in some other view depending on your layout). The problem most likely is that self.view is not the direct parent of searchBar where you are calling this. So using the frame of the searchBar in self.view will give unexpected results.
Instead of using self.view and trying to figure out where the searchBar is relative to that, you can use the searchBar itself for the "inView" and for the "rect", use the searchBar's bounds:
[currentPopover presentPopoverFromRect:searchBar.bounds inView:searchBar
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
Next, to fix the height of the popover, try setting the popover's popoverContentSize instead of the view controller's contentSizeForViewInPopover:
//tempDateViewController.contentSizeForViewInPopover=CGSizeMake(320, 300);
currentPopover.popoverContentSize = CGSizeMake(320, 300);
Finally, a separate issue but, the minimum height for a datepicker should be 216, not 200:
CGRect pickerFrame = CGRectMake(0,0,300,216);

How to avoid corrupted borders when presenting a modal controller in a UIPopOverController

I'm having an issue when presenting a modal view controller within a popover using the modal presentation style UIModalPresentationCurrentContext.
Something strange is happening to the border of the popover, here are some before/after screenshots:
Before presenting the modal controller:
And now once the modal controller has been displayed:
See how the border width on the left and top has changed? When the modal controller is dismissed the border width stays corrupted but if the whole popover is dismissed it displays correctly until the modal controller is shown again.
Here is the code we're using in a sample project that we've created to demonstrate the problem:
- (IBAction)showModalViewButtonTapped:(id)sender
{
UIViewController *viewController = [[UIViewController alloc] init];
UIButton *dismissButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
dismissButton.frame = CGRectMake(30, 30, 100, 25);
[dismissButton setTitle:#"Dismiss" forState:UIControlStateNormal];
[dismissButton addTarget:self action:#selector(dismissButtonTapped) forControlEvents:UIControlEventTouchUpInside];
[viewController.view addSubview:dismissButton];
viewController.view.backgroundColor = [UIColor redColor];
viewController.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentModalViewController:viewController animated:YES];
[viewController release];
}
Has anyone seen this before? any ideas?