RTAlertView 0.0.7

RTAlertView 0.0.7

测试已测试
语言语言 Objective CObjective C
许可协议 MIT
发布日期最后发布日期Dec 2014

Roland Tecson 维护。



  • Roland Tecson

iOS 7 和 iOS 6 的 UIAlertView 替代方案

亮点

  • 可定制的警报视图,外观与 iOS 7 系统警报视图完全相同
  • 支持 iOS 6 和 iOS 7
  • 与 UIAlertView 相同的 API
  • iOS 7 中的半透明高斯模糊背景(iOS 6 中为半透明白色背景)
  • 当应用转到后台时,关闭警报视图(可配置)
  • 当警报视图显示时,降低其他可见控件的 tintColor
  • 类似于 UIAlertView 的太空翘曲效果
  • 支持 Swift 桥接(见 Swift 桥接

描述

RTAlertView 是一个可自定义的警报视图,外观与 iOS 7 系统警报视图完全相同。它实现了与 UIAlertView 相同的接口,但还允许您自定义警报视图中使用的字体和颜色。

RTAlertView

该警报使用自动布局将自己定位在屏幕中央,并在设备方向改变或键盘显示时自动调整其位置。

以下属性,除了在 UIAlertView 中定义的属性外,还在 RTAlertView 中定义

@property (strong, nonatomic) UIColor *titleColor;
@property (strong, nonatomic) UIFont *titleFont;
@property (strong, nonatomic) UIColor *messageColor;
@property (strong, nonatomic) UIFont *messageFont;
@property (strong, nonatomic) UIColor *cancelButtonColor;
@property (strong, nonatomic) UIFont *cancelButtonFont;
@property (strong, nonatomic) UIColor *otherButtonColor;
@property (strong, nonatomic) UIFont *otherButtonFont;
@property (strong, nonatomic) UIFont *textFieldPlaceholderFont;

如果没有指定这些属性的值,则将使用与 UIAlertView 相同的默认颜色和字体。

按钮的高亮颜色自动从按钮的颜色派生。

RTAlertView one button highlighted

与 UIAlertView 类似,如果定义了两个按钮,则它们将并排放置。

RTAlertView two buttons

如果定义了三个或更多按钮,则将垂直排列。

RTAlertView four buttons

与 UIAlertView 类似,如果 alertViewStyle 设置为 UIAlertViewStyleSecureTextInputUIAlertViewStylePlainTextInput,则将显示一个文本字段。

RTAlertView single text field

如果 alertViewStyle 设置为 UIAlertViewStyleLoginAndPasswordInput,则将显示两个文本字段。

RTAlertView double text fields

默认情况下,RTAlertView 将在应用转到后台时自动关闭。这是根据苹果的建议进行的,以确保当应用恢复时,用户不会遇到不在上下文中的警报消息。您可以通过将该属性 dismissesWhenAppGoesToBackground 设置为 NO(默认值为 YES)来覆盖此行为。

RTAlertView 会降低您的应用中所有可见控件(至少是支持它的控件)的 tintColor,类似于 iOS 7 的 UIAlertView。RTAlertView 还实现了 iOS 7 UIAlertView 中的空间翘曲效果。

Swift桥接

Swift不支持直接桥接到Objective-C的变量参数方法。不幸的是,UIAlertView的指定初始化器(因此RTAlertView作为尝试镜像UIAlertView公共API的结果)是一个变量参数方法。

    - (id)initWithTitle:(NSString *)title
                message:(NSString *)message
               delegate:(id)delegate
      cancelButtonTitle:(NSString *)cancelButtonTitle
      otherButtonTitles:(NSString *)otherButtonTitles, ...;

为了解决这个问题,RTAlertView提供了一个备选初始化器,它接受一个固定数量的参数。这个初始化器只接受一个otherButtonTitle(如果不存在则可能为nil)。如果还需要额外的其他按钮标题,可以使用方法-addButtonTitle:

    - (id)initWithTitle:(NSString *)title
                message:(NSString *)message
               delegate:(id)delegate
      cancelButtonTitle:(NSString *)cancelButtonTitle
       otherButtonTitle:(NSString *)otherButtonTitle;

安装与使用

要在您的应用中使用,请使用Cocoapods进行安装

  1. 创建或编辑您的Podfile文件并添加以下行pod 'RTAlertView', '0.0.6'
  2. 执行pod install
  3. 将行#import <RTAlertView.h>添加到您的类中
  4. 请记住,从现在开始,在Xcode中打开的文件为<App>.xcworkspace文件(而不是<App>.xcodeproj文件)

要运行测试应用程序

  1. 克隆git仓库,git clone https://github.com/rtecson/RTAlertView.git
  2. cd RTAlertView
  3. 运行pod install,pod install(如果未安装cocoapods,请参见http://http://beta.cocoapods.org
  4. 运行Xcode 5(或更高版本)并打开RTAlertView.xcworkspace

用法

    // Alloc and init RTAlertView, button titles may also be set here
    RTAlertView *customAlertView = [[RTAlertView alloc] initWithTitle:@"Test"
                                                              message:@"Message here"
                                                             delegate:nil
                                                    cancelButtonTitle:nil
                                                    otherButtonTitles:nil];

    // Conforms to the RTAlertViewDelegate protocol
    customAlertView.delegate = self;

    // Default is UIAlertViewStyleDefault
    customAlertView.alertViewStyle = UIAlertViewStylePlainTextInput;

    // Add other buttons
    for (int i=0; i<kNumberOfOtherButtons; i++)
    {
        [customAlertView addButtonWithTitle:[NSString stringWithFormat:@"Button %d", i]];
    }

    // Add cancel button
    customAlertView.cancelButtonIndex = [customAlertView addButtonWithTitle:@"Done"];

    // Set button colours
    customAlertView.otherButtonColor = kCustomColor;
    customAlertView.cancelButtonColor = kCustomColor;

    // Display alert view
    [customAlertView show];

致谢

许可证

RTAlertView由Roland Tecson编写。它使用MIT许可证发布。

如果您的应用中使用RTAlertView,请给我一个简短的回复。我很乐意听取关于它的事情。