这个库提供了一个易于使用的类,可以在屏幕顶部显示小的通知视图。(类似于 Tweetbot)。
通知从屏幕顶部移动到导航条下方,并保持几秒钟,具体时间取决于显示文本的长度。要在时间用尽前关闭通知,用户可以将其向上滑动或直接点击。
已经为您设置好了 4 种不同的类型:成功、错误、警告、消息(查看截图)
添加不同设计的通知类型非常简单。将新类型添加到 notificationType 枚举中,将所需的设计属性添加到配置文件中,并在 TSMessagesView.m 中的 switch 案例中设置主题名称(在配置文件和图片中使用)。
查看示例项目以了解如何使用此库。 由于示例项目使用 Cocoapods,因此您必须打开工作区,而不是项目文件。
在 Twitter 上联系开发者: KrauseFx(Felix Krause)
将 TSMessageView 和 TSMessage 文件复制到您的项目中。同时复制 TSMessagesDesignDefault.json。
要显示通知,请使用以下代码
[TSMessage showNotificationWithTitle:@"Your Title"
subtitle:@"A description"
type:TSMessageNotificationTypeError];
// Add a button inside the message
[TSMessage showNotificationInViewController:self
title:@"Update available"
subtitle:@"Please update the app"
image:nil
type:TSMessageNotificationTypeMessage
duration:TSMessageNotificationDurationAutomatic
callback:nil
buttonTitle:@"Update"
buttonCallback:^{
NSLog(@"User tapped the button");
}
atPosition:TSMessageNotificationPositionTop
canBeDismissedByUser:YES];
// Use a custom design file
[TSMessage addCustomDesignFromFileWithName:@"AlternativeDesign.json"];
您可以为显示通知定义一个默认视图控制器
[TSMessage setDefaultViewController:myNavController];
您可以为显示通知定义一个默认视图控制器
[TSMessage setDelegate:self];
...
- (CGFloat)messageLocationOfMessageView:(TSMessageView *)messageView
{
return messageView.viewController...; // any calculation here
}
您可以在显示之前自定义一个消息视图,如设置透明度值或添加自定义子视图
[TSMessage setDelegate:self];
...
- (void)customizeMessageView:(TSMessageView *)messageView
{
messageView.alpha = 0.4;
[messageView addSubview:...];
}
您可以使用UIAppearance自定义消息视图元素
#import <TSMessages/TSMessageView.h>
@implementation TSAppDelegate
....
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//If you want you can overidde some properties using UIAppearance
[[TSMessageView appearance] setTitleFont:[UIFont boldSystemFontOfSize:6]];
[[TSMessageView appearance] setTitleTextColor:[UIColor redColor]];
[[TSMessageView appearance] setContentFont:[UIFont boldSystemFontOfSize:10]];
[[TSMessageView appearance]setContentTextColor:[UIColor greenColor]];
[[TSMessageView appearance]setErrorIcon:[UIImage imageNamed:@"NotificationButtonBackground"]];
[[TSMessageView appearance]setSuccessIcon:[UIImage imageNamed:@"NotificationButtonBackground"]];
[[TSMessageView appearance]setMessageIcon:[UIImage imageNamed:@"NotificationButtonBackground"]];
[[TSMessageView appearance]setWarningIcon:[UIImage imageNamed:@"NotificationButtonBackground"]];
//End of override
return YES;
}
在创建新通知时可以设置以下属性
除标题和通知类型外,列出的所有值都是可选的
如果您不需要详细的描述(标题下的文本),则无需设置。通知将自动调整大小。
iOS 7 设计
iOS 6 设计
TSMessages 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。
可以在本存储库的 发行版部分 找到。