Application crashes on changing orientation while popover is displayed. - objective-c

Hi friends,
On clicking the toolbar button, my popover view appears. When i rotate the simulator to landscape orientation , following error message is displayed :
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIPopoverController presentPopoverFromRect:inView:permittedArrowDirections:animated:]: Popovers cannot be presented from a view which does not have a window.'
*** First throw call stack:
(0x1b69052 0x1868d0a 0x1b11a78 0x1b119e9 0x9ae72e 0x9affcb 0x9b26eb 0x281a39 0x1b34885 0x1b347a8 0x1c61aa 0x5490d8 0x54d499 0x54d584 0x11ede00 0x15ff4f0 0x1aa0833 0x1a9fdb4 0x1a9fccb 0x209a879 0x209a93e 0x51ca9b 0x29dd 0x2955)
Code to handle my button event is :
-(IBAction)name:(id)sender
{
if ([_popover isPopoverVisible])
{
[_popover dismissPopoverAnimated:YES];
}
else {
task * content = [[task alloc] init];
content.navigationItem.title =#"name";
[content setDelegate:self];
navController = [[UINavigationController alloc] initWithRootViewController: content];
_popover = [[UIPopoverController alloc]
initWithContentViewController:navController];
_popover. popoverContentSize=CGSizeMake(350, 480);
[_popover presentPopoverFromBarButtonItem:sender
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}
Thanks..

Here you need to add code to dismiss the popover from the view when you changing the orientation.
if ([_popover isPopoverVisible])
{
[_popover dismissPopoverAnimated:YES];
}
So, put your code in the method shouldAutorotateToInterfaceOrientation() when changing the orientation and it will work.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if ([_popover isPopoverVisible])
{
[_popover dismissPopoverAnimated:YES];
}
return YES;
}
If you have any more questions then feel free to ask.

Related

UIImagePickerController added as subView and getting memory error

UIImagePickerController added on view as subView and open camera 2-3 time app get crash.
"Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates."
"Received memory warning"
UIImagePickerController * imagePickerController = [UIImagePickerController new];
imagePickerController.view.tag=view.tag;
imagePickerController.delegate = self;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
[imagePickerController.view setFrame:CGRectMake(0, 0, SCREEN_WIDTH, KCameraViewHeight)];
[self.view addSubview:imagePickerController.view];"
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker.view removeFromSuperview];
[picker removeFromParentViewController];
}

Statusbar background was nil,after presenting the SKStoreProductViewController in ios 8.4

I'm using SKStoreProductViewController for opening the appstore in my app. it was presenting successfully,but the statusbar in it was not showing, Navbar looks like a 44pixels height. But it won't happens in any iOS 8.3v in any device.
This situation happens only in all iOS 8.4v devices.
In my plist UIViewControllerBasedStatusbarAppearance is set to NO. i tried with Yes also, but no use.
the Red color in status bar is superview navigationbar color.
NOTE: i'm presenting the SKStoreProductViewController from my ParentViewController.
Any help will be greatly appreciated.
I solved this by adding statusBar as UIView Component, Its a workaround to the solution. I also faced similar issue.
SKStoreProductViewController *_apsvc = [[SKStoreProductViewController alloc]init];
_apsvc.delegate = self;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
UIView *addStatusBar = [[UIView alloc] init];
//change this to match your navigation bar
UIViewController* viewController = [UIApplication sharedApplication].keyWindow.rootViewController;
addStatusBar.frame = CGRectMake(0, 0, viewController.view.frame.size.width, 20);
addStatusBar.backgroundColor =[UIColor whiteColor];
}
UIViewController* viewController = [UIApplication sharedApplication].keyWindow.rootViewController;
if (viewController)
{
[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:_apsvc animated:YES completion:^()
{
[_apsvc loadProductWithParameters:#{SKStoreProductParameterITunesItemIdentifier : AppID}
completionBlock:^(BOOL result, NSError *error) {
if(error)
{
....
}
}];
[viewController.view addSubview:addStatusBar];
}];
}
}
-(void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController
{
[addStatusBar removeFromSuperview];
if (viewController)
{
[viewController dismissViewControllerAnimated:YES completion:nil];
}
}

Statusbar overlapping on UIImagepickercontroller's Camera View in iOS 7 and iOS 8

im using UIImagepickercontroller to take photo and choose existing photos, when the camera view appears, the top portion on camera was covered by the white status bar. How can i remove the status bar or hide the status bar. Many of them saying its a ios 7 bug, it has been fixed in ios 7.1, but still i'm facing this problem.
This is my code to show the imagepickerController
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = (id)self;
imagePicker.sourceType = (buttonIndex == actionSheet.firstOtherButtonIndex ?
UIImagePickerControllerSourceTypeCamera : UIImagePickerControllerSourceTypePhotoLibrary);
[self.viewController presentViewController:imagePicker animated:YES completion:nil];
Also i tried by with below codes to hide statusbar
- (void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[[UIApplication sharedApplication] setStatusBarHidden:YES];
}
This
-(BOOL) prefersStatusBarHidden {
return YES;
}
And This in Delegate
[UIApplication ShareApplication]SetStatusbarHidden:YES];
Right now i'm working with ios8, and this behaviour happening in all ios version.
Please help me out.
Actually the problem is in my rootviewcontroller i'm creating a Statusbar using UIView and setting its constraints for landscape and portrait programmatically
- (void)setTopBar {
if(topBar == nil && [[UIApplication sharedApplication] keyWindow] != nil) {
topBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
topBar.backgroundColor = navController.view.backgroundColor;
topBar.tag = topBarViewTag;
UIView *containerView = [[UIApplication sharedApplication] keyWindow];
[containerView addSubview:topBar];
[containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[view(20)]" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:#{#"view" : topBar}]];
[containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"|[view]|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:#{#"view" : topBar}]];
[containerView layoutSubviews];
}
}
so when i commented the above method in ViewwillAppear, status bar where not showing up, so the problem is with my code.
if anyone face the same issue, i suggest you to have a look on your RootViewcontroller, which acts a navigation to all other classes.

Changing size of UIImagePicker presented via UIPopOver (iPad)

I am trying to change the size of a UIImagePicker which I am presenting via a UIPopOver. The code is shown below. The issue is that the PopOver size doesn't display properly - it briefly flashes up in the correct size, then animates down to the usual default size.
Can anybody advise me on how I can change the size of the popover ?
- (void)showImagePickerForPhotoLibrary {
NSLog(#"Picker");
imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePickerController.contentSizeForViewInPopover = CGSizeMake(768, 1000);
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
Class cls = NSClassFromString(#"UIPopoverController");
if (cls != nil) {
popoverController = [[UIPopoverController alloc] initWithContentViewController:imagePickerController];
//popoverController.popoverContentSize = CGSizeMake(768, 1000);
[popoverController presentPopoverFromRect:selectedRect inView:self.view permittedArrowDirections:4 animated:YES];
}
}
else {
[app.mainViewController presentModalViewController:imagePickerController animated:YES];
}
}
I'm not sure this is the most elegant solution, however it seems to work fine for me:
You can embed the UIImagePickerController's view into a "container" UIViewController's view.
- (void)showImagePickerForPhotoLibrary {
NSLog(#"Picker");
imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
UIViewController *containerController = [[UIViewController alloc] init];
containerController.contentSizeForViewInPopover = CGSizeMake(768, 1000);
[containerController.view addSubview:imagePickerController.view];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
Class cls = NSClassFromString(#"UIPopoverController");
if (cls != nil) {
popoverController = [[UIPopoverController alloc] initWithContentViewController:containerController];
[popoverController presentPopoverFromRect:selectedRect inView:self.view permittedArrowDirections:4 animated:YES];
[imagePickerController.view setFrame:containerController.view.frame];
}
}
else {
[app.mainViewController presentModalViewController:containerController animated:YES];
}
}
Hope this helps
EDIT: for some strange reason, the frame does indeed change when in landscape right.
To come over this, you need to set the image picker's view's frame after you present the popover.
I have edited the code above to show this.
Also, in your controller, add this:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
[imagePickerController.view setFrame:imagePickerController.view.superview.frame];
}
When I tried Mutix's solution the popover showed just the navigation bar with an empty white view.
My way around it was to add imagePickerController as a child of containerController and not just adding the picker's view.
Mainly it means to replace this line:
[containerController.view addSubview:imagePickerController.view];
with these lines:
[containerController addChildViewController:imagePickerController];
[containerController.view addSubview:imagePickerController.view];
[imagePickerController didMoveToParentViewController:containerController];
In addition, when using this approach, I didn't need any special handling for landscape mode.

Uiimagepicker For showing camera

Hi everyone I am trying to make a camera app. I am doing this as
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
where picker is the object of UIimagepicker Controller.
But when is run the code then the application terminates showing the error.
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Source type 1 not available'
I am using this on simulator. I know that it is not possible to check camera in simulator, but we can test for that. I think it might be that because camera is not available thats why it's terminating.
But I saw an application with the same code but that was running on the simulator, just showing the camera view.
Just help me out how to resolve this problem. And moreover how can I put my custom view to the camera in that app?
You need to check if the device has camera available before setting the sourcetype.
The following can check if device has camera available.
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
}
You can't check the camera functionality from your simulator. You can assign UIImagePickerControllerSourceTypePhotoLibrary as the sourceType to test on simulator.
Swift 2.2
if UIImagePickerController.isSourceTypeAvailable(.Camera) {
imagePicker.delegate = self
imagePicker.sourceType = .Camera
presentViewController(imagePicker, animated: true, completion: nil)
} else {
print("The device has no camera")
}
Saved photos album
if UIImagePickerController.isSourceTypeAvailable(.SavedPhotosAlbum) {
imagePicker.delegate = self
imagePicker.sourceType = .SavedPhotosAlbum
imagePicker.allowsEditing = false
self.presentViewController(imagePicker, animated: true, completion: nil)
}
Put Below Code Where Exception Occures. Remember You Need To Implement navigationController
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIAlertController *alertView = [UIAlertController alertControllerWithTitle:#"ERROR" message:#"No Camera Avalible" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ok = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
[self dismissViewControllerAnimated:alertView completion:nil];
}];
[alertView addAction:ok];
[self.navigationController presentViewController:alertView animated:YES completion:nil];
}