ios 如何在 iPhone 应用程序中创建弹出框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11547969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create popover in iPhone app?
提问by Xtrician
I'm looking for popover in iPhone and i want to make it like iOS 5 Reader feature:
我正在 iPhone 中寻找 popover,我想让它像 iOS 5 Reader 功能一样:
After little research i found WEPopover and FPPopover but i'm looking if there anything like this API built-in iphone SDK.
经过很少的研究,我发现了 WEPopover 和 FPPopover,但我正在寻找是否有类似这个 API 内置 iphone SDK 的东西。
回答by Aleksander Azizi
You could make a UIView
with some custom artwork and display it with an animation on top of your view as a "popover" with some buttons like so:
您可以UIView
使用一些自定义艺术品制作一个,并在视图顶部显示动画作为“弹出框”,并带有一些按钮,如下所示:
UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(25, 25, 100, 50)]; //<- change to where you want it to show.
//Set the customView properties
customView.alpha = 0.0;
customView.layer.cornerRadius = 5;
customView.layer.borderWidth = 1.5f;
customView.layer.masksToBounds = YES;
//Add the customView to the current view
[self.view addSubview:customView];
//Display the customView with animation
[UIView animateWithDuration:0.4 animations:^{
[customView setAlpha:1.0];
} completion:^(BOOL finished) {}];
Don't forget to #import <QuartzCore/QuartzCore.h>
, if you want to use the customView.layer
.
不要忘记#import <QuartzCore/QuartzCore.h>
,如果您想使用customView.layer
.
回答by Soberman
Since iOS8 we are now able to create popovers, that will be the same on iPhone, as on iPad, which would be especially awesome for those who make universal apps, thus no need to make separate views or code.
从 iOS8 开始,我们现在可以创建弹出框,这在 iPhone 和 iPad 上都是一样的,这对于那些制作通用应用程序的人来说尤其棒,因此无需制作单独的视图或代码。
You can get the class as well as demo project here: https://github.com/soberman/ARSPopover
您可以在此处获取课程和演示项目:https: //github.com/soberman/ARSPopover
All you need to do is subclass UIViewController
, conform to the UIPopoverPresentationControllerDelegate
protocol and set desired modalPresentationStyle
along with the delegate
value:
您需要做的就是 subclass UIViewController
,符合UIPopoverPresentationControllerDelegate
协议并设置所需modalPresentationStyle
的delegate
值:
// This is your CustomPopoverController.m
@interface CustomPopoverController () <UIPopoverPresentationControllerDelegate>
@end
@implementation CustomPopoverController.m
- (instancetype)init {
if (self = [super init]) {
self.modalPresentationStyle = UIModalPresentationPopover;
self.popoverPresentationController.delegate = self;
}
return self;
}
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
return UIModalPresentationNone; //You have to specify this particular value in order to make it work on iPhone.
}
Afterwards, instantiate your newly created subclass in the method from which you want to show it and assign two more values to sourceView
and sourceRect
. It looks like this:
随后,实例化的方法,从要显示它并指定两个值,新创建的子类sourceView
和sourceRect
。它看起来像这样:
CustomPopoverController *popoverController = [[CustomPopoverController alloc] init];
popoverController.popoverPresentationController.sourceView = sourceView; //The view containing the anchor rectangle for the popover.
popoverController.popoverPresentationController.sourceRect = CGRectMake(384, 40, 0, 0); //The rectangle in the specified view in which to anchor the popover.
[self presentViewController:popoverController animated:YES completion:nil];
And there you have it, nice, neat blurred popover.
你有它,漂亮,整洁的模糊弹出框。