iOS Custom prompt pop-up box to achieve similar uialertview effect _php instance

Source: Internet
Author: User
Tags uikit

First look at the implementation of the effect diagram


Below is a few words to say, the following is the implementation of the sample code

#import <UIKit/UIKit.h>

typedef void (^alertresult) (Nsinteger index);

@interface Xlalertview:uiview

@property (nonatomic,copy) Alertresult Resultindex;

-(Instancetype) Initwithtitle: (NSString *) title message: (NSString *) message surebtn: (NSString *) Suretitle canclebtn: ( NSString *) Cancletitle;

-(void) Showxlalertview;

@end
#import "XLAlertView.h"///alertview width #define ALERTW 280///distance between columns #define XLSPACE 10.0 @interface Xlalertview ()/
/Bullet window @property (nonatomic,retain) UIView *alertview;
Title @property (nonatomic,retain) Uilabel *titlelbl;
Content @property (nonatomic,retain) Uilabel *msglbl;
Confirm Button @property (nonatomic,retain) UIButton *surebtn;
Cancel button @property (nonatomic,retain) UIButton *canclebtn;
Horizontal line @property (nonatomic,retain) UIView *lineview;

Vertical @property (nonatomic,retain) UIView *verlineview; @end @implementation Xlalertview-(Instancetype) Initwithtitle: (NSString *) title message: (NSString *) message surebtn: ( NSString *) Suretitle canclebtn: (NSString *) Cancletitle {if (self = = [Super init]) {self.frame = [UIScreen mainscr

    Een].bounds;

    Self.backgroundcolor = [Uicolor colorwithwhite:0.8 alpha:0.6];
    Self.alertview = [[UIView alloc] init];
    Self.alertView.backgroundColor = [Uicolor Whitecolor];

    Self.alertView.layer.cornerRadius = 5.0; Self.alertView.frame= CGRectMake (0, 0, ALERTW, 100);

    Self.alertView.layer.position = Self.center; if (title) {Self.titlelbl = [self Getadaptivelable:cgrectmake (2*xlspace, 2*xlspace, Alertw-4*xlspace,) Andtext:
      Title Andistitle:yes];

      Self.titleLbl.textAlignment = Nstextalignmentcenter;

      [Self.alertview ADDSUBVIEW:SELF.TITLELBL];
      CGFloat Titlew = self.titleLbl.bounds.size.width;

      CGFloat Titleh = self.titleLbl.bounds.size.height;

    Self.titleLbl.frame = CGRectMake (Alertw-titlew)/2, 2*xlspace, Titlew, Titleh); } if (message) {SELF.MSGLBL = [self Getadaptivelable:cgrectmake (Xlspace, Cgrectgetmaxy (self.titleLbl.frame) +XL
      Space, Alertw-2*xlspace, Andtext:message Andistitle:no];

      Self.msgLbl.textAlignment = Nstextalignmentcenter;

      [Self.alertview ADDSUBVIEW:SELF.MSGLBL];
      CGFloat MSGW = self.msgLbl.bounds.size.width;

      CGFloat MSGH = self.msgLbl.bounds.size.height; Self.msgLbl.frame = Self.titlelbl? CGRectMake ((ALERTW-MSGW)/2, Cgrectgetmaxy (self.titleLbl.frame) +xlspace, MSGW, MSGH): CGRectMake ((ALERTW-MSGW)/2, 2*xlspace, MSGW, MSGH)
    ;
    } Self.lineview = [[UIView alloc] init]; Self.lineView.frame = Self.msglbl? CGRectMake (0, Cgrectgetmaxy (self.msgLbl.frame) +2*xlspace, ALERTW, 1): CGRectMake (0, Cgrectgetmaxy (
    Self.titleLbl.frame) +2*xlspace, ALERTW, 1);
    Self.lineView.backgroundColor = [Uicolor colorwithwhite:0.8 alpha:0.6];

    [Self.alertview AddSubview:self.lineView];
      Two button if (cancletitle && suretitle) {self.canclebtn = [UIButton Buttonwithtype:uibuttontypesystem];
      Self.cancleBtn.frame = CGRectMake (0, Cgrectgetmaxy (self.lineView.frame), (AlertW-1)/2, 40); [Self.canclebtn setbackgroundimage:[self imagewithcolor:[uicolor colorwithred:255/255.0 green:255/255.0 blue:255/
      255.0 alpha:0.2]] forstate:uicontrolstatenormal]; [Self.canclebtn setbackgroundimage:[self imagewithcolor:[uicolor colorwithred:255/255.0 green:255/255.0 blue:255/ 255.0 alpha:0.2]]Forstate:uicontrolstateselected];
      [Self.canclebtn Settitle:cancletitle Forstate:uicontrolstatenormal];
      [Self.canclebtn Settitlecolor:[uicolor Graycolor] forstate:uicontrolstatenormal];
      Self.cancleBtn.tag = 1;

      [Self.canclebtn addtarget:self Action: @selector (buttonevent:) forcontrolevents:uicontroleventtouchupinside]; Uibezierpath *maskpath = [Uibezierpath bezierPathWithRoundedRect:self.cancleBtn.bounds byroundingcorners:
      Uirectcornerbottomleft Cornerradii:cgsizemake (5.0, 5.0)];
      Cashapelayer *masklayer = [[Cashapelayer alloc] init];
      Masklayer.frame = Self.cancleBtn.bounds;
      Masklayer.path = Maskpath.cgpath;

      Self.cancleBtn.layer.mask = Masklayer;
    [Self.alertview AddSubview:self.cancleBtn];
      } if (Cancletitle && suretitle) {self.verlineview = [[UIView alloc] init]; Self.verLineView.frame = CGRectMake (Cgrectgetmaxx (Self.cancleBtn.frame), Cgrectgetmaxy (Self.lineView.frame), 1, 40)
      ; Self.verlineviEw.backgroundcolor = [Uicolor colorwithwhite:0.8 alpha:0.6];
    [Self.alertview AddSubview:self.verLineView];
      } if (Suretitle && cancletitle) {self.surebtn = [UIButton Buttonwithtype:uibuttontypesystem]; Self.sureBtn.frame = CGRectMake (Cgrectgetmaxx (Self.verLineView.frame), Cgrectgetmaxy (Self.lineView.frame), (
      AlertW-1)/2+1, 40); [Self.surebtn setbackgroundimage:[self imagewithcolor:[uicolor colorwithred:255/255.0 green:255/255.0 blue:255/
      255.0 alpha:0.2]] forstate:uicontrolstatenormal]; [Self.surebtn setbackgroundimage:[self imagewithcolor:[uicolor colorwithred:255/255.0 green:255/255.0 blue:255/
      255.0 alpha:0.2]] forstate:uicontrolstateselected];
      [Self.surebtn Settitle:suretitle Forstate:uicontrolstatenormal];
      [Self.surebtn Settitlecolor:[uicolor Whitecolor] forstate:uicontrolstatenormal];
      Self.sureBtn.tag = 2; [Self.surebtn addtarget:self Action: @selector (buttonevent:) forcontrolevents:uicontroleventtouchupinside];

      Uibezierpath *maskpath = [Uibezierpath bezierPathWithRoundedRect:self.sureBtn.bounds Byroundingcorners:uirectcorne
      Rbottomright Cornerradii:cgsizemake (5.0, 5.0)];
      Cashapelayer *masklayer = [[Cashapelayer alloc] init];
      Masklayer.frame = Self.sureBtn.bounds;
      Masklayer.path = Maskpath.cgpath;

      Self.sureBtn.layer.mask = Masklayer;

    [Self.alertview AddSubview:self.sureBtn]; ///Only Cancel button if (cancletitle &&!suretitle) {self.canclebtn = [UIButton buttonwithtype:uibuttontype
      System];
      Self.cancleBtn.frame = CGRectMake (0, Cgrectgetmaxy (self.lineView.frame), ALERTW, 40); [Self.canclebtn setbackgroundimage:[self imagewithcolor:[uicolor colorwithred:255/255.0 green:255/255.0 blue:255/
      255.0 alpha:0.2]] forstate:uicontrolstatenormal]; [Self.canclebtn setbackgroundimage:[self imagewithcolor:[uicolor colorwithred:255/255.0 green:255/255.0 blue:255/
      255.0 alpha:0.2]] forstate:uicontrolstateselected]; [Self.canclebtn SETtitle:cancletitle Forstate:uicontrolstatenormal];
      [Self.canclebtn Settitlecolor:[uicolor Graycolor] forstate:uicontrolstatenormal];
      Self.cancleBtn.tag = 1;

      [Self.canclebtn addtarget:self Action: @selector (buttonevent:) forcontrolevents:uicontroleventtouchupinside]; Uibezierpath *maskpath = [Uibezierpath bezierPathWithRoundedRect:self.cancleBtn.bounds byroundingcorners: Uirectcornerbottomleft |
      Uirectcornerbottomright Cornerradii:cgsizemake (5.0, 5.0)];
      Cashapelayer *masklayer = [[Cashapelayer alloc] init];
      Masklayer.frame = Self.cancleBtn.bounds;
      Masklayer.path = Maskpath.cgpath;

      Self.cancleBtn.layer.mask = Masklayer;
    [Self.alertview AddSubview:self.cancleBtn]; ///Only OK button if (suretitle &&!cancletitle) {self.surebtn = [UIButton buttonwithtype:uibuttontypesyst
      EM];
      Self.sureBtn.frame = CGRectMake (0, Cgrectgetmaxy (self.lineView.frame), ALERTW, 40); [Self.surebtn setbackgroundimage:[self ImagewitHcolor:[uicolor colorwithred:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2] forState:UIControlStateNormal]; [Self.surebtn setbackgroundimage:[self imagewithcolor:[uicolor colorwithred:255/255.0 green:255/255.0 blue:255/
      255.0 alpha:0.2]] forstate:uicontrolstateselected];
      [Self.surebtn Settitle:suretitle Forstate:uicontrolstatenormal];
      [Self.surebtn Settitlecolor:[uicolor Graycolor] forstate:uicontrolstatenormal];
      Self.sureBtn.tag = 2;

      [Self.surebtn addtarget:self Action: @selector (buttonevent:) forcontrolevents:uicontroleventtouchupinside]; Uibezierpath *maskpath = [Uibezierpath bezierPathWithRoundedRect:self.sureBtn.bounds byroundingcorners: Uirectcornerbottomleft |
      Uirectcornerbottomright Cornerradii:cgsizemake (5.0, 5.0)];
      Cashapelayer *masklayer = [[Cashapelayer alloc] init];
      Masklayer.frame = Self.sureBtn.bounds;
      Masklayer.path = Maskpath.cgpath;

      Self.sureBtn.layer.mask = Masklayer; [Self.alertview Addsubview:SELF.SUREBTN]; }//Calculate height cgfloat alertheight = cancletitle?
    Cgrectgetmaxy (self.cancleBtn.frame): Cgrectgetmaxy (Self.sureBtn.frame);
    Self.alertView.frame = CGRectMake (0, 0, ALERTW, alertheight);

    Self.alertView.layer.position = Self.center;
  [Self addSubview:self.alertView];
return self;
  #pragma mark-pop-up-(void) Showxlalertview {UIWindow *rootwindow = [UIApplication Sharedapplication].keywindow;
  [Rootwindow addsubview:self];
[Self creatshowanimation];
  }-(void) creatshowanimation {self.alertView.layer.position = Self.center;
  Self.alertView.transform = Cgaffinetransformmakescale (0.90, 0.90); [UIView animatewithduration:0.25 delay:0 usingspringwithdamping:0.8 initialspringvelocity:1 options:
  Uiviewanimationoptioncurvelinear animations:^{self.alertView.transform = Cgaffinetransformmakescale (1.0, 1.0);
} completion:^ (BOOL finished) {}]; #pragma mark-Callback-setting is only 2-> OK Callback-(void) Buttonevent: (UIButton *) Sender {if sender.tAG = = 2) {if (Self.resultindex) {self.resultindex (Sender.tag);
} [self Removefromsuperview]; }-(Uilabel *) getadaptivelable: (cgrect) rect andtext: (NSString *) contentstr andistitle: (BOOL) istitle {Uilabel *content
  LBL = [[Uilabel alloc] initwithframe:rect];
  Contentlbl.numberoflines = 0;
  Contentlbl.text = Contentstr;
  Contentlbl.textalignment = Nstextalignmentcenter;
  if (istitle) {contentlbl.font = [Uifont boldsystemfontofsize:16.0];
  }else{Contentlbl.font = [Uifont systemfontofsize:14.0];
  } nsmutableattributedstring *mattrstr = [[Nsmutableattributedstring alloc] initwithstring:contentstr];
  Nsmutableparagraphstyle *mparastyle = [[Nsmutableparagraphstyle alloc] init];
  Mparastyle.linebreakmode = nslinebreakbycharwrapping;
  [Mparastyle setlinespacing:3.0];
  [Mattrstr addattribute:nsparagraphstyleattributename value:mparastyle range:nsmakerange (0,[CONTENTSTR length])];
  [Contentlbl SETATTRIBUTEDTEXT:MATTRSTR];

  [Contentlbl SizeToFit];return CONTENTLBL;
  }-(UIImage *) Imagewithcolor: (Uicolor *) color {CGRect rect = CGRectMake (0.0f, 0.0f, 1.0f, 1.0f);
  Uigraphicsbeginimagecontext (rect.size);
  Cgcontextref context = Uigraphicsgetcurrentcontext ();
  Cgcontextsetfillcolorwithcolor (context, [color Cgcolor]);
  Cgcontextfillrect (context, rect);
  UIImage *theimage = Uigraphicsgetimagefromcurrentimagecontext ();
  Uigraphicsendimagecontext ();
return theimage; } @end

Call directly at the place where you want to use

Xlalertview *xlalertview = [[Xlalertview alloc] initwithtitle:@ "Custom Uialertview" message:@ "don't like to spray, great God a lot of guidance. Appreciate "surebtn:@" confirm "canclebtn:@" "Cancel"];
Xlalertview.resultindex = ^ (Nsinteger index) {
//callback---Handle a series of actions
};
[Xlalertview Showxlalertview];

Summarize

The above is the entire content of this article, I hope this article on the content of iOS developers can help, if there are questions you can message exchange, thank you for your support cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.