iOS在狀態(tài)欄上顯示提醒信息的功能定制
更新時間:2017年06月09日 09:38:18 作者:踏歌尋方
這篇文章主要給大家介紹了iOS在狀態(tài)欄上顯示提醒信息的相關資料,實現(xiàn)后的效果非常不錯,文中給出了詳細的示例代碼供大家參考學習,需要的朋友們下面來一起看看吧。
先看效果圖

實現(xiàn)這個效果,用到了JDStatusBarNotification,這是一個易于使用和定制的在狀態(tài)欄上顯示提醒信息的控件,可自定義顏色、字體以及動畫,支持進度條展示,并可以顯示活動指示器。
假設這么一個場景,需要調接口修改個人資料,這時有3個狀態(tài),正在修改、修改成功、修改失敗。我們可以寫一個公共類,方便調用,譬如 NSObject+Common。
.h文件寫方法
#import <Foundation/Foundation.h> @interface NSObject (Common) - (void)showStatusBarQueryStr:(NSString *)tipStr; - (void)showStatusBarSuccessStr:(NSString *)tipStr; //此方法在實際開發(fā)中調用,調接口失敗返回的error - (void)showStatusBarError:(NSError *)error; //... - (void)showStatusBarErrorStr:(NSString *)tipStr; @end
.m文件實現(xiàn)方法
#import "NSObject+Common.h"
#import "JDStatusBarNotification.h"
@implementation NSObject (Common)
//error返回的tipStr
- (NSString *)tipFromError:(NSError *)error {
if (error && error.userInfo) {
NSMutableString *tipStr = [[NSMutableString alloc] init];
if ([error.userInfo objectForKey:@"msg"]) {
NSArray *msgArray = [[error.userInfo objectForKey:@"msg"] allValues];
NSUInteger num = [msgArray count];
for (int i = 0; i < num; i++) {
NSString *msgStr = [msgArray objectAtIndex:i];
if (i+1 < num) {
[tipStr appendString:[NSString stringWithFormat:@"%@\n", msgStr]];
}else{
[tipStr appendString:msgStr];
}
}
}else{
if ([error.userInfo objectForKey:@"NSLocalizedDescription"]) {
tipStr = [error.userInfo objectForKey:@"NSLocalizedDescription"];
}else{
[tipStr appendFormat:@"ErrorCode%ld", (long)error.code];
}
}
return tipStr;
}
return nil;
}
- (void)showStatusBarQueryStr:(NSString *)tipStr {
[JDStatusBarNotification showWithStatus:tipStr styleName:JDStatusBarStyleSuccess];
[JDStatusBarNotification showActivityIndicator:YES indicatorStyle:UIActivityIndicatorViewStyleWhite];
}
- (void)showStatusBarSuccessStr:(NSString *)tipStr {
if ([JDStatusBarNotification isVisible]) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[JDStatusBarNotification showActivityIndicator:NO indicatorStyle:UIActivityIndicatorViewStyleWhite];
[JDStatusBarNotification showWithStatus:tipStr dismissAfter:1.5 styleName:JDStatusBarStyleSuccess];
});
}else{
[JDStatusBarNotification showActivityIndicator:NO indicatorStyle:UIActivityIndicatorViewStyleWhite];
[JDStatusBarNotification showWithStatus:tipStr dismissAfter:1.0 styleName:JDStatusBarStyleSuccess];
}
}
- (void)showStatusBarError:(NSError *)error {
if ([JDStatusBarNotification isVisible]) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[JDStatusBarNotification showActivityIndicator:NO indicatorStyle:UIActivityIndicatorViewStyleWhite];
[JDStatusBarNotification showWithStatus:[self tipFromError:error] dismissAfter:1.5 styleName:JDStatusBarStyleError];
});
}else{
[JDStatusBarNotification showActivityIndicator:NO indicatorStyle:UIActivityIndicatorViewStyleWhite];
[JDStatusBarNotification showWithStatus:[self tipFromError:error] dismissAfter:1.5 styleName:JDStatusBarStyleError];
}
}
- (void)showStatusBarErrorStr:(NSString *)tipStr {
if ([JDStatusBarNotification isVisible]) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[JDStatusBarNotification showActivityIndicator:NO indicatorStyle:UIActivityIndicatorViewStyleWhite];
[JDStatusBarNotification showWithStatus:tipStr dismissAfter:1.5 styleName:JDStatusBarStyleError];
});
}else{
[JDStatusBarNotification showActivityIndicator:NO indicatorStyle:UIActivityIndicatorViewStyleWhite];
[JDStatusBarNotification showWithStatus:tipStr dismissAfter:1.5 styleName:JDStatusBarStyleError];
}
}
調用方法
[self showStatusBarQueryStr:@"正在修改個人信息"];
[self showStatusBarSuccessStr:@"個人信息修改成功"];
//[self showStatusBarError:error]; [self showStatusBarErrorStr:@"修改失敗"];
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關文章
IOS開發(fā)代碼分享之設置UISearchBar的背景顏色
在項目開發(fā)中,我們經常要用到UISearchBar,在網(wǎng)上看到了很多關于去除掉他背景色的方法,都已經失效了,今天來分享一個正常使用的方法,希望能幫到大家2014-09-09
Objective-C之Category實現(xiàn)分類示例詳解
這篇文章主要為大家介紹了Objective-C之Category實現(xiàn)分類示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08

