注意:您可以在本仓库的 swift 分支 中找到此库的 Swift 2.0 实现。
CWStatusBarNotification
是一个库,它允许您轻松地创建出现在状态栏上的基于文本的通知。
您可以在此处找到文档。
CWStatusBarNotification
使用 ARC 并需要 iOS 7.0 或更高版本。
适用于 iPhone 和 iPad。
将 CWStatusBarNotification
文件夹复制到您的项目中。
首先,您需要以下导入语句
#import "CWStatusBarNotification.h"
现在,您需要创建一个 CWStatusBarNotification
对象。建议您将其作为属性附加到一个 UIViewController
上。
CWStatusBarNotification *notification = [CWStatusBarNotification new];
拥有一个 CWStatusBarNotification
对象后,您可以简单地调用 displayNotificationMessage:forDuration:
方法
[self.notification displayNotificationWithMessage:@"Hello, World!"
forDuration:1.0f];
如果您更喜欢手动选择何时显示和隐藏通知,您也可以这样做
[self.notification displayNotificationWithMessage:@"Hello" completion:nil];
// wait until you need to dismiss
[self.notification dismissNotification];
当通知被点击时的默认行为是将其关闭。但是,您可以通过设置不同的 onTapNotification
块来覆盖这种行为。
例如
self.notification.notificationTappedBlock = ^(void) {
NSLog(@"notification tapped");
// more code here
};
请注意,覆盖此块意味着在点击时通知将不再关闭。如果您希望通知在点击时仍然关闭,在覆盖块时务必实现以下操作
__weak typeof(self) weakSelf = self;
self.notification.notificationTappedBlock = ^(void) {
if (!weakSelf.notificationIsDismissing) {
[weakSelf dismissNotification];
// more code here
}
};
首先,您可以使用以下属性自定义背景颜色和文本颜色:notificationLabelBackgroundColor
和 notificationLabelTextColor
。
示例
notification.notificationLabelBackgroundColor = [UIColor blackColor];
notification.notificationLabelTextColor = [UIColor greenColor];
notificationLabelBackgroundColor
的默认值是 [[UIApplication sharedApplication] delegate].window.tintColor
。
notification.notificationLabelTextColor
的默认值是 [UIColor whiteColor]
。
最后,您还可以从两种样式中选择 - 一个与状态栏大小相等的通知,或者一个与状态栏和导航栏大小相等的通知。只需将 CWStatusBarNotification
对象的 notificationStyle
属性更改为 CWNotificationStyleStatusBarNotification
或 CWNotificationStyleNavigationBarNotification
。
notificationStyle
的默认值是 CWNotificationStyleStatusBarNotification
。
有两个属性决定了通知的动画风格:notificationAnimationInStyle
和 notificationAnimationOutStyle
。每个属性都可以选择以下四个值之一:
CWNotificationAnimationStyleTop
CWNotificationAnimationStyleBottom
CWNotificationAnimationStyleLeft
CWNotificationAnimationStyleRight
notificationAnimationInStyle
描述了通知从哪里来,而 notificationAnimationOutStyle
描述了通知将会去哪里。
notificationAnimationInStyle
的默认值是 CWNotificationAnimationStyleTop
。
notificationAnimationOutStyle
的默认值是 CWNotificationAnimationStyleTop
。
从版本 2.2.0
开始,您可以选择显示一个自定义视图,而不是简单地显示一条消息。演示项目展示了如何制作一个自定义 NIB 文件,并使用 displayNotificationWithView:forDuration:
方法将其作为通知视图显示的简单方法。
UIView *view = [[NSBundle mainBundle] loadNibNamed:@"CustomView" owner:nil options:nil][0];
[self.notification displayNotificationWithView:view forDuration:self.sliderDuration.value];
您也可以像往常一样显示通知,并选择在何时将其关闭
[self.notification displayNotificationWithView:view completion:nil];
// wait until you need to dismiss
[self.notification dismissNotification];
通知将在两种屏幕方向下都正常工作,但在显示通知时屏幕旋转尚未完全支持。
如果您想在这里展示您的应用程序,请联系我,我很乐意了解您的应用程序!
The MIT License (MIT)
Copyright (c) 2015 Cezary Wojcik <http://www.cezarywojcik.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.