iOS 7 和 iOS 6 的 UIAlertView 替代方案
RTAlertView 是一个可自定义的警报视图,外观与 iOS 7 系统警报视图完全相同。它实现了与 UIAlertView 相同的接口,但还允许您自定义警报视图中使用的字体和颜色。
该警报使用自动布局将自己定位在屏幕中央,并在设备方向改变或键盘显示时自动调整其位置。
以下属性,除了在 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 相同的默认颜色和字体。
按钮的高亮颜色自动从按钮的颜色派生。
与 UIAlertView 类似,如果定义了两个按钮,则它们将并排放置。
如果定义了三个或更多按钮,则将垂直排列。
与 UIAlertView 类似,如果 alertViewStyle
设置为 UIAlertViewStyleSecureTextInput
或 UIAlertViewStylePlainTextInput
,则将显示一个文本字段。
如果 alertViewStyle
设置为 UIAlertViewStyleLoginAndPasswordInput
,则将显示两个文本字段。
默认情况下,RTAlertView 将在应用转到后台时自动关闭。这是根据苹果的建议进行的,以确保当应用恢复时,用户不会遇到不在上下文中的警报消息。您可以通过将该属性 dismissesWhenAppGoesToBackground
设置为 NO
(默认值为 YES
)来覆盖此行为。
RTAlertView 会降低您的应用中所有可见控件(至少是支持它的控件)的 tintColor,类似于 iOS 7 的 UIAlertView。RTAlertView 还实现了 iOS 7 UIAlertView 中的空间翘曲效果。
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进行安装
Podfile
文件并添加以下行pod 'RTAlertView', '0.0.6'
pod install
#import <RTAlertView.h>
添加到您的类中<App>.xcworkspace
文件(而不是<App>.xcodeproj
文件)要运行测试应用程序
git clone https://github.com/rtecson/RTAlertView.git
cd RTAlertView
pod install
(如果未安装cocoapods,请参见http://http://beta.cocoapods.org)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,请给我一个简短的回复。我很乐意听取关于它的事情。