iOS在狀態(tài)欄上顯示提醒信息的功能定制
先看效果圖
實(shí)現(xiàn)這個(gè)效果,用到了JDStatusBarNotification,這是一個(gè)易于使用和定制的在狀態(tài)欄上顯示提醒信息的控件,可自定義顏色、字體以及動(dòng)畫,支持進(jìn)度條展示,并可以顯示活動(dòng)指示器。
假設(shè)這么一個(gè)場景,需要調(diào)接口修改個(gè)人資料,這時(shí)有3個(gè)狀態(tài),正在修改、修改成功、修改失敗。我們可以寫一個(gè)公共類,方便調(diào)用,譬如 NSObject+Common。
.h文件寫方法
#import <Foundation/Foundation.h> @interface NSObject (Common) - (void)showStatusBarQueryStr:(NSString *)tipStr; - (void)showStatusBarSuccessStr:(NSString *)tipStr; //此方法在實(shí)際開發(fā)中調(diào)用,調(diào)接口失敗返回的error - (void)showStatusBarError:(NSError *)error; //... - (void)showStatusBarErrorStr:(NSString *)tipStr; @end
.m文件實(shí)現(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]; } }
調(diào)用方法
[self showStatusBarQueryStr:@"正在修改個(gè)人信息"];
[self showStatusBarSuccessStr:@"個(gè)人信息修改成功"];
//[self showStatusBarError:error]; [self showStatusBarErrorStr:@"修改失敗"];
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
- iOS 自定義狀態(tài)欄和導(dǎo)航欄詳細(xì)介紹
- 詳解在iOS App中自定義和隱藏狀態(tài)欄的方法
- iOS實(shí)現(xiàn)點(diǎn)擊狀態(tài)欄自動(dòng)回到頂部效果詳解
- iOS 隱藏導(dǎo)航條和狀態(tài)欄實(shí)現(xiàn)方法
- 圖文講解如何解決App的iOS 7頂部狀態(tài)欄適配問題
- iOS 設(shè)置狀態(tài)欄的背景顏色方法
- 深入理解iOS的狀態(tài)欄
- IOS點(diǎn)擊按鈕隱藏狀態(tài)欄詳解及實(shí)例代碼
- IOS 開發(fā)狀態(tài)欄隱藏的實(shí)現(xiàn)辦法
- iOS狀態(tài)欄、導(dǎo)航欄的一些筆記分享
相關(guān)文章
iOS如何獲取設(shè)備型號(hào)的最新方法總結(jié)
在開發(fā)中,我們經(jīng)常需要獲取設(shè)備的型號(hào)以進(jìn)行數(shù)據(jù)統(tǒng)計(jì)或者做不同的適配。這篇文章主要給大家介紹了關(guān)于iOS如何獲取設(shè)備型號(hào)的最新方法,需要的朋友可以參考下2018-11-11基于ios逆向過程中l(wèi)ldb調(diào)試技巧(推薦)
下面小編就為大家?guī)硪黄趇os逆向過程中l(wèi)ldb調(diào)試技巧(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07IOS開發(fā)代碼分享之設(shè)置UISearchBar的背景顏色
在項(xiàng)目開發(fā)中,我們經(jīng)常要用到UISearchBar,在網(wǎng)上看到了很多關(guān)于去除掉他背景色的方法,都已經(jīng)失效了,今天來分享一個(gè)正常使用的方法,希望能幫到大家2014-09-09iOS使用AFN進(jìn)行單圖和多圖上傳的實(shí)例代碼
本篇文章中主要介紹了iOS使用AFN進(jìn)行單圖和多圖上傳的實(shí)例代碼,整理出單張和多張圖片上傳的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-04-04Objective-C之Category實(shí)現(xiàn)分類示例詳解
這篇文章主要為大家介紹了Objective-C之Category實(shí)現(xiàn)分類示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08