iOS开发-自定义UIAlterView(iOS 7)

十度 IOS 2015年12月01日 收藏

App中不可能少了弹框,弹框是交互的必要形式,使用起来也非常简单,不过最近需要自定义一个弹框,虽然iOS本身的弹框已经能满足大部分的需求,但是不可避免还是需要做一些自定义的工作。iOS7之前是可以自定义AlterView的,就是继承一下UIAlterView,然后初始化的时候通过addSubview添加自定义的View,但是iOS7之后这样做就不行了,不过还好有开源项目可以解决这个问题。

iOS默认弹框

viewDidLoad中添加两个按钮,代码如下:

 

  1. UIButton *orignalBtn=[[UIButton alloc]initWithFrame:CGRectMake(100, 40, 100, 50)];
  2. [orignalBtn setBackgroundColor:[UIColor greenColor]];
  3. [orignalBtn setTitle:@"iOS弹框" forState:UIControlStateNormal];
  4. [orignalBtn addTarget:self action:@selector(orignalShow) forControlEvents:UIControlEventTouchUpInside];
  5. [self.view addSubview:orignalBtn];
  6. UIButton *customlBtn=[[UIButton alloc]initWithFrame:CGRectMake(100, 140, 100, 50)];
  7. [customlBtn setBackgroundColor:[UIColor redColor]];
  8. [customlBtn setTitle:@"自定义弹框" forState:UIControlStateNormal];
  9. [customlBtn addTarget:self action:@selector(customShow) forControlEvents:UIControlEventTouchUpInside];
  10. [self.view addSubview:customlBtn];

 

 响应默认弹框事件:

  1. -(void)orignalShow{
  2. UIAlertView *alterView=[[UIAlertView alloc]initWithTitle:@"提示" message:@"博客园-Fly_Elephant" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
  3. [alterView show];
  4. }

  效果如下:

 

自定义弹框

主要解决iOS7之后的系统无法自定义弹框的问题,使用开源项目,项目地址:https://github.com/wimagguc/ios-custom-alertview,其实就是自定义了一个类:

CustomIOSAlertView.h

  1. #import <UIKit/UIKit.h>
  2.  
  3. @protocol CustomIOSAlertViewDelegate
  4.  
  5. - (void)customIOS7dialogButtonTouchUpInside:(id)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
  6.  
  7. @end
  8.  
  9. @interface CustomIOSAlertView : UIView<CustomIOSAlertViewDelegate>
  10.  
  11. @property (nonatomic, retain) UIView *parentView; // The parent view this 'dialog' is attached to
  12. @property (nonatomic, retain) UIView *dialogView; // Dialog's container view
  13. @property (nonatomic, retain) UIView *containerView; // Container within the dialog (place your ui elements here)
  14.  
  15. @property (nonatomic, assign) id<CustomIOSAlertViewDelegate> delegate;
  16. @property (nonatomic, retain) NSArray *buttonTitles;
  17. @property (nonatomic, assign) BOOL useMotionEffects;
  18.  
  19. @property (copy) void (^onButtonTouchUpInside)(CustomIOSAlertView *alertView, int buttonIndex) ;
  20.  
  21. - (id)init;
  22.  
  23. /*!
  24. DEPRECATED: Use the [CustomIOSAlertView init] method without passing a parent view.
  25. */
  26. - (id)initWithParentView: (UIView *)_parentView __attribute__ ((deprecated));
  27.  
  28. - (void)show;
  29. - (void)close;
  30.  
  31. - (IBAction)customIOS7dialogButtonTouchUpInside:(id)sender;
  32. - (void)setOnButtonTouchUpInside:(void (^)(CustomIOSAlertView *alertView, int buttonIndex))onButtonTouchUpInside;
  33.  
  34. - (void)deviceOrientationDidChange: (NSNotification *)notification;
  35. - (void)dealloc;
  36.  
  37. @end

CustomIOSAlertView.m

  1. #import "CustomIOSAlertView.h"
  2. #import <QuartzCore/QuartzCore.h>
  3.  
  4. const static CGFloat kCustomIOSAlertViewDefaultButtonHeight = 50;
  5. const static CGFloat kCustomIOSAlertViewDefaultButtonSpacerHeight = 1;
  6. const static CGFloat kCustomIOSAlertViewCornerRadius = 7;
  7. const static CGFloat kCustomIOS7MotionEffectExtent = 10.0;
  8.  
  9. @implementation CustomIOSAlertView
  10.  
  11. CGFloat buttonHeight = 0;
  12. CGFloat buttonSpacerHeight = 0;
  13.  
  14. @synthesize parentView, containerView, dialogView, onButtonTouchUpInside;
  15. @synthesize delegate;
  16. @synthesize buttonTitles;
  17. @synthesize useMotionEffects;
  18.  
  19. - (id)initWithParentView: (UIView *)_parentView
  20. {
  21. self = [self init];
  22. if (_parentView) {
  23. self.frame = _parentView.frame;
  24. self.parentView = _parentView;
  25. }
  26. return self;
  27. }
  28.  
  29. - (id)init
  30. {
  31. self = [super init];
  32. if (self) {
  33. self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
  34.  
  35. delegate = self;
  36. useMotionEffects = false;
  37. buttonTitles = @[@"Close"];
  38. [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
  39.  
  40. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
  41. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
  42. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
  43. }
  44. return self;
  45. }
  46.  
  47. // Create the dialog view, and animate opening the dialog
  48. - (void)show
  49. {
  50. dialogView = [self createContainerView];
  51. dialogView.layer.shouldRasterize = YES;
  52. dialogView.layer.rasterizationScale = [[UIScreen mainScreen] scale];
  53. self.layer.shouldRasterize = YES;
  54. self.layer.rasterizationScale = [[UIScreen mainScreen] scale];
  55.  
  56. #if (defined(__IPHONE_7_0))
  57. if (useMotionEffects) {
  58. [self applyMotionEffects];
  59. }
  60. #endif
  61.  
  62. self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
  63.  
  64. [self addSubview:dialogView];
  65.  
  66. // Can be attached to a view or to the top most window
  67. // Attached to a view:
  68. if (parentView != NULL) {
  69. [parentView addSubview:self];
  70.  
  71. // Attached to the top most window
  72. } else {
  73.  
  74. // On iOS7, calculate with orientation
  75. if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) {
  76. UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  77. switch (interfaceOrientation) {
  78. case UIInterfaceOrientationLandscapeLeft:
  79. self.transform = CGAffineTransformMakeRotation(M_PI * 270.0 / 180.0);
  80. break;
  81. case UIInterfaceOrientationLandscapeRight:
  82. self.transform = CGAffineTransformMakeRotation(M_PI * 90.0 / 180.0);
  83. break;
  84. case UIInterfaceOrientationPortraitUpsideDown:
  85. self.transform = CGAffineTransformMakeRotation(M_PI * 180.0 / 180.0);
  86. break;
  87. default:
  88. break;
  89. }
  90. [self setFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
  91.  
  92. // On iOS8, just place the dialog in the middle
  93. } else {
  94.  
  95. CGSize screenSize = [self countScreenSize];
  96. CGSize dialogSize = [self countDialogSize];
  97. CGSize keyboardSize = CGSizeMake(0, 0);
  98.  
  99. dialogView.frame = CGRectMake((screenSize.width - dialogSize.width) / 2, (screenSize.height - keyboardSize.height - dialogSize.height) / 2, dialogSize.width, dialogSize.height);
  100.  
  101. }
  102.  
  103. [[[[UIApplication sharedApplication] windows] firstObject] addSubview:self];
  104. }
  105.  
  106. dialogView.layer.opacity = 0.5f;
  107. dialogView.layer.transform = CATransform3DMakeScale(1.3f, 1.3f, 1.0);
  108.  
  109. [UIView animateWithDuration:0.2f delay:0.0 options:UIViewAnimationOptionCurveEaseInOut
  110. animations:^{
  111. self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4f];
  112. dialogView.layer.opacity = 1.0f;
  113. dialogView.layer.transform = CATransform3DMakeScale(1, 1, 1);
  114. }
  115. completion:NULL
  116. ];
  117.  
  118. }
  119.  
  120. // Button has been touched
  121. - (IBAction)customIOS7dialogButtonTouchUpInside:(id)sender
  122. {
  123. if (delegate != NULL) {
  124. [delegate customIOS7dialogButtonTouchUpInside:self clickedButtonAtIndex:[sender tag]];
  125. }
  126.  
  127. if (onButtonTouchUpInside != NULL) {
  128. onButtonTouchUpInside(self, (int)[sender tag]);
  129. }
  130. }
  131.  
  132. // Default button behaviour
  133. - (void)customIOS7dialogButtonTouchUpInside: (CustomIOSAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  134. {
  135. NSLog(@"Button Clicked! %d, %d", (int)buttonIndex, (int)[alertView tag]);
  136. [self close];
  137. }
  138.  
  139. // Dialog close animation then cleaning and removing the view from the parent
  140. - (void)close
  141. {
  142. CATransform3D currentTransform = dialogView.layer.transform;
  143.  
  144. if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) {
  145. CGFloat startRotation = [[dialogView valueForKeyPath:@"layer.transform.rotation.z"] floatValue];
  146. CATransform3D rotation = CATransform3DMakeRotation(-startRotation + M_PI * 270.0 / 180.0, 0.0f, 0.0f, 0.0f);
  147.  
  148. dialogView.layer.transform = CATransform3DConcat(rotation, CATransform3DMakeScale(1, 1, 1));
  149. }
  150.  
  151. dialogView.layer.opacity = 1.0f;
  152.  
  153. [UIView animateWithDuration:0.2f delay:0.0 options:UIViewAnimationOptionTransitionNone
  154. animations:^{
  155. self.backgroundColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.0f];
  156. dialogView.layer.transform = CATransform3DConcat(currentTransform, CATransform3DMakeScale(0.6f, 0.6f, 1.0));
  157. dialogView.layer.opacity = 0.0f;
  158. }
  159. completion:^(BOOL finished) {
  160. for (UIView *v in [self subviews]) {
  161. [v removeFromSuperview];
  162. }
  163. [self removeFromSuperview];
  164. }
  165. ];
  166. }
  167.  
  168. - (void)setSubView: (UIView *)subView
  169. {
  170. containerView = subView;
  171. }
  172.  
  173. // Creates the container view here: create the dialog, then add the custom content and buttons
  174. - (UIView *)createContainerView
  175. {
  176. if (containerView == NULL) {
  177. containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 150)];
  178. }
  179.  
  180. CGSize screenSize = [self countScreenSize];
  181. CGSize dialogSize = [self countDialogSize];
  182.  
  183. // For the black background
  184. [self setFrame:CGRectMake(0, 0, screenSize.width, screenSize.height)];
  185.  
  186. // This is the dialog's container; we attach the custom content and the buttons to this one
  187. UIView *dialogContainer = [[UIView alloc] initWithFrame:CGRectMake((screenSize.width - dialogSize.width) / 2, (screenSize.height - dialogSize.height) / 2, dialogSize.width, dialogSize.height)];
  188.  
  189. // First, we style the dialog to match the iOS7 UIAlertView >>>
  190. CAGradientLayer *gradient = [CAGradientLayer layer];
  191. gradient.frame = dialogContainer.bounds;
  192. gradient.colors = [NSArray arrayWithObjects:
  193. (id)[[UIColor colorWithRed:218.0/255.0 green:218.0/255.0 blue:218.0/255.0 alpha:1.0f] CGColor],
  194. (id)[[UIColor colorWithRed:233.0/255.0 green:233.0/255.0 blue:233.0/255.0 alpha:1.0f] CGColor],
  195. (id)[[UIColor colorWithRed:218.0/255.0 green:218.0/255.0 blue:218.0/255.0 alpha:1.0f] CGColor],
  196. nil];
  197.  
  198. CGFloat cornerRadius = kCustomIOSAlertViewCornerRadius;
  199. gradient.cornerRadius = cornerRadius;
  200. [dialogContainer.layer insertSublayer:gradient atIndex:0];
  201.  
  202. dialogContainer.layer.cornerRadius = cornerRadius;
  203. dialogContainer.layer.borderColor = [[UIColor colorWithRed:198.0/255.0 green:198.0/255.0 blue:198.0/255.0 alpha:1.0f] CGColor];
  204. dialogContainer.layer.borderWidth = 1;
  205. dialogContainer.layer.shadowRadius = cornerRadius + 5;
  206. dialogContainer.layer.shadowOpacity = 0.1f;
  207. dialogContainer.layer.shadowOffset = CGSizeMake(0 - (cornerRadius+5)/2, 0 - (cornerRadius+5)/2);
  208. dialogContainer.layer.shadowColor = [UIColor blackColor].CGColor;
  209. dialogContainer.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:dialogContainer.bounds cornerRadius:dialogContainer.layer.cornerRadius].CGPath;
  210.  
  211. // There is a line above the button
  212. UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, dialogContainer.bounds.size.height - buttonHeight - buttonSpacerHeight, dialogContainer.bounds.size.width, buttonSpacerHeight)];
  213. lineView.backgroundColor = [UIColor colorWithRed:198.0/255.0 green:198.0/255.0 blue:198.0/255.0 alpha:1.0f];
  214. [dialogContainer addSubview:lineView];
  215. // ^^^
  216.  
  217. // Add the custom container if there is any
  218. [dialogContainer addSubview:containerView];
  219.  
  220. // Add the buttons too
  221. [self addButtonsToView:dialogContainer];
  222.  
  223. return dialogContainer;
  224. }
  225.  
  226. // Helper function: add buttons to container
  227. - (void)addButtonsToView: (UIView *)container
  228. {
  229. if (buttonTitles==NULL) { return; }
  230.  
  231. CGFloat buttonWidth = container.bounds.size.width / [buttonTitles count];
  232.  
  233. for (int i=0; i<[buttonTitles count]; i++) {
  234.  
  235. UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
  236.  
  237. [closeButton setFrame:CGRectMake(i * buttonWidth, container.bounds.size.height - buttonHeight, buttonWidth, buttonHeight)];
  238.  
  239. [closeButton addTarget:self action:@selector(customIOS7dialogButtonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
  240. [closeButton setTag:i];
  241.  
  242. [closeButton setTitle:[buttonTitles objectAtIndex:i] forState:UIControlStateNormal];
  243. [closeButton setTitleColor:[UIColor colorWithRed:0.0f green:0.5f blue:1.0f alpha:1.0f] forState:UIControlStateNormal];
  244. [closeButton setTitleColor:[UIColor colorWithRed:0.2f green:0.2f blue:0.2f alpha:0.5f] forState:UIControlStateHighlighted];
  245. [closeButton.titleLabel setFont:[UIFont boldSystemFontOfSize:14.0f]];
  246. [closeButton.layer setCornerRadius:kCustomIOSAlertViewCornerRadius];
  247.  
  248. [container addSubview:closeButton];
  249. }
  250. }
  251.  
  252. // Helper function: count and return the dialog's size
  253. - (CGSize)countDialogSize
  254. {
  255. CGFloat dialogWidth = containerView.frame.size.width;
  256. CGFloat dialogHeight = containerView.frame.size.height + buttonHeight + buttonSpacerHeight;
  257.  
  258. return CGSizeMake(dialogWidth, dialogHeight);
  259. }
  260.  
  261. // Helper function: count and return the screen's size
  262. - (CGSize)countScreenSize
  263. {
  264. if (buttonTitles!=NULL && [buttonTitles count] > 0) {
  265. buttonHeight = kCustomIOSAlertViewDefaultButtonHeight;
  266. buttonSpacerHeight = kCustomIOSAlertViewDefaultButtonSpacerHeight;
  267. } else {
  268. buttonHeight = 0;
  269. buttonSpacerHeight = 0;
  270. }
  271.  
  272. CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
  273. CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
  274.  
  275. // On iOS7, screen width and height doesn't automatically follow orientation
  276. if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) {
  277. UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  278. if (UIInterfaceOrientationIsLandscape(interfaceOrientation)) {
  279. CGFloat tmp = screenWidth;
  280. screenWidth = screenHeight;
  281. screenHeight = tmp;
  282. }
  283. }
  284. return CGSizeMake(screenWidth, screenHeight);
  285. }
  286.  
  287. #if (defined(__IPHONE_7_0))
  288. // Add motion effects
  289. - (void)applyMotionEffects {
  290.  
  291. if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
  292. return;
  293. }
  294.  
  295. UIInterpolatingMotionEffect *horizontalEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.x"
  296. type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
  297. horizontalEffect.minimumRelativeValue = @(-kCustomIOS7MotionEffectExtent);
  298. horizontalEffect.maximumRelativeValue = @( kCustomIOS7MotionEffectExtent);
  299.  
  300. UIInterpolatingMotionEffect *verticalEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.y"
  301. type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
  302. verticalEffect.minimumRelativeValue = @(-kCustomIOS7MotionEffectExtent);
  303. verticalEffect.maximumRelativeValue = @( kCustomIOS7MotionEffectExtent);
  304.  
  305. UIMotionEffectGroup *motionEffectGroup = [[UIMotionEffectGroup alloc] init];
  306. motionEffectGroup.motionEffects = @[horizontalEffect, verticalEffect];
  307.  
  308. [dialogView addMotionEffect:motionEffectGroup];
  309. }
  310. #endif
  311.  
  312. - (void)dealloc
  313. {
  314. [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
  315.  
  316. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
  317. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
  318. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
  319. }
  320.  
  321. // Rotation changed, on iOS7
  322. - (void)changeOrientationForIOS7 {
  323.  
  324. UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  325. CGFloat startRotation = [[self valueForKeyPath:@"layer.transform.rotation.z"] floatValue];
  326. CGAffineTransform rotation;
  327. switch (interfaceOrientation) {
  328. case UIInterfaceOrientationLandscapeLeft:
  329. rotation = CGAffineTransformMakeRotation(-startRotation + M_PI * 270.0 / 180.0);
  330. break;
  331. case UIInterfaceOrientationLandscapeRight:
  332. rotation = CGAffineTransformMakeRotation(-startRotation + M_PI * 90.0 / 180.0);
  333. break;
  334. case UIInterfaceOrientationPortraitUpsideDown:
  335. rotation = CGAffineTransformMakeRotation(-startRotation + M_PI * 180.0 / 180.0);
  336. break;
  337. default:
  338. rotation = CGAffineTransformMakeRotation(-startRotation + 0.0);
  339. break;
  340. }
  341.  
  342. [UIView animateWithDuration:0.2f delay:0.0 options:UIViewAnimationOptionTransitionNone
  343. animations:^{
  344. dialogView.transform = rotation;
  345. }
  346. completion:nil
  347. ];
  348. }
  349.  
  350. // Rotation changed, on iOS8
  351. - (void)changeOrientationForIOS8: (NSNotification *)notification {
  352.  
  353. CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
  354. CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
  355.  
  356. [UIView animateWithDuration:0.2f delay:0.0 options:UIViewAnimationOptionTransitionNone
  357. animations:^{
  358. CGSize dialogSize = [self countDialogSize];
  359. CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
  360. self.frame = CGRectMake(0, 0, screenWidth, screenHeight);
  361. dialogView.frame = CGRectMake((screenWidth - dialogSize.width) / 2, (screenHeight - keyboardSize.height - dialogSize.height) / 2, dialogSize.width, dialogSize.height);
  362. }
  363. completion:nil
  364. ];
  365.  
  366. }
  367.  
  368. // Handle device orientation changes
  369. - (void)deviceOrientationDidChange: (NSNotification *)notification
  370. {
  371. // If dialog is attached to the parent view, it probably wants to handle the orientation change itself
  372. if (parentView != NULL) {
  373. return;
  374. }
  375.  
  376. if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) {
  377. [self changeOrientationForIOS7];
  378. } else {
  379. [self changeOrientationForIOS8:notification];
  380. }
  381. }
  382.  
  383. // Handle keyboard show/hide changes
  384. - (void)keyboardWillShow: (NSNotification *)notification
  385. {
  386. CGSize screenSize = [self countScreenSize];
  387. CGSize dialogSize = [self countDialogSize];
  388. CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
  389.  
  390. UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  391. if (UIInterfaceOrientationIsLandscape(interfaceOrientation)) {
  392. CGFloat tmp = keyboardSize.height;
  393. keyboardSize.height = keyboardSize.width;
  394. keyboardSize.width = tmp;
  395. }
  396.  
  397. [UIView animateWithDuration:0.2f delay:0.0 options:UIViewAnimationOptionTransitionNone
  398. animations:^{
  399. dialogView.frame = CGRectMake((screenSize.width - dialogSize.width) / 2, (screenSize.height - keyboardSize.height - dialogSize.height) / 2, dialogSize.width, dialogSize.height);
  400. }
  401. completion:nil
  402. ];
  403. }
  404.  
  405. - (void)keyboardWillHide: (NSNotification *)notification
  406. {
  407. CGSize screenSize = [self countScreenSize];
  408. CGSize dialogSize = [self countDialogSize];
  409.  
  410. [UIView animateWithDuration:0.2f delay:0.0 options:UIViewAnimationOptionTransitionNone
  411. animations:^{
  412. dialogView.frame = CGRectMake((screenSize.width - dialogSize.width) / 2, (screenSize.height - dialogSize.height) / 2, dialogSize.width, dialogSize.height);
  413. }
  414. completion:nil
  415. ];
  416. }
  417.  
  418. @end

调用代码:

  1. -(void)customShow{
  2. CustomIOSAlertView *alertView = [[CustomIOSAlertView alloc] init];
  3. [alertView setContainerView:[self customView]];
  4. [alertView setButtonTitles:[NSMutableArray arrayWithObjects:@"取消", @"确定", nil]];
  5. [alertView setDelegate:self];
  6. [alertView setOnButtonTouchUpInside:^(CustomIOSAlertView *alertView, int buttonIndex) {
  7. NSString *result=alertView.buttonTitles[buttonIndex];
  8. NSLog(@"点击了%@按钮",result);
  9. [alertView close];
  10. }];
  11. [alertView setUseMotionEffects:true];
  12. [alertView show];
  13.  
  14. }
  15.  
  16. - (UIView *)customView
  17. {
  18. UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 240, 160)];
  19. UILabel *tip=[[UILabel alloc]initWithFrame:CGRectMake(100, 10, 50, 30)];
  20. [tip setText:@"提示"];
  21. [customView addSubview:tip];
  22. UILabel *content=[[UILabel alloc]initWithFrame:CGRectMake(10, 60, 210, 30)];
  23. [content setText:@"http://www.cnblogs.com/xiaofeixiang"];
  24. [content setFont:[UIFont systemFontOfSize:12]];
  25. [customView addSubview:content];
  26. return customView;
  27. }

 效果如下: