我正在寻找iPhone中的气泡弹出窗口,并希望将其制作成类似于iOS 5阅读器功能的样式:
经过一些研究,我发现了WEPopover和FPPopover,但我想知道是否有类似于此API内置于iPhone SDK中。
你可以用一些自定义的艺术品制作一个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) {}];
如果你想使用customView.layer
,请不要忘记导入#import <QuartzCore/QuartzCore.h>
。
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.
}
接下来,在您想要显示它的方法中实例化您新创建的子类,并将两个更多的值分配给sourceView
和sourceRect
。看起来像这样:
之后,在您希望展示它的方法中实例化您新创建的子类,并且给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];
就是这样,一个漂亮、整洁而模糊的弹出框。
self.modalPresentationStyle = UIModalPresentationPopover;
应该放在前面,否则 popoverPresentationController
将为空。 - Vitalii Gozhenko