PKAlertController 是一个灵活、高度可自定义、多种视图过渡动画弹窗视图控制器。
PKAlertController
有标题和描述标签,您可以设置文本对齐。PKAlertDefaultTheme
的类。为了运行示例项目,请克隆存储库,然后首先从 Example 目录中运行 pod install
。
UIAlertAction
。使用以下导入使用它
#import <PKAlertController.h>
// Import this library.
#import <PKAlertController.h>
// Instantiate and configure a simple alert view controller with ok button.
PKAlertViewController *alertViewController = [PKAlertViewController simpleAlertControllerWithConfigurationBlock:^(PKAlertControllerConfiguration *configuration) {
configuration.title = @"Alert title";
configuration.message = @"Alert message";
configuration.preferredStyle = PKAlertControllerStyleAlert;
configuration.tintAdjustmentMode = UIViewTintAdjustmentModeDimmed; // Dimmed view's tint color.
}];
// Call present view controller as modal view controller goes with custom view controller transitional animation.
[self presentViewController:alertViewController animated:YES completion:nil];
设置弹窗中标题和信息的文本对齐
PKAlertViewController *alertViewConroller = [PKAlertViewController simpleAlertControllerWithConfigurationBlock:^(PKAlertControllerConfiguration *configuration) {
configuration.title = @"Alert title";
configuration.message = @"Alert message";
configuration.titleTextAlignment = NSTextAlignmentLeft;
configuration.messageTextAlignment = NSTextAlignmentLeft;
configuration.tintAdjustmentMode = UIViewTintAdjustmentModeDimmed;
}];
// Add common actions.
NSMutableArray *actions = [NSMutableArray array];
[actions addObject:[PKAlertAction cancelAction]];
[actions addObject:[PKAlertAction okActionWithHandler:^(PKAlertAction *action, BOOL closed) {
if (closed) {
NSLog(@"Dismmied the PKAlertViewController!");
} else {
NSLog(@"Tap the ok button!");
}
}]];
// Add a custom action.
[actions addObject:[PKAlertAction actionWithTitle:@"Close" handler:^(PKAlertAction *action, BOOL closed) {
if (closed) {
NSLog(@"Dismmied the PKAlertViewController!");
} else {
NSLog(@"Tap the close button!");
}
}]];
PKAlertViewController *alertViewConroller = [PKAlertViewController simpleAlertControllerWithConfigurationBlock:^(PKAlertControllerConfiguration *configuration) {
configuration.message = @"Alert message";
// Set to the alert configuration.
[configuration addActions:actions];
}];
// Create a custom view
UINib *nib = [UINib nibWithNibName:NSStringFromClass([PKCustomView class]) bundle:[NSBundle mainBundle]];
PKCustomView *customView = [[nib instantiateWithOwner:nil options:nil] firstObject];
// Set the custom view
PKAlertViewController *alertViewConroller = [PKAlertViewController simpleAlertControllerWithConfigurationBlock:^(PKAlertControllerConfiguration *configuration) {
configuration.customView = customView;
}];
如果你在自定义视图中实现了 PKAlertViewLayoutAdapter
,则有机会应用于 PKAlertViewController
视图组件。
#pragma mark - <PKAlertViewLayoutAdapter>
- (void)applyLayoutWithAlertComponentViews:(NSDictionary *)views {
// apply autolayouts to a PKAlertViewController's component.
// contained views in views
// key = `PKAlertRootViewKey`, value = PKAlertViewController's root view.
// key = `PKAlertContentViewKey`, value = PKAlertViewController's content container view.
// key = `PKAlertScrollViewKey`, value = PKAlertViewController's content scroll view.
// key = `PKAlertTopLayoutGuideKey`, value = Top layout guide object.
UIView *contentView = PKAlertGetViewInViews(PKAlertContentViewKey, views);
NSMutableArray *contentConstraints = @[
[NSLayoutConstraint constraintWithItem:self.headerImageView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationLessThanOrEqual toItem:
contentView attribute:NSLayoutAttributeTop multiplier:1 constant:0],
].mutableCopy;
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self.headerImageView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:contentView attribute:NSLayoutAttributeTop multiplier:1 constant:0];
constraint.priority = UILayoutPriorityDefaultHigh;
[contentConstraints addObject:constraint];
[contentView addConstraints:contentConstraints];
}
- (void)visibleSizeInAlertView {
// Adjust visible size in alert view.
CGSize size = self.layoutSize;
size.height -= 44;
return size;
}
PKAlertViewControler 的样式。请参阅示例项目以使用样式。
typedef NS_ENUM (NSInteger, PKAlertControllerStyle) {
PKAlertControllerStyleAlert = 0, // Normal style that the size about same as the size of UIAlertView.
PKAlertControllerStyleFlexibleAlert, // A style of flexible size.
PKAlertControllerStyleFullScreen, // A style of full screen.
};
如果你使用全屏样式并将其推送到导航控制器,请编写以下代码
// Create a PKAlertViewController
PKAlertViewController *viewController = [PKAlertViewController instantiateOwnerViewController];
// Create a custom view
UINib *nib = [UINib nibWithNibName:NSStringFromClass([PKCustomView class]) bundle:[NSBundle mainBundle]];
PKCustomView *customView = [[nib instantiateWithOwner:nil options:nil] firstObject];
// Configure behavier
PKAlertControllerConfiguration *configuration = viewController.configuration;
configuration.customView = customView;
configuration.preferredStyle = PKAlertControllerStyleFullScreen; // Set the full screen style.
configuration.statusBarAppearanceUpdate = NO; // Prevent the status bar appearance updating.
viewController.view.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds));
viewController.title = @"Full screen style";
// Push alert to a navigation controller.
[self.navigationController pushViewController:viewController animated:YES];
要使用动画样式,请使用以下代码
// Set the custom view
PKAlertViewController *alertViewConroller = [PKAlertViewController simpleAlertControllerWithConfigurationBlock:^(PKAlertControllerConfiguration *configuration) {
// Set the presenting transition style
configuration.presentationTransitionStyle = PKAlertControllerPresentationTransitionStyleScale;
// Set the dismissing transition style
configuration.dismissTransitionStyle = PKAlertControllerDismissStyleTransitionZoomOut;
}];
// Set the custom view
PKAlertViewController *alertViewConroller = [PKAlertViewController simpleAlertControllerWithConfigurationBlock:^(PKAlertControllerConfiguration *configuration) {
configuration.tintAdjustmentMode = UIViewTintAdjustmentModeAutomatic; // Default: UIViewTintAdjustmentModeDimmed;
}];
动作按钮便捷构造器,其一
+ (instancetype)actionWithTitle:(NSString *)title handler:(PKActionHandler)handler;
+ (instancetype)cancelAction;
+ (instancetype)cancelActionWithHandler:(void(^)(PKAlertAction *action, BOOL closed))handler;
+ (instancetype)okAction;
+ (instancetype)okActionWithHandler:(void(^)(PKAlertAction *action, BOOL closed))handler;
+ (instancetype)doneAction;
+ (instancetype)doneActionWithHandler:(void(^)(PKAlertAction *action, BOOL closed))handler;
警报视图控制器的便捷构造器,其一
+ (instancetype)instantiateOwnerViewController;
+ (instancetype)alertControllerWithConfigurationBlock:(PKAlertControllerConfigurationBlock)configurationBlock;
+ (instancetype)simpleAlertControllerWithConfigurationBlock:(PKAlertControllerConfigurationBlock)configurationBlock;
+ (instancetype)alertControllerWithConfiguration:(PKAlertControllerConfiguration *)configuration;
如果你喜欢过程化编写
PKAlertControllerConfiguration *configuration = [[PKAlertControllerConfiguration alloc] init];
configuration.message = @"Alert message";
configuration.actionControlHeight = 60;
[configuration addAction:[PKAlertAction cancelAction]];
PKAlertViewController *viewController = [PKAlertViewController alertControllerWithConfiguration:configuration];
或者使用建造者设计模式
PKAlertViewController *viewController = [PKAlertViewController alertControllerWithConfigurationBlock:^(PKAlertControllerConfiguration *configuration) {
configuration.message = @"Alert message";
configuration.actionControlHeight = 60;
[configuration addAction:[PKAlertAction cancelAction]];
}];
Satoshi Ohki,[email protected]
PKAlertController 在 MIT 许可证下提供。有关更多信息,请参阅 LICENSE 文件。