FFToastiOS 消息提醒控件
FFToast是一個(gè)非常強(qiáng)大的iOS message notifications和AlertView擴(kuò)展。它可以很容易實(shí)現(xiàn)從屏幕頂部、屏幕底部和屏幕中間彈出一個(gè)通知。你可以很容易的自定義彈出的View.
要求
-
支持iOS 8或更高版本
-
支持ARC
特點(diǎn)
-
簡單易用
-
可以很容易自定義
安裝
CocoaPods
要使用CocoaPods安裝FFToast,請將其集成到您現(xiàn)有的Podfile中,或創(chuàng)建一個(gè)新的Podfile:
target 'MyApp' do pod 'FFToast' end
然后 pod install.
手動
將FFToast文件夾添加到項(xiàng)目中
使用方法
#import<FFToast/FFToast.h>
你可以通過調(diào)用下面的方法創(chuàng)建一個(gè)顯示在頂部的默認(rèn)效果的消息通知:
/** 創(chuàng)建并顯示一個(gè)Toast @param title 標(biāo)題 @param message 消息內(nèi)容 @param iconImage 消息icon,toastType不為FFToastTypeDefault時(shí)iconImage為空仍然會有相應(yīng)icon @param duration 顯示時(shí)長 */ + (void)showToastWithTitle:(NSString *)title message:(NSString *)message iconImage:(UIImage*)iconImage duration:(NSTimeInterval)duration toastType:(FFToastType)toastType;
其中的toastType:
typedef NS_ENUM(NSInteger, FFToastType) {
//灰色背景、無圖標(biāo)
FFToastTypeDefault = 0,
//綠色背景+成功圖標(biāo)
FFToastTypeSuccess = 1,
//紅色背景+錯(cuò)誤圖標(biāo)
FFToastTypeError = 2,
//橙色背景+警告圖標(biāo)
FFToastTypeWarning = 3,
//灰藍(lán)色背景+信息圖標(biāo)
FFToastTypeInfo = 4,
};
例如:
[FFToast showToastWithTitle:@"標(biāo)題" message:@"消息內(nèi)容......." iconImage:[UIImage imageNamed:@"test"] duration:3 toastType:FFToastTypeDefault];
標(biāo)題(title)、消息內(nèi)容(message)、圖標(biāo)(iconImage)均可以為nil,F(xiàn)FToast會根據(jù)具體的內(nèi)容進(jìn)行自適應(yīng)。
如果想在狀態(tài)欄下方、屏幕下方或者屏幕中間顯示消息通知,可以通過設(shè)置一些屬性實(shí)現(xiàn)。
設(shè)置顯示位置:
typedef NS_ENUM(NSInteger, FFToastPosition) {
//顯示在屏幕頂部
FFToastPositionDefault = 0,
//顯示在狀態(tài)欄下方
FFToastPositionBelowStatusBar = 1,
//顯示在狀態(tài)欄下方+圓角+左右邊距
FFToastPositionBelowStatusBarWithFillet = 2,
//顯示在屏幕底部
FFToastPositionBottom = 3,
//顯示在屏幕底部+圓角
FFToastPositionBottomWithFillet = 4,
//顯示在屏幕中間
FFToastPositionCentre = 5,
//顯示在屏幕中間+圓角
FFToastPositionCentreWithFillet = 6
};
其他的一些屬性:
//背景顏色 @property (strong, nonatomic) UIColor* toastBackgroundColor; //Toast標(biāo)題文字顏色 @property (strong, nonatomic) UIColor* titleTextColor; //Toast內(nèi)容文字顏色 @property (strong, nonatomic) UIColor* messageTextColor; //Toast標(biāo)題文字字體 @property (strong, nonatomic) UIFont* titleFont; //Toast文字字體 @property (strong, nonatomic) UIFont* messageFont; //Toast View圓角 @property(assign,nonatomic)CGFloat toastCornerRadius; //Toast View透明度 @property(assign,nonatomic)CGFloat toastAlpha; //Toast顯示時(shí)長 @property(assign,nonatomic)NSTimeInterval duration; //Toast消失動畫是否啟用 @property(assign,nonatomic)BOOL dismissToastAnimated; //Toast顯示位置 @property (assign, nonatomic) FFToastPosition toastPosition; //Toast顯示類型 @property (assign, nonatomic) FFToastType toastType; //是否自動隱藏,autoDismiss、enableDismissBtn、dismissBtnImage三個(gè)屬性僅對從屏幕中間彈出的Toast有效 @property(assign,nonatomic)BOOL autoDismiss; //是否在右上角顯示隱藏按鈕 @property(assign,nonatomic)BOOL enableDismissBtn; //隱藏按鈕的圖標(biāo) @property (strong, nonatomic) UIImage* dismissBtnImage;
設(shè)置完屬性后,就可以調(diào)用下面方法將其顯示出來:
/** 顯示一個(gè)Toast */ - (void)show;
或者:
/** 顯示一個(gè)Toast @param handler Toast點(diǎn)擊回調(diào) */ - (void)show:(handler)handler;
例如:
FFToast *toast = [[FFToast alloc]initToastWithTitle:@"標(biāo)題" message:@"消息內(nèi)容......." iconImage:[UIImage imageNamed:@"fftoast_info"]];
toast.toastPosition = FFToastPositionBelowStatusBarWithFillet;
toast.toastBackgroundColor = [UIColor colorWithRed:75.f/255.f green:107.f/255.f blue:122.f/255.f alpha:1.f];
[toast show:^{
//點(diǎn)擊消息通知時(shí)調(diào)用
}];//[toast show];
如果你想自定義一個(gè)從中間彈出的Toast,可以調(diào)用下面的方法:
/** 在中間顯示一個(gè)自定義Toast @param customToastView 自定義的ToastView @param autoDismiss 是否自動隱藏 @param duration 顯示時(shí)長(autoDismiss = NO時(shí)該參數(shù)將無效) @param enableDismissBtn 是否顯示隱藏按鈕 @param dismissBtnImage 隱藏按鈕圖片(enableDismissBtn = NO時(shí)該參數(shù)將無效) @return Toast */ - (instancetype)initCentreToastWithView:(UIView *)customToastView autoDismiss:(BOOL)autoDismiss duration:(NSTimeInterval)duration enableDismissBtn:(BOOL)enableDismissBtn dismissBtnImage:(UIImage*)dismissBtnImage;
你在自定義從中間彈出的Toast時(shí),你可以將上面的參數(shù)autoDismiss和參數(shù)enableDismissBtn設(shè)為NO。然后在你自定義的View中自己在合適的位置添加一個(gè)關(guān)閉按鈕。
關(guān)閉從中間彈出的Toast,可以調(diào)用下面的方法:
/** 隱藏一個(gè)Toast */ - (void)dismissCentreToast;
頂部、底部彈出的Toast不可自定義View,但是對于iconImage、Title、message均可以根據(jù)需要設(shè)置并且可以為nil,最終Toast會根據(jù)具體的內(nèi)容進(jìn)行自適應(yīng)。
隱藏消息通知:
默認(rèn)3秒后自動消失,向上滑動彈出的消息通知它也會消失。
關(guān)于
作者:
imlifengfeng
微博:
@imlifengfeng
