- GitHub: Toast
- Star: 3k
Toast for iOS
Toast 是一個(gè)基于 Objective-C 語言的范疇(category)框架,它向 UIView 對(duì)象添加 Toast 通知。它簡(jiǎn)單、輕便且易于使用。大多數(shù) Toast 通知都可以通過一行代碼觸發(fā)。
如果你在使用 Swift? 這個(gè)框架也有一個(gè)原生的 Swift 版本支持: Toast-Swift
截屏

簡(jiǎn)單示例
// 基本用法
[self.view makeToast:@"This is a piece of toast."];
// 指定位置和持續(xù)時(shí)間
// duration:顯示 Toast 的持續(xù)時(shí)間,默認(rèn)顯示 3.0 秒后自動(dòng)消失。
// position:顯示 Toast 的位置,
// 可選位置參數(shù):CSToastPositionTop、CSToastPositionCenter、CSToastPositionBottom
[self.view makeToast:@"This is a piece of toast with a specific duration and position."
duration:3.0
position:CSToastPositionTop];
// 帶所有可自定義配置選項(xiàng)的 Toast
[self.view makeToast:@"This is a piece of toast with a title & image"
duration:3.0
position:[NSValue valueWithCGPoint:CGPointMake(110, 110)]
title:@"Toast Title"
image:[UIImage imageNamed:@"toast.png"]
style:nil
completion:^(BOOL didTap) {
if (didTap) {
NSLog(@"completion from tap");
} else {
NSLog(@"completion without tap");
}
}];
// 顯示加載活動(dòng)指示器的 Toast,這個(gè)方法類似于 MBProgressHUD
[self.view makeToastActivity:CSToastPositionCenter];
// 顯示自定義 UIView 的 Toast
[self.view showToast:myView];
注:Toast 框架中的 makeToast: 這個(gè)方法其實(shí)也可以用 MBProgressHUD 中的方法實(shí)現(xiàn):
// 封裝成通用一個(gè)方法
- (void)showMBProgressHudWithString:(NSString *)string {
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
// Set the text mode to show only text
hud.mode = MBProgressHUDModeText;
hud.label.text = NSLocalizedString(string, @"HUD message title");
// 移動(dòng)到底部中心
hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
// 3秒后自動(dòng)消失
[hud hideAnimated:YES afterDelay:3.f];
}
另外,Toast 中顯示 Loading 動(dòng)畫的方法 makeToastActivity: 你也可以等同于 MBProgressHUD 中的這個(gè)方法:
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
等等,還有更多!
// CSToastStyle 類是用來創(chuàng)建自定義樣式的
CSToastStyle *style = [[CSToastStyle alloc] initWithDefaultStyle];
// this is just one of many style options
// 文本顏色
style.messageColor = [UIColor orangeColor];
// present the toast with the new style
[self.view makeToast:@"This is a piece of toast."
duration:3.0
position:CSToastPositionBottom
style:style];
// or perhaps you want to use this style for all toasts going forward?
// just set the shared style and there's no need to provide the style again
// 設(shè)置一個(gè)共享的自定義樣式,然后通用所有 Toast
[CSToastManager setSharedStyle:style];
// toggle "tap to dismiss" functionality
// 打開 “點(diǎn)擊以關(guān)閉” 功能
[CSToastManager setTapToDismissEnabled:YES];
// toggle queueing behavior
// 按順序顯示 Toast
[CSToastManager setQueueEnabled:YES];
// immediately hides all toast views in self.view
// 立即隱藏所有正在顯示的 Toast
[self.view hideAllToasts];
安裝使用說明
CocoaPods
Install with CocoaPods by adding the following to your Podfile:
pod 'Toast', '~> 4.0.0'
Carthage
Install with Carthage by adding the following to your Cartfile:
github "scalessec/Toast" ~> 4.0.0
Run carthage update to build the framework and link against Toast.framework. Then, #import <Toast/Toast.h>.
手動(dòng)集成
- 將
UIView+Toast.h和UIView+Toast.m添加到你的項(xiàng)目中。 - 使用時(shí)直接導(dǎo)入文件
#import "UIView+Toast.h"即可。 - 等待奇跡(原文是“Grab yourself a cold ??”)。
MIT License
Copyright (c) 2011-2017 Charles Scalesse.
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.