alertview封装 ios
2018-08-25 10:35:00 weixin_33835690 阅读数 17
1897259-97b8ccadd859011b.png
效果

代码地址

2016-09-01 18:04:00 weixin_34072458 阅读数 13

////  AlertViewTool.h//  AlertActiionDemo////  Created by Max on 16/8/30.//  Copyright © 2016年 maxzhang. All rights reserved.//#import#importtypedef void (^ActionBlockAtIndex)(NSInteger index);

@interface AlertViewTool : NSObject

@property (nonatomic, copy) ActionBlockAtIndex actionBlockAtIndex;

//UIAlertControllerStyleAlert,多个按钮(中间)

+ (id)AlertAlertWithTitle:(NSString*)title Message:(NSString *)message otherItemArrays:(NSArray *)array viewController:(UIViewController *)controller handler:(ActionBlockAtIndex)block;

//用法

//    UIAlertControllerStyleAlert

//[AlertViewTool AlertAlertWithTitle:@"标题" Message:@"这是一条消息" otherItemArrays:@[@"按钮1", @"按钮2"] viewController:self handler:^(NSInteger index) {

//

//    if (index == 0) {

//        NSLog(@"点击了按钮1");

//    }

//    else if (index == 1) {

//        NSLog(@"点击了按钮2");

//    }

//

//}];

//UIAlertControllerStyleActionSheet 多个按钮(底部) isShowCancel==是否展示取消按钮,yes表示展示

+ (id)AlertSheetToolWithTitle:(NSString*)title Message:(NSString *)message otherItemArrays:(NSArray *)array viewController:(UIViewController *)controller handler:(ActionBlockAtIndex)block isShowCancel:(BOOL)isShow CancelTitle:(NSString*)cancetitle;

//用法

//  UIAlertControllerStyleActionSheet

//    [AlertViewTool AlertSheetToolWithTitle:@"标题" Message:@"这是一条消息" otherItemArrays:@[@"按钮1", @"按钮2", @"按钮3"] viewController:self handler:^(NSInteger index) {

//        if (index == 0) {

//                NSLog(@"点击了按钮1");

//            }

//            else if (index == 1) {

//                NSLog(@"点击了按钮2");

//            }

//            else if (index == 2) {

//                NSLog(@"点击了按钮3");

//            }

//            else if (index == 2) {

//                NSLog(@"点击了取消");

//            }

//

//    } isShowCancel:YES CancelTitle:@"取消"];

@end


//

//  AlertViewTool.m

//  AlertActiionDemo

//

//  Created by Max on 16/8/30.

//  Copyright © 2016年 maxzhang. All rights reserved.

//

#import "AlertViewTool.h"

@implementation AlertViewTool

#pragma mark ==================AlertView=========================

//在中间提示的很多个按钮

+ (id)AlertAlertWithTitle:(NSString*)title Message:(NSString *)message otherItemArrays:(NSArray *)array viewController:(UIViewController *)controller handler:(ActionBlockAtIndex)block{

return [[self alloc] initWithTitleCenter:title Message:message otherItemArrays:array viewController:controller handler:block];

}

//在中间提示的很多个按钮

- (instancetype)initWithTitleCenter:(NSString *)title Message:(NSString*)message otherItemArrays:(NSArray *)array viewController:(UIViewController *)controller handler:(id)sender{

if ([self init]) {

UIAlertController *alertC = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];

self.actionBlockAtIndex = sender;

if (![array isKindOfClass:[NSNull class]] && array != nil && array.count) {

for (int i = 0; i < array.count; i++) {

UIAlertAction *otherAction = [UIAlertAction actionWithTitle:array[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

if (self.actionBlockAtIndex) {

self.actionBlockAtIndex(i);

}

[alertC dismissViewControllerAnimated:YES completion:nil];

}];

[alertC addAction:otherAction];

}

}

[controller presentViewController:alertC animated:YES completion:nil];

}

return self;

}

#pragma mark ==================AlertSheetTool=========================

+ (id)AlertSheetToolWithTitle:(NSString*)title Message:(NSString *)message otherItemArrays:(NSArray *)array viewController:(UIViewController *)controller handler:(ActionBlockAtIndex)block isShowCancel:(BOOL)isShow CancelTitle:(NSString*)cancetitle

{

return [[self alloc] initWithCancelTitle:title Message:message otherItemArrays:array viewController:controller handler:block isShowCancel:isShow CancelTitle:cancetitle];

}

- (instancetype)initWithCancelTitle:(NSString *)title Message:(NSString*)message otherItemArrays:(NSArray *)array viewController:(UIViewController *)controller handler:(id)sender isShowCancel:(BOOL)isShowCancel CancelTitle:(NSString *)Cancetitle

{

if ([self init]) {

UIAlertController *alertC = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];

self.actionBlockAtIndex = sender;

if (isShowCancel == YES) {

UIAlertAction *cancelAction=[UIAlertAction actionWithTitle:Cancetitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

if (self.actionBlockAtIndex) {

self.actionBlockAtIndex(array.count);

}

[alertC dismissViewControllerAnimated:YES completion:nil];

}];

[alertC addAction:cancelAction];

}

if (![array isKindOfClass:[NSNull class]] && array != nil && array.count) {

for (int i = 0; i < array.count; i++) {

UIAlertAction *otherAction = [UIAlertAction actionWithTitle:array[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

if (self.actionBlockAtIndex) {

self.actionBlockAtIndex(i);

}

[alertC dismissViewControllerAnimated:YES completion:nil];

}];

[alertC addAction:otherAction];

}

}

[controller presentViewController:alertC animated:YES completion:nil];

}

return self;

}

@end

//调用

#import "AlertViewTool.h"

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event

{

//    UIAlertControllerStyleAlert

[AlertViewTool AlertAlertWithTitle:@"标题" Message:@"这是一条消息" otherItemArrays:@[@"按钮1", @"按钮2"] viewController:self handler:^(NSInteger index) {

if (index == 0) {

NSLog(@"点击了按钮1");

}

else if (index == 1) {

NSLog(@"点击了按钮2");

}

}];

//  UIAlertControllerStyleActionSheet

//    [AlertViewTool AlertSheetToolWithTitle:@"标题" Message:@"这是一条消息" otherItemArrays:@[@"按钮1", @"按钮2", @"按钮3"] viewController:self handler:^(NSInteger index) {

//        if (index == 0) {

//                NSLog(@"点击了按钮1");

//            }

//            else if (index == 1) {

//                NSLog(@"点击了按钮2");

//            }

//            else if (index == 2) {

//                NSLog(@"点击了按钮3");

//            }

//            else if (index == 2) {

//                NSLog(@"点击了取消");

//            }

//

//    } isShowCancel:YES CancelTitle:@"取消"];

}

2017-08-02 18:18:00 weixin_33721344 阅读数 25

在项目中我们经常需要各种各样的弹窗,而系统提供的UIAlertView有的时候并不能满足我们的需求,这个时候需要自定义一个弹窗,我在项目中自定义了一个写的不好,大家见谅

1419035-7174f28481135942.gif
效果图.gif

0.首先创建一个ListAlertView继承UIView

1.声明一个初始化方法

我们先来看苹果给我们提供的初始化方法

UIAlertView *alertView = [UIAlertView alloc] initWithTitle:<#(nullable NSString *)#> message:<#(nullable NSString *)#> delegate:<#(nullable id)#> cancelButtonTitle:<#(nullable NSString *)#> otherButtonTitles:<#(nullable NSString *), ...#>, nil

苹果使用的是类方法,那么我们也按照苹果的格式创建这个自定义控件,因为封装好的控件有可能给其他的同事使用,在没有文档的情况下,对系统提供的方法是最熟悉的,这样可以减少沟通成本,我是这样创建的

+ (instancetype)initWithTitle: (NSString *)title listArray: ( NSArray *)listArray delegate: (id)delegate cancelButtonTitle: (NSString *)cancelButtonTitle otherButtonTitles: (NSString *)otherButtonTitles type: (alertViewType)type cellIndex: (NSInteger)cellIndex;

这个方法不一定是写死了,以后可能需要什么参数可以回来再改

到.m文件中实现这个方法

+ (instancetype)initWithTitle: (NSString *)title listArray: (NSArray *)listArray delegate: (id)delegate cancelButtonTitle: (NSString *)cancelButtonTitle otherButtonTitles: (NSString *)otherButtonTitles type: (alertViewType)type cellIndex: (NSInteger)cellIndex {
    
    ListAlertView *alertView = [[ListAlertView alloc]init];
    alertView.delegate = delegate;
    alertView.listArray = listArray;
    [alertView.title setTitle:title forState:UIControlStateNormal];
    [alertView.cancel setTitle:cancelButtonTitle forState:UIControlStateNormal];
    alertView.cellIndex = cellIndex;
    alertView.alertViewType = type;
    [alertView.sure setTitle:otherButtonTitles forState:UIControlStateNormal];
    alertView.leaveTypeArray = listArray;
    [[[UIApplication sharedApplication].delegate window] addSubview:alertView];
    alertView.frame = [UIScreen mainScreen].bounds;
    return alertView;
}

方法解释:在初始化方法里我实例化了alertView,它实际上就是整个后面的蒙版,它有一些属性是我声明在.m中的私有变量

@property (nonatomic , strong) UIView *backGround;
@property (nonatomic , strong) UIButton *sure;
@property (nonatomic , strong) UIButton *cancel;
@property (nonatomic , strong) UIButton *title;
@property (nonatomic , strong) UIView *firstLine;
@property (nonatomic , strong) UIView *secondLine;
@property (nonatomic , strong) UIView *threeLine;
@property (nonatomic , strong) UITableView *tableView;
@property (nonatomic , strong) NSArray *leaveTypeArray;
@property (nonatomic , assign) alertViewType alertViewType;
@property (nonatomic, strong)  UIPickerView *datePicker;
@property (nonatomic , assign) NSInteger cellIndex;

同样的,需要懒加载这些控件

- (UIView *)firstLine {
    if (_firstLine == nil) {
        _firstLine = [[UIView alloc]init];
        _firstLine.backgroundColor = ZCRedColor;
    }
    return _firstLine;
}
//threeLine
- (UIView *)threeLine {
    if (_threeLine == nil) {
        _threeLine = [[UIView alloc]init];
        _threeLine.backgroundColor = ZCRedColor;
    }
    return _threeLine;
}
- (UIView *)secondLine {
    if (_secondLine == nil) {
        _secondLine = [[UIView alloc]init];
        _secondLine.backgroundColor = ZCRedColor;
    }
    return _secondLine;
}
- (UIButton *)title {
    if (_title == nil) {
        _title = [[UIButton alloc]init];
//        NSLog(@"backGround%f",self.backGround.width);
        [_title setTitleColor:ZCRedColor forState:UIControlStateNormal];
        _title.titleLabel.font = ZCFont16;
    }
    return _title;
}
//sure

- (UIButton *)cancel {
    if (_cancel == nil) {
        _cancel = [[UIButton alloc]init];
        [_cancel setTitleColor:ZCRedColor forState:UIControlStateNormal];
        [_cancel addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancel;
}
- (UIButton *)sure {
    if (_sure == nil) {
        _sure = [[UIButton alloc]init];
        [_sure setTitleColor:ZCRedColor forState:UIControlStateNormal];
        _sure.titleLabel.font = ZCFont16;
        [_sure addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sure;
}
- (UIView *)backGround {
    if (_backGround == nil) {
        if (self.alertViewType == alertViewTypeList) {
             _backGround = [[UIView alloc]initWithFrame:CGRectMake(0, 0,288, 160)];
        } else if (self.alertViewType == alertViewTypeDate) {
            _backGround = [[UIView alloc]initWithFrame:CGRectMake(0, 0,kScreenWidth * 0.9, 240)];
        }
        _backGround.backgroundColor = ZCWhiteColor;
        _backGround.layer.cornerRadius = 8;
        _backGround.layer.shadowOffset = CGSizeMake(0, 0);
        _backGround.layer.shadowOpacity = 1.0;
        _backGround.layer.shadowColor = ZCLightGrayColor.CGColor;
        _backGround.center = self.center;
    }
    return _backGround;
}

未完

2015-01-06 12:18:38 Sunnylucy1 阅读数 530

http://www.idev101.com/code/User_Interface/UIAlertView.html

Alert views are pop-up views that appear over the current view on the iPhone.

Creating and showing an alert (ARC compatible):

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Really reset?" message:@"Do you really want to reset this game?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
    // optional - add more buttons:
    [alert addButtonWithTitle:@"Yes"];
    [alert show];

For non-ARC (retain/release) projects, you must autorelease the alert view:

    UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Really reset?" message:@"Do you really want to reset this game?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil] autorelease];
    // optional - add more buttons:
    [alert addButtonWithTitle:@"Yes"];
    [alert show];

If you add the UIAlertViewDelegate protocol to your controller, you can also add the following method which is called after the user dismisses the alert view:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        // do stuff
    }
}

Button indices start at 0 (for the cancelButton specified in the alloc/init), and go up by 1 for each addButtonWithTitle call you add. If you have a lot of alerts, your didDismiss method can keep track of which one is being dismissed if you add the setTag call to the alert initialization: [alert setTag:23];

    UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Error" message:@"I'm sorry Dave, I'm afraid I can't do that." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
    [alert setTag:12];
    [alert show];

... later ...

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if ([alertView tag] == 12) {    // it's the Error alert
        if (buttonIndex == 0) {     // and they clicked OK.
            // do stuff
        }
    }
}

Additional References

2017-06-26 15:42:00 weixin_33692284 阅读数 11

前言:

本文仅作参考存留,请用新版封装:iOS 更加优雅便捷的UIAlertView/UIAlertController封装使用

UIAlertController是iOS8.0之后出来的新方法,其将系统原先的UIAlertView和UIActionSheet进行了规范整合。iOS9.0之后,UIAlertView和UIActionSheet已经不建议使用,但还未彻底废弃。
alert提示窗可以算得上是十分常用的UI控件了,基于上述情况,考虑到版本兼容,笔者将上述控件进行了简单的整合封装。
封装之后,只需一句话,便可调用系统的alert提示,至于是调用alertView还是alertController,会根据系统版本自行判断,做到了兼容适配。alert提示窗的回调方法,也基于block进行了封装。按钮数量提供了变参和数组两种封装模式,各有用途。

代码见GitHub,已将这个较为简单但个人感觉还算十分实用的库进行了开源共享,喜欢的欢迎下载使用。
代码可能还入不了大牛的眼,水平有限,还望见谅,也欢迎使用和反馈。

支持的多种效果展示


下面叙述一下封装库内部分主要API的具体说明,代码中也有较为详细的注释。

1.普通alert 变参 兼容适配alertView和alertController

  /**
   *  普通alert定义 兼容适配alertView和alertController
   *
   *  @param viewController    当前视图,alertController模态弹出的指针
   *  @param title             标题
   *  @param message           详细信息
   *  @param block             用于执行方法的回调block
   *  @param cancelBtnTitle    取消按钮,可为nil
   *  @param destructiveBtn    alertController的特殊按钮类型,可为nil
   *  @param otherButtonTitles 其他按钮 变参量 但是按钮类型的相对位置是固定的,可为nil

   *  NS_REQUIRES_NIL_TERMINATION 是一个宏,用于编译时非nil结尾的检查 自动添加结尾的nil

      ***注意1***
      //block方法序列号和按钮名称相同,按钮类型排列顺序固定
      //如果取消为nil,则index0为特殊,以此往后类推,以第一个有效按钮为0开始累加
      //取消有的话默认为0

      ***注意2***
      destructiveButtonTitle
      iOS8以前,alert设置无效,因为不支持
      iOS8以后,alert设置有效
   */
  + (void) showAlertWith:(UIViewController *)viewController
                   title:(NSString *)title
                 message:(NSString *)message
           callbackBlock:(CallBackBlock)block
       cancelButtonTitle:(NSString *)cancelBtnTitle
  destructiveButtonTitle:(NSString *)destructiveBtn
       otherButtonTitles:(NSString *)otherButtonTitles, ...NS_REQUIRES_NIL_TERMINATION;

具体调用实例(默认系统是ios8以后的):

  [JXTAlertTools showAlertWith:self title:EmptyTitle message:_titleArray[indexPath.row] callbackBlock:^(NSInteger btnIndex) {
        if (btnIndex == 0) {
            NSLog(@"取消");
        }
        if (btnIndex == 1) {//注意ios8以前,没有这个键
            NSLog(@"特殊");
        }
        if (btnIndex == 2) {
            NSLog(@"其他");
        }
    } cancelButtonTitle:@"取消" destructiveButtonTitle:@"特殊" otherButtonTitles:@"其他", nil];

没有按钮时:

  //没有按钮时,默认自动消失
  [JXTAlertTools showAlertWith:self title:EmptyTitle message:_titleArray[indexPath.row] callbackBlock:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil, nil];

弹窗自动消失的持续时间由宏控制,可自行修改(详见JXTAlertTools.h):

  /**
   *  弹框显示的时间,默认1秒
   */
  #define AlertViewShowTime 1.0

2.多按钮数组模式排布alert 兼容适配alertView和alertController

  /**
   *  多按钮列表数组排布alert初始化 兼容适配
   *
   *  @param viewController       当前视图,alertController模态弹出的指针
   *  @param title                标题
   *  @param message              详细信息
   *  @param block                用于执行方法的回调block
   *  @param cancelBtnTitle       取消按钮
   *  @param otherBtnTitleArray   其他按钮的标题数组
   *  @param otherBtnStyleArray   按钮样式分布数组(普通/特殊),alertView默认为普通样式

      ***注意***
      UIAlertActionStyleCancel/JXTAlertActionStyleCancel最多只能有一个,否则崩溃
      Log:
      'UIAlertController can only have one action with a style of UIAlertActionStyleCancel'
   */
  + (void)showArrayAlertWith:(UIViewController *)viewController
                       title:(NSString *)title
                     message:(NSString *)message
               callbackBlock:(CallBackBlock)block
           cancelButtonTitle:(NSString *)cancelBtnTitle
       otherButtonTitleArray:(NSArray *)otherBtnTitleArray
       otherButtonStyleArray:(NSArray *)otherBtnStyleArray;

此方法是为了弥补前面那个方法初始化时,alert的按钮样式排布相对固定的局限,当然,这种排布只在iOS8之后有效。

具体调用实例(默认系统是ios8以后的):

  NSArray * titles = @[@"确定1", @"特殊1", @"确定2", @"特殊2"];
  NSArray * styles = @[
                         [NSNumber numberWithInteger:JXTAlertActionStyleDefault],
                         [NSNumber numberWithInteger:JXTAlertActionStyleDestructive],
                         [NSNumber numberWithInteger:JXTAlertActionStyleDefault],
                         [NSNumber numberWithInteger:JXTAlertActionStyleDestructive]
                         ];        
    [JXTAlertTools showArrayAlertWith:self title:EmptyTitle message:_titleArray[indexPath.row] callbackBlock:^(NSInteger btnIndex) {
        if (btnIndex == 0) {
            NSLog(@"取消");
        }
        else
            NSLog(@"%@", titles[btnIndex - 1]);
    } cancelButtonTitle:@"取消" otherButtonTitleArray:titles otherButtonStyleArray:styles];

上面要注意按钮的样式是枚举值,添加数组时要注意转化为对象,这里用了NSNumber,因为方法实现中也是按照NSNumber解析的:

  NSNumber * styleNum = otherBtnStyleArray[i];
  UIAlertActionStyle actionStyle =  styleNum.integerValue;
  UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherBtnTitleArray[i] style:actionStyle handler:^(UIAlertAction *action) {
     block(count);
  }];
  [alertController addAction:otherAction];

样式枚举定义:

  typedef enum {
      JXTAlertActionStyleDefault = 0,
      JXTAlertActionStyleCancel,
      JXTAlertActionStyleDestructive
  }JXTAlertActionStyle;

这里之所以不用系统提供的:

  typedef NS_ENUM(NSInteger, UIAlertActionStyle) {
      UIAlertActionStyleDefault = 0,
      UIAlertActionStyleCancel,
      UIAlertActionStyleDestructive
  } NS_ENUM_AVAILABLE_IOS(8_0);

是因为为了系统版本适配,系统的样式枚举是iOS8之后才提供的,如果直接使用,系统版本一旦低于iOS8,此时使用可能导致程序崩溃。

没有按钮时,情况同上:

  [JXTAlertTools showArrayAlertWith:self title:EmptyTitle message:_titleArray[indexPath.row] callbackBlock:nil cancelButtonTitle:nil otherButtonTitleArray:nil otherButtonStyleArray:nil];

关于actionSheet的API,基本同上,也提供了两个相应的方法,同样是兼容适配的。
关于方法的具体实现,在此不再赘述,有兴趣的可以参考代码,有什么问题欢迎讨论。

3.两种简易提示窗

这两种建议的提示窗,是基于上述方法的简化,适用于较为简单的提示场景。

  • 1.单按钮或无按钮alert提示

    [JXTAlertTools showTipAlertViewWith:self title:EmptyTitle message:_titleArray[indexPath.row] buttonTitle:@"确认" buttonStyle:JXTAlertActionStyleDefault];
  • 2.窗口底部简易actionSheet,无按钮

    [JXTAlertTools showBottomTipViewWith:self title:_titleArray[indexPath.row] message:_titleArray[indexPath.row]];

4.两个特殊用途的方法

  • 1.判断当前窗口是否有alert/actionSheet显示
    + (BOOL)isAlertShowNow;
    这个的用途具体看需求,可以用来去重显示,尤其是在alertView的情况下,有时可能连续多次弹出同一alertView,例如观察者回调,系统貌似没有做去重处理,笔者就遇到过监听回调导致的alert重复显示。但是如果是alertController,就不会发生了,控制台可能会直接给出警告的,因为是试图在一个已经销毁的vc上(第一次弹出的alertController消失时)第二次连续推出视图,这是做不到的。
    做去重显示判定时,此方法慎用,因为未做弹窗区分,同时的弹窗有可能是因为重复显示,也可能是不同警告类型的提示窗,去重的话就可能导致第二个不同的提示窗被过滤掉。

用法示例:

  NSLog(@"显示alert:%@", [JXTAlertTools isAlertShowNow] ? @"是" : @"否");
  if (![JXTAlertTools isAlertShowNow]) {//检测弹窗,控制alertView的去重显示
     [JXTAlertTools showTipAlertViewWith:self title:EmptyTitle message:_titleArray[indexPath.row] buttonTitle:nil buttonStyle:JXTAlertActionStyleDefault];
  }
  NSLog(@"显示alert:%@", [JXTAlertTools isAlertShowNow] ? @"是" : @"否");

控制台输出:


可能用方法有问题,但我是临时这个解决的。。。

  • 2.查找当前活动窗口
    + (UIViewController *)activityViewController;

这个方法源于网络,实际使用可行,主要是用来确定alertController的,也就是上面那个方法有用到此方法(已封装),当然这个方法不局限于只确定alertController。而且,alertView的检测和alertController的检测不是同一个方法,毕竟一个是view,一个是vc。

5.封装时遇到的一个小问题

当且仅当模拟器使用6p或者6sp时,alert有至多2个按钮,至少1个按钮,alert的message中使用“\n”时,分为3行,第一行无所谓,第2行字符小于38字符或者第3行字符小于76时(\n算一个字符,数字可能记错了。。。),控制台直接给了下面的警告,警告的达成条件较为苛刻,2、3任意一行多于临界值都不行,但的确是偶然,同样的字符数,没有“\n”就没事,多于这几个临界值也没事,唯独不能少。。。:

最简单的一个messge实例:@“1\n2\n3”

警告:

2016-01-28 10:13:47.714 JXTAlertTools[2379:60409] the behavior of the UICollectionViewFlowLayout is not defined because:
2016-01-28 10:13:47.715 JXTAlertTools[2379:60409] the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values.
2016-01-28 10:13:47.715 JXTAlertTools[2379:60409] The relevant UICollectionViewFlowLayout instance is <_UIAlertControllerCollectionViewFlowLayout: 0x7fc21ac7ed10>, and it is attached to <UICollectionView: 0x7fc21c877a00; frame = (0 94.6667; 270 44); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x7fc21ac1b5d0>; layer = <CALayer: 0x7fc21ac1c470>; contentOffset: {0, 0}; contentSize: {0, 0}> collection view layout: <_UIAlertControllerCollectionViewFlowLayout: 0x7fc21ac7ed10>.
2016-01-28 10:13:47.715 JXTAlertTools[2379:60409] Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.

不是无聊,只是偶然碰到这个情况,当时还排查了半天,最后才发现是这个问题,这也是很容易遇到的问题,无论是alertView还是alertController,都会出现。

复制代码:

  UIAlertView * al = [[UIAlertView alloc] initWithTitle:@"title" message:@"1\n2\n2" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
  [al show];

或者:

 UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"title" message:@"1\n2\n2" preferredStyle:UIAlertControllerStyleAlert];
 UIAlertAction * act =[UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    NSLog(@"输出");
 }];
 [alert addAction:act];
 [self presentViewController:alert animated:YES completion:nil];

用6p或者6sp运行,就会出现上述警告,原因不明(系统bug?)。。。其他模拟器都没事。这里提出来只是提醒注意规避吧。

而且分析上述警告也很有意思,大致是说UICollectionView的布局有问题,这是不是可以说明系统的alertView或者alertController都是利用UICollectionView进行封装的呢?

上述临界情况的样式:
1.不会出现的:



2.会出现的,仅仅是少了一个字符:


参考文章:
1.iOS 引用当前显示的UIAlertView
2.IOS -获取当前视图的Controller
3.UIWindowLevel详解

·转载请声明出处·

iOS 自定义UIAlertView

阅读数 281

iOS自定义AlertView

阅读数 621

iOS自定义UIAlertView

阅读数 4100

iOS 自定义AlertView

阅读数 12

没有更多推荐了,返回首页