PKAlertController 0.6.0

PKAlertController 0.6.0

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布最后发布2016年5月

Satoshi Ohki 维护。




  • Satoshi Ohki

Dependency Status Reference Status

PKAlertController 是一个灵活、高度可自定义、多种视图过渡动画弹窗视图控制器。

屏幕截图

Default Theme

White Blue Theme

View Controller transitions

特性

  • PKAlertController 有标题和描述标签,您可以设置文本对齐。
  • 基于 UIViewController 构建,因此您可以将其作为模态视图控制器调用,或添加到一些视图控制器中。
  • 有许多自定义视图控制器过渡动画。
  • 有一些布局样式,它的大小与 UIAlertView 大致相同,有灵活大小和全屏大小。
  • 要自定义 UI 颜色主题,使用继承自 PKAlertDefaultTheme 的类。
  • 视图内容可自定义,可以设置与 UINavigationItem 的 titleView 相同的自定义视图。

用法

为了运行示例项目,请克隆存储库,然后首先从 Example 目录中运行 pod install

要求

  • 支持的构建目标 - iOS 8.2 (Xcode 6.2, Apple LLVM 编译器 6.0)
  • 支持的部署目标 - iOS 7.1

安装

组件

  • PKAlertViewController - 弹窗的根视图控制器
  • PKAlertAction - 表示可以在点击弹窗中的按钮时执行的操作的对象,类似于 UIAlertAction
  • PKAlertControllerConfiguration - 定义弹窗行为的对象
  • PKAlertThemeManager - 管理UI颜色主题的对象

示例

导入

使用以下导入使用它

#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;
}

PreferredStyle

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;
}];

修改 PKAlert 视图的 tintAdjustmentMode

// Set the custom view
PKAlertViewController *alertViewConroller = [PKAlertViewController simpleAlertControllerWithConfigurationBlock:^(PKAlertControllerConfiguration *configuration) {
    configuration.tintAdjustmentMode = UIViewTintAdjustmentModeAutomatic;   // Default: UIViewTintAdjustmentModeDimmed;
}];

PKAlertAction

动作按钮便捷构造器,其一

+ (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;

PKAlertViewController

警报视图控制器的便捷构造器,其一

+ (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 文件。