剛開始一直使用的是UIAlertController饥伊,發(fā)現(xiàn)多重alert會(huì)因?yàn)槎啻蝑issmiss回到不可預(yù)見的界面歇竟,所以就謹(jǐn)慎使用AlertVC了舞丛,
調(diào)起本地相冊(cè)相機(jī)
/*使用ALertController
* @brief調(diào)起本地相機(jī)撒踪,相冊(cè)
*底部出現(xiàn)偷俭,數(shù)組中傳遞的是字符串?dāng)?shù)組
* @param<#param#>
*/
+ (UIAlertController*)setAlertViewForAddImageWithAlertStyle:(UIAlertControllerStyle)alertStyle array:(NSArray *)titleArray local:(StatusBlock)local camera:(StatusBlock)camera {
UIAlertController* userIconAlert = [UIAlertControlleralertControllerWithTitle:titleArray[0]message:@""preferredStyle:alertStyle];
if(titleArray[1]) {
UIAlertAction* chooseFromPhotoAlbum = [UIAlertActionactionWithTitle:titleArray[1]style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*_Nonnullaction) {
local();
}];
[userIconAlertaddAction:chooseFromPhotoAlbum];
}
if(titleArray[2]) {
UIAlertAction* chooseFromCamera = [UIAlertActionactionWithTitle:titleArray[2]style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*_Nonnullaction) {
camera();
}];
[userIconAlertaddAction:chooseFromCamera];
}
UIAlertAction* canelAction = [UIAlertActionactionWithTitle:[titleArraylastObject]style:UIAlertActionStyleCancelhandler:^(UIAlertAction*_Nonnullaction) {
//取消
}];
[userIconAlertaddAction:canelAction];
returnuserIconAlert;
}
帶有按鈕的Alert
/*
* @brief ?title為按鈕字符串走贪,message為提示信息
*
* @param<#param#>
*/
+ (void)setAlertWithViewController:(UIViewController*)viewController Title:(NSString*)title message:(NSString*)message cancelTitle:(NSString*)cancelTitle sureTitle:(NSString*)sureTitle cancle:(StatusBlock)cancle sure:(StatusBlock)sure {
__weaktypeof(viewController) weakSelf = viewController;
UIAlertController*alertVC = [UIAlertControlleralertControllerWithTitle:titlemessage:messagepreferredStyle:UIAlertControllerStyleAlert];
if(cancelTitle) {
UIAlertAction*cancelAction = [UIAlertActionactionWithTitle:cancelTitlestyle:UIAlertActionStyleCancelhandler:^(UIAlertAction*_Nonnullaction) {
cancle();
}];
[alertVCaddAction:cancelAction];
}
if(sureTitle) {
UIAlertAction*sureAction = [UIAlertActionactionWithTitle:sureTitlestyle:UIAlertActionStyleDefaulthandler:^(UIAlertAction*_Nonnullaction) {
sure();
}];
[alertVCaddAction:sureAction];
}
[weakSelfpresentViewController:alertVCanimated:YEScompletion:nil];
}
常規(guī)提示框位置固定在屏幕下部
OC中并每次調(diào)用全部view佛猛,label都是臨時(shí)創(chuàng)建的,方法如下
+ (UIView*)showViewAtBottomWithViewController:(UIViewController*)viewController string:(NSString*)string{
UIView*view = [[UIViewalloc]initWithFrame:CGRectMake(ScreenWidth/2.0-100,ScreenHeight-90,200,40)];
view.layer.masksToBounds=YES;
view.layer.cornerRadius=5;
view.backgroundColor= [[UIColorblackColor]colorWithAlphaComponent:0.4];
UILabel*label = [[UILabelalloc]initWithFrame:CGRectMake(5,5,190,30)];
label.text= string;
label.textColor= [UIColorwhiteColor];
label.textAlignment=NSTextAlignmentCenter;
label.font= [UIFontsystemFontOfSize:14];
CGFloatwidth = [UILabelgetWidthWithTitle:stringfont:[UIFontsystemFontOfSize:14]];
if(width >ScreenWidth-80) {
view.frame=CGRectMake(40,ScreenHeight-90,ScreenWidth-80,40);
label.frame=CGRectMake(5,5,ScreenWidth-80-10,30);
}
else
{
view.frame=CGRectMake((ScreenWidth- width)/2,ScreenHeight-90, width +10,40);
label.frame=CGRectMake(5,5, width,30);
}
[viewaddSubview:label];
[viewController.viewaddSubview:view];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0*NSEC_PER_SEC)),dispatch_get_main_queue(), ^{
[viewremoveFromSuperview];
});
returnview;
}
其中方法+ (CGFloat)getWidthWithTitle:(NSString*)title font:(UIFont*)font 是給了label類目坠狡,自適應(yīng)提示信息高度不完美继找,
+ (CGFloat)getWidthWithTitle:(NSString*)title font:(UIFont*)font {
UILabel*label = [[UILabelalloc]initWithFrame:CGRectMake(0,0,1000,0)];
label.text= title;
label.font= font;
[labelsizeToFit];
returnlabel.frame.size.width;
}
OC中這個(gè)方法沒有完善,1.每次調(diào)用新建空間逃沿,2.提示信息沒那么長(zhǎng)所有沒有給定限制婴渡,
swift中完善了這個(gè)方法
import UIKit
class AlertViewHelper:NSObject{
static let shareInstance =AlertViewHelper()
private override init() {
}//單例
private var view =UIView()
private var label =UILabel()
private var size:CGSize?
//MARK: MARK標(biāo)簽----底部提示框
/*
* @brief<#Function#>
*
* @param<#param#>
*/
func showViewAtBottom(target:UIView,string:String) ->UIView{
if target.subviews.contains(view) {
view.removeFromSuperview()
}
view.frame=CGRectMake(SCREENWIDTH/2.0-100,SCREENHEIGHT-90,200,40)
view.layer.masksToBounds=true
view.layer.cornerRadius=5
view.backgroundColor=UIColor.blackColor().colorWithAlphaComponent(0.4)
label.frame=CGRectMake(5,5,190,30)
label.text= string
label.textColor= .whiteColor()
label.textAlignment= .Center
label.font=UIFont.systemFontOfSize(14)
label.numberOfLines=0
size=RequestHelper.getLabelSize(CGSizeMake(CGFloat(MAXFLOAT),CGFloat(MAXFLOAT)), string: string, font:UIFont.systemFontOfSize(14))
ifInt(size!.width) >Int(SCREENWIDTH) -60{
size=getLabelSize(CGSizeMake((CGFloat(SCREENWIDTH) -60),CGFloat(MAXFLOAT)), string: string, font:UIFont.systemFontOfSize(14))
}
label.frame=CGRectMake(10,5,size!.width,size!.height)
view.frame=CGRectMake((SCREENWIDTH-size!.width)/2.0-20,SCREENHEIGHT-50-size!.height-5,size!.width+20,size!.height+10)
view.addSubview(label)
target.addSubview(view)
dispatch_after(dispatch_time(DISPATCH_TIME_NOW,Int64(1.0*Double(NSEC_PER_SEC))),dispatch_get_main_queue()) {
iftarget.subviews.contains(self.view) {
self.view.removeFromSuperview()
}
}
returnview
}
//MARK: MARK標(biāo)簽----獲取文字的長(zhǎng)度和高度
/*
* @brief需要顯示一行,則size寬高均為最大凯亮,定寬時(shí)高度為最大
*
* @param<#param#>
*/
func getLabelSize(size:CGSize,string:String,font:UIFont) ->CGSize{
let str = string as NSString
return(str.boundingRectWithSize(size, options: .UsesLineFragmentOrigin, attributes: [NSFontAttributeName:font], context:nil).size)
}
}
中心狀態(tài)顯示
沒怎么用
/*
* @brief狀態(tài)顯示
*
* @param<#param#>
*/
+ (UIActivityIndicatorView*)setAlertViewAndFlowersWithViewController:(UIViewController*)viewController {
UIView*view = [[UIViewalloc]initWithFrame:CGRectMake(ScreenWidth/2.0-40,ScreenHeight/2.0-40,80,80)];
view.backgroundColor= [[UIColorblackColor]colorWithAlphaComponent:0.5];
view.layer.masksToBounds=YES;
view.layer.cornerRadius=5;
[viewController.viewaddSubview:view];
UIActivityIndicatorView*flowerView = [[UIActivityIndicatorViewalloc]initWithFrame:CGRectMake(15,15,50,50)];
flowerView.activityIndicatorViewStyle=UIActivityIndicatorViewStyleWhite;
flowerView.hidesWhenStopped=YES;
[flowerViewstartAnimating];
[viewaddSubview:flowerView];
returnflowerView;
}
含有l(wèi)ablel和TextField的多行提示框
+ (void)alertControllerWithTitle:(NSString*)title message:(NSString*)message viewController:(UIViewController*)viewController preferredStyle:(UIAlertControllerStyle)preferredStyle textFieldPlaceHolders:(NSArray *) placeholderssureHandel:(void(^)(NSArray* alterTextFs)) sureHandel cancelHandel:(void(^)()) cancelHander{
__weaktypeof(viewController) weakSelf = viewController;
UIAlertController*alertC = [UIAlertControlleralertControllerWithTitle:titlemessage:messagepreferredStyle:preferredStyle];
UIAlertAction*shureAction = [UIAlertActionactionWithTitle:@"確定"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*_Nonnullaction) {
if(sureHandel) {
NSMutableArray*temArr = [NSMutableArrayarray];
[alertC.textFieldsenumerateObjectsUsingBlock:^(UITextField*_Nonnullobj,NSUIntegeridx,BOOL*_Nonnullstop) {
[temArraddObject:obj.text];
}];
sureHandel([temArrcopy]);
}
}];
UIAlertAction*cancelAction = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:^(UIAlertAction*_Nonnullaction) {
if(cancelHander) {
cancelHander();
}
}];
[alertCaddAction:shureAction];
[alertCaddAction:cancelAction];
//plcaceholder
[placeholdersenumerateObjectsUsingBlock:^(NSString*_Nonnullobj,NSUIntegeridx,BOOL*_Nonnullstop) {
[alertCaddTextFieldWithConfigurationHandler:^(UITextField*_NonnulltextField) {
textField.placeholder= placeholders[idx];
}];
}];
[weakSelfpresentViewController:alertCanimated:YEScompletion:nil];
}