How can I fix the UIPopoverController is deprecated warning?
You no longer need UIPopoverController
for presenting a view controller.
Instead you can set the modalPresentationStyle
of view controller to UIModalPresentationPopover
.
You can use the following code for that:
avc.modalPresentationStyle = UIModalPresentationPopover;
avc.popoverPresentationController.sourceView = theButton;
[self presentViewController:avc animated:YES completion:nil];
UIModalPresentationPopover
In a horizontally regular environment, a
presentation style where the content is displayed in a popover view.
The background content is dimmed and taps outside the popover cause
the popover to be dismissed. If you do not want taps to dismiss the
popover, you can assign one or more views to the passthroughViews
property of the associated UIPopoverPresentationController object,
which you can get from the popoverPresentationController property.In a horizontally compact environment, this option behaves the same as
UIModalPresentationFullScreen.Available in iOS 8.0 and later.
Reference UIModalPresentationStyle Reference
You need to set either sourceView
or barButtonItem
property, else it will crash with the following message:
*** Terminating app due to uncaught exception 'NSGenericException', reason: 'UIPopoverPresentationController (***) should have a non-nil
sourceView or barButtonItem set before the presentation occurs.'
For anchoring the popover arrow correctly, you need to specify the sourceRect
property also.
avc.modalPresentationStyle = UIModalPresentationPopover;
avc.popoverPresentationController.sourceView = self.view;
avc.popoverPresentationController.sourceRect = theButton.frame;
[self presentViewController:avc animated:YES completion:nil];
Refer sourceView and sourceRect for more details.
UIPopoverController deprecated
UIModalPresentationPopover
UIModalPresentationPopover is replacement of UIPopoverController
.
Available in iOS 8.0 and later.
ModalViewController *modal = [[ModalViewController alloc] init];
modal.modalPresentationStyle = UIModalPresentationPopover;
modal.transitioningDelegate = self;
modal.popoverPresentationController.sourceView = self.view;
modal.popoverPresentationController.sourceRect = CGRectZero;
modal.popoverPresentationController.delegate = self;
[self presentViewController:modal animated:YES completion:nil];
Or else You can use below link.
UIPopoverPresentationController
iOS 7 - UIPopoverController : deprecated arrows?
This looks like a bug in the documentation. Arrows were probably on the chopping block by designers but the decision was reverted in usability testing.
If you look in UIPopoverController.h
, you don't see any deprecation attributes (e.g. NS_AVAILABLE_IOS
or NS_DEPRECATED_IOS
):
/* Returns the direction the arrow is pointing on a presented popover. Before
presentation, this returns UIPopoverArrowDirectionUnknown.
*/
@property (nonatomic, readonly) UIPopoverArrowDirection popoverArrowDirection;
Contrast this with -[NSString sizeWithFont:constrainedToSize:]
:
- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size NS_DEPRECATED_IOS(2_0, 7_0, "Use -boundingRectWithSize:options:attributes:context:");
These attributes are important because they generated the compiler warnings and errors. They are also maintained by the people who actually code the UI.
Combining the lack of attributes with the fact that the behavior you see directly contradicts the documentation, you really shouldn't worry about it. If it really bothers you, can file a bug report.
UIPopoverController trouble
Your code is quite strange.
For example why do you create a view and do you add it to the content view of your popover?
Then, you have to make attention to memory leaks. There a lot of them in your code.
That said, here a simple example for display a UIViewcontroller
within a UIPopoverController
.
- (IBAction)yourAction:(id)sender
UIButton* senderButton = (UIButton*)sender;
UIViewController* yourController = [[UIViewController alloc] init];
UIPopoverController* pop = [[UIPopoverController alloc] initWithContentViewController:yourController];
pop.delegate = self;
pop.popoverContentSize = CGSizeMake(300, 300);
self.popover = pop;
[pop presentPopoverFromRect:senderButton.bounds inView:senderButton permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
pop.passthroughViews = nil;
[yourController release];
[pop release];
}
where self.popover
is a @property
with a retain policy. In this manner in UIPopoverControllerDelegate
methods (or wherever you want), you can release your popover and/or dismiss it.
Hope it helps.
P.S. Check the code because I've written by hand.
Edit
Usually when you create a popover, its content view controller is or a custom UIViewController
or a UINavigationController
(in this case you want to take advantage of its navigation bar).
For example, instead of the simple UIViewController
, you could create a custom one
//.h
@interface CustomUIViewController : UIViewController
@end
//.m
@implementation CustomUIViewController
// other code here...
- (void)viewDidLoad
{
// here you are sure that the view has been loaded in memory (alternatively override loadView method), so
UIView* greenView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
green.backgroundColor = [UIColor greenColor];
[self.view addSubview:greenView];
[greenView release];
}
@end
and use it within a a popover
- (IBAction)yourAction:(id)sender
UIButton* senderButton = (UIButton*)sender;
CustomUIViewController* yourController = [[CustomUIViewController alloc] init];
// same as before...
}
iOS Popover Is Displaying On TextView
I am not sure why this was occurring but the quick solution is to move the frame where you want it. I did this with the code below:
let absoluteframe = patientTextField!.convertRect(patientTextField!.frame, fromView: self.scrollView)
popoverViewController.popoverPresentationController!.sourceRect = CGRectMake(absoluteframe.minX + 60,absoluteframe.minY + 20,0,0)
First, you need to get the frame for the item the popup is anchored to. Then you can set the sourceRect by updating the rectangle position with their current x and y values. Hope this helps anyone with similar problems!
Related Topics
How to Xcodebuild a Static Library with Bitcode Enabled
Xcode6: Run Two Instances of the Simulator
Using Core Data, Icloud and Cloudkit for Syncing and Backup and How It Works Together
How to Add a Container View Programmatically
How Do We Create a Bigger Center Uitabbar Item
If No Table View Results, Display "No Results" on Screen
iOS Swift Multiple Dimension Arrays - Compiliing Takes Ages. What Should I Change
Coca Pod Chart Not Appearing (Swift4)
How to Disable the Network in iOS Simulator
Specifying One Dimension of Cells in Uicollectionview Using Auto Layout
Fixing Xcode 9 Issue: "iPhone Is Busy: Preparing Debugger Support for Iphone"
I Get Conflicting Provisioning Settings Error When I Try to Archive to Submit an iOS App
Itsappusesnonexemptencryption Export Compliance While Internal Testing
Cocoa Touch: How to Change Uiview's Border Color and Thickness
How to Prevent from Scrolling Uitableview Up When Nsfetchedresultscontroller Add New Record
iOS Localization: Unicode Character Escape Sequences, Which Have the Form '\Uxxxx' Does Not Work