IOS custom alertView, inherited from UIView, you can add a subview, title image + text,

Source: Internet
Author: User
Tags uicontrol

IOS custom alertView, inherited from UIView, you can add a subview, title image + text,

Custom alertView, inherited from UIView, you can add a subview in the message area: addCustomerSubview

The title can contain images and text. Only two buttons are supported.


//-Call the alertView show method on the Controller that requires alert

CustomAlertView * alertView = [[CustomAlertView alloc] initWithTitle: @ "prompt" message: @ "dylan_lwb _" delegate: self cancelButtonTitle: @ "OK" otherButtonTitles: nil]; // [alertView addCustomerSubview: myView]; [alertView show]; [alertView release]; //-proxy method-(void) alertView :( CustomAlertView *) alertView clickedButtonAtIndex :( NSInteger) buttonIndex {}-(void) alertViewClosed :( CustomAlertView *) alertView {}-(void) willPresentCustomAlertView :( UIView *) alertView {}



//-Create a CustomAlertView category in the project and copy all the following code.

# If defined (_ APPLE_CC _) & (_ APPLE_CC _> = 5549) # define NS_REQUIRES_NIL_TERMINATION _ attribute _ (sentinel ))) # else # define NS_REQUIRES_NIL_TERMINATION _ attribute _ (sentinel) # endif # define SCREEN_WIDTH [[UIScreen mainScreen] bounds]. size. width # define SCREEN_HEIGHT [[UIScreen mainScreen] bounds]. size. height # define RGBA_COLOR (r, g, B, a) [UIColor colorWithRed: r/255.0f green: g/255.0f blue: B/255.0f alpha: a] # define kBCAlertViewPresentNotify @ "kBCAlertViewPresentNotify" // alertview present presenty # import <Foundation/Foundation. h> # import <UIKit/UIKit. h> @ class CustomAlertView; @ protocol upload <NSObject> @ optional //-proxy method-(void) alertView :( CustomAlertView *) alertView clickedButtonAtIndex :( NSInteger) buttonIndex;-(void) alertViewClosed :( CustomAlertView *) alertView;-(void) preview :( UIView *) alertView; //-hide the hidden category pop-up keyboard-(void) hideCurrentKeyBoard; @ end @ interface CustomAlertView: UIView {}@ property (nonatomic, assign) id <MBAlertViewDelegate> delegate; @ property (nonatomic, assign) BOOL isNeedCloseBtn; //-upper left corner with cross button @ property (nonatomic, retain) NSString * title; @ property (nonatomic, retain) NSString * message; @ property (nonatomic, retain) UIView * backView; @ property (nonatomic, retain) UIView * titleBackgroundView; @ property (nonatomic, retain) UILabel * titleLabel; @ property (nonatomic, retain) UIImageView * titleIcon; @ property (nonatomic, retain) NSMutableArray * customerViewsToBeAdd;-(id) initWithTitle :( NSString *) title message :( NSString *) message delegate :( id) delegate cancelButtonTitle :( NSString *) cancelButtonTitle otherButtonTitles :( NSString *) otherButtonTitles ,... response;-(void) initTitle :( NSString *) title message :( NSString *) message delegate :( id) del cancelButtonTitle :( NSString *) cancelBtnTitle otherButtonTitles :( NSString *) otherBtnTitles ,... NS_REQUIRES_NIL_TERMINATION;-(void) show; //-Add a custom control in alertview-(void) addCustomerSubview :( UIView *) view; + (void) exChangeOut :( UIView *) changeOutView dur :( CFTimeInterval) dur; + (CustomAlertView *) defaultAlert; @ end




# Import "CustomAlertView. h "@ interface CustomAlertView () {BOOL _ isShow;} @ property (nonatomic, retain) NSString * cancelButtonTitle; @ property (nonatomic, retain) NSMutableArray * otherButtonTitles; @ end @ implementation CustomAlertViewstatic const CGFloat mWidth = 290; static const CGFloat mHeight = 180; static const CGFloat mMaxHeight = 250; static const CGFloat mBtnHeight = 30; static const CGFloat mBtnWidt H = 110; static const CGFloat mHeaderHeight = 40; + (CustomAlertView *) defaultAlert {static CustomAlertView * shareCenter = nil; if (! ShareCenter) {shareCenter = [[CustomAlertView alloc] init];} return shareCenter;}-(NSMutableArray *) customerViewsToBeAdd {if (_ customerViewsToBeAdd = nil) {_ customerViewsToBeAdd = [[NSMutableArray alloc] init];} return _ customerViewsToBeAdd;}-(id) init {self = [super initWithFrame: CGRectMake (0, 0, SCREEN_WIDTH, SCREEN_HEIGHT + 20)]; if (self) {_ isShow = NO;} return self;}-(id) initWithTitle :( N SString *) title message :( NSString *) message delegate :( id) del cancelButtonTitle :( NSString *) cancelBtnTitle otherButtonTitles :( NSString *) otherBtnTitles ,... {self = [super initWithFrame: CGRectMake (0, 0, SCREEN_WIDTH, SCREEN_HEIGHT + 20)]; if (self) {self. delegate = del; self. cancelButtonTitle = cancelBtnTitle; self. isNeedCloseBtn = NO; if (! _ OtherButtonTitles) {va_list argList; if (otherBtnTitles) {self. otherButtonTitles = [NSMutableArray array]; [self. otherButtonTitles addObject: otherBtnTitles];} va_start (argList, otherBtnTitles); id arg; while (arg = va_arg (argList, id) {[self. otherButtonTitles addObject: arg] ;}} self. title = title; self. message = message;} return self;}-(void) initTitle :( NSString *) title message :( NSString *) m Essage delegate :( id) del cancelButtonTitle :( NSString *) cancelBtnTitle otherButtonTitles :( NSString *) otherBtnTitles ,... {if (self) {self. delegate = del; self. cancelButtonTitle = cancelBtnTitle; self. isNeedCloseBtn = NO; if (! _ OtherButtonTitles) {va_list argList; if (otherBtnTitles) {self. otherButtonTitles = [NSMutableArray array]; [self. otherButtonTitles addObject: otherBtnTitles];} va_start (argList, otherBtnTitles); id arg; while (arg = va_arg (argList, id) {[self. otherButtonTitles addObject: arg] ;}} self. title = title; self. message = message ;}}- (void) layoutSubviews {[super layoutSubviews]; for (UIView * view In [self subviews]) {[view removeFromSuperview];} UIView * bgView = [[UIView alloc] initWithFrame: self. frame]; [bgView setBackgroundColor: [UIColor blackColor]; [bgView setAlpha: 0.4]; [self addSubview: bgView]; [bgView release]; if (! _ BackView) {_ backView = [[UIView alloc] initWithFrame: CGRectMake (0, 0, mWidth, mHeight)]; _ backView. opaque = NO; _ backView. backgroundColor = [UIColor whiteColor]; _ backView. layer. shadowOffset = CGSizeMake (1, 1); _ backView. layer. shadowRadius = 2.0; _ backView. layer. shadowColor = [UIColor grayColor]. CGColor; _ backView. layer. shadowOpacity = 0.8; [_ backView. layer setMasksToBounds: NO];} //-set the display area of the header/ /-Set the title background UIView * titleView = [[UIView alloc] initWithFrame: CGRectMake (0, 0, mWidth, mHeaderHeight)]; titleView. backgroundColor = RGBA_COLOR (36,193, 64, 1); CGSize titleSize = CGSizeZero; if (self. title & [self. title length]> 0) {titleSize = [self. title sizeWithFont: [UIFont fontWithName: @ "Helvetica-Bold" size: 18366f];} if (titleSize. width> 0) {//-title image CGFloat startX = (titleView. frame. si Ze. width-40-titleSize. width)/2; UIImageView * titleImage = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @ "xxxxx.png"]; titleImage. frame = CGRectMake (startX, (titleView. frame. size. height-30)/2, 30, 30); self. titleIcon = titleImage; [titleView addSubview: titleImage]; [titleImage release]; startX + = 40; //-title UILabel * titleLabel = [[UILabel alloc] initWithFrame: CGRectMake (startX, (TitleView. frame. size. height-20)/2, titleSize. width, 20)]; //-if the title is too long, you need to process if (titleLabel. frame. size. width & gt; 250) {titleLabel. frame = CGRectMake (50, (titleView. frame. size. height-20)/2, mWidth-60, 20); titleImage. frame = CGRectMake (5, (titleView. frame. size. height-30)/2, 30, 30);} titleLabel. text = self. title; titleLabel. font = [UIFont fontWithName: @ "Helvetica-Bold" size: 18366f]; tit LeLabel. textColor = [UIColor whiteColor]; titleLabel. backgroundColor = [UIColor clearColor]; self. titleLabel = titleLabel; [titleView addSubview: titleLabel]; [titleLabel release];} else {//-title image UIImageView * titleImage = [[UIImageView alloc] Subject: [UIImage imageNamed: @ "xxxxx.png"]; titleImage. frame = CGRectMake (titleView. frame. size. width-30)/2, (titleView. frame. size. height-30 )/ 2, 30, 30); [titleView addSubview: titleImage]; [titleImage release];} if (self. isNeedCloseBtn) {UIButton * btn = [[UIButton alloc] initWithFrame: CGRectMake (mWidth-mHeaderHeight, 0, mHeaderHeight, mHeaderHeight)]; btn. backgroundColor = [UIColor clearColor]; [btn setImage: [UIImage imageNamed: @ "success"] forState: UIControlStateNormal]; [btn addTarget: self action: @ selector (closeBtnCli Cked :) forControlEvents: UIControlEventTouchUpInside]; [titleView addSubview: btn]; [btn release];} [_ backView addSubview: titleView]; self. titleBackgroundView = titleView; [titleView release]; //-set UIScrollView * msgView = [[UIScrollView alloc] initWithFrame: CGRectMake (0, mHeaderHeight, mWidth, (mHeight-mHeaderHeight * 2)]; //-sets the background color msgView. backgroundColor = [UIColor whiteColor]; //-inner CGSize messageSize = CGSizeZero; if (self. message & [self. message length]> 0) {messageSize = [self. message sizeWithFont: [UIFont systemFontOfSize: 16366f] constrainedToSize: CGSizeMake (mWidth-20, MAXFLOAT) lineBreakMode: NSLineBreakByWordWrapping]; if (messageSize. width> 0) {UILabel * msgLabel = [[UILabel alloc] init]; msgLabel. font = [UIFont systemFontOfSize: 16366f]; msgLabel. text = self. message; m SgLabel. numberOfLines = 0; msgLabel. textAlignment = NSTextAlignmentCenter; msgLabel. backgroundColor = [UIColor clearColor]; msgLabel. frame = CGRectMake (10, (mHeight-mHeaderHeight * 2)-messageSize. height)/2, mWidth-20, messageSize. height + 5); if (messageSize. height> mMaxHeight) {msgView. frame = CGRectMake (msgView. frame. origin. x, msgView. frame. origin. y, msgView. frame. size. width, mMaxHeight + 25); _ backView. frame = CGRectMake (0, 0, mWidth, mHeaderHeight * 2 + msgView. frame. size. height); msgLabel. textAlignment = NSTextAlignmentLeft; msgLabel. frame = CGRectMake (10, 10, mWidth-20, messageSize. height); msgView. contentSize = CGSizeMake (msgView. frame. size. width, msgLabel. frame. size. height + 20);} else if (messageSize. height> (mHeight-mHeaderHeight * 2)-10) {msgView. frame = CGRectMak E (msgView. frame. origin. x, msgView. frame. origin. y, msgView. frame. size. width, messageSize. height + 25); _ backView. frame = CGRectMake (0, 0, mWidth, mHeaderHeight * 2 + msgView. frame. size. height); msgLabel. frame = CGRectMake (10, 10, mWidth-20, messageSize. height + 5);} [msgView addSubview: msgLabel]; [msgLabel release]; [_ backView addSubview: msgView];} else {if (self. customerViewsToBeAdd & [self. CustomerViewsToBeAdd count]> 0) {CGFloat startY = 0; for (UIView * subView in self. customerViewsToBeAdd) {CGRect rect = subView. frame; rect. origin. y = startY; subView. frame = rect; [msgView addSubview: subView]; startY + = rect. size. height;} msgView. frame = CGRectMake (0, mHeaderHeight, mWidth, startY);} [_ backView addSubview: msgView]; _ backView. frame = CGRectMake (0, 0, mWidth, msgView. frame. size. Height + mHeaderHeight * 2 + 20);} [msgView release]; //-set the button to display the region if (_ otherButtonTitles! = Nil | _ cancelButtonTitle! = Nil) {UIView * btnView = [[UIView alloc] initWithFrame: CGRectMake (0, _ backView. frame. size. height-mHeaderHeight, mWidth, mHeaderHeight)]; //-if only one button is displayed, calculate the display size of the button if (_ otherButtonTitles = nil | _ cancelButtonTitle = nil) {UIButton * btn = [[UIButton alloc] initWithFrame: CGRectMake (_ backView. frame. size. width-mBtnWidth)/2, 0, mBtnWidth, mBtnHeight)]; btn. backgroundColor = RGBA_COLOR (36,193, 6 4, 1); btn. titleLabel. font = [UIFont fontWithName: @ "Helvetica" size: 17366f]; btn. titleLabel. textColor = [UIColor whiteColor]; [btn addTarget: self action: @ selector (buttonClicked :) forControlEvents: UIControlEventTouchUpInside]; if (_ otherButtonTitles = nil & _ cancelButtonTitle! = Nil) {[btn setTitle: _ cancelButtonTitle forState: UIControlStateNormal];} else {[btn setTitle: [_ otherButtonTitles objectAtIndex: 0] forState: UIControlStateNormal];} btn. tag = 0; btn. layer. cornerRadius = 5; [btnView addSubview: btn]; [btn release];} else if (_ otherButtonTitles & [_ otherButtonTitles count] = 1) {//-display two buttons //-set CGFloat startX = (_ backView. frame. size. width-mBtnWidth * 2-20)/2; UIButton * otherBtn = [[UIButton alloc] initWithFrame: CGRectMake (startX, 0, mBtnWidth, mBtnHeight)]; otherBtn. backgroundColor = RGBA_COLOR (36,193, 64, 1); otherBtn. titleLabel. font = [UIFont fontWithName: @ "Helvetica" size: 17366f]; otherBtn. titleLabel. textColor = [UIColor whiteColor]; [otherBtn setTitle: [_ otherButtonTitles objectAtIndex: 0] forState: UIControlStateNormal]; otherBtn. layer. corner Radius = 5; [otherBtn addTarget: self action: @ selector (buttonClicked :) forControlEvents: UIControlEventTouchUpInside]; otherBtn. tag = 0; [btnView addSubview: otherBtn]; [otherBtn release]; startX ++ = mBtnWidth + 20; //-set the UIButton * cancelBtn = [[UIButton alloc] initWithFrame: CGRectMake (startX, 0, mBtnWidth, mBtnHeight)]; cancelBtn. backgroundColor = RGBA_COLOR (36,193, 64, 1); cancelBtn. titleLa Bel. font = [UIFont fontWithName: @ "Helvetica" size: 17366f]; cancelBtn. titleLabel. textColor = [UIColor whiteColor]; [cancelBtn setTitle: _ cancelButtonTitle forState: UIControlStateNormal]; cancelBtn. layer. cornerRadius = 5; [cancelBtn addTarget: self action: @ selector (buttonClicked :) forControlEvents: UIControlEventTouchUpInside]; cancelBtn. tag = 1; [btnView addSubview: cancelBtn]; [cancelBtn release];} [_ BackView addSubview: btnView]; [btnView release];} else {CGRect rc = _ backView. frame; rc. size. height-= mHeaderHeight; _ backView. frame = rc;} UIControl * touchView = [[UIControl alloc] initWithFrame: self. frame]; [touchView addTarget: self action: @ selector (touchViewClickDown) forControlEvents: UIControlEventTouchDown]; touchView. frame = self. frame; [self addSubview: touchView]; [touchView release]; _ BackView. center = self. center; [self addSubview: _ backView]; if (! _ IsShow) [CustomAlertView exChangeOut: _ backView dur: 0.5]; if (self. delegate) {if ([self. delegate respondsToSelector: @ selector (willPresentCustomAlertView :)]) {[self. delegate willPresentCustomAlertView: self];} [[nsicationcenter center defacenter center] postNotificationName: kBCAlertViewPresentNotify object: nil userInfo: nil] ;}- (void) touchViewClickDown {if (self. delegate) {if ([self. delegate respondsT OSelector: @ selector (hideCurrentKeyBoard)]) {[self. delegate hideCurrentKeyBoard] ;}}//-set the custom control in the message area-(void) addCustomerSubview :( UIView *) view {[self. customerViewsToBeAdd addObject: view];}-(void) buttonClicked :( id) sender {UIButton * btn = (UIButton *) sender; _ isShow = NO; if (btn) {if (self. delegate & [self. delegate respondsToSelector: @ selector (alertView: clickedButtonAtIndex :)]) {[self. deltas Te alertView: self clickedButtonAtIndex: btn. tag];} [self removeFromSuperview];}-(void) closeBtnClicked :( id) sender {_ isShow = NO; if (self. delegate & [self. delegate respondsToSelector: @ selector (alertViewClosed :)]) {[self. delegate alertViewClosed: self];} [self removeFromSuperview];}-(void) show {[self layoutSubviews]; if (! _ IsShow) [[[UIApplication sharedApplication]. delegate window] addSubview: self]; _ isShow = YES;}-(void) dealloc {[_ backView release]; [_ mermerviewstobeadd release]; self. titleLabel = nil; self. titleBackgroundView = nil; self. titleIcon = nil; [super dealloc];} //-alertview animation + (void) exChangeOut :( UIView *) changeOutView dur :( CFTimeInterval) dur {CAKeyframeAnimation * animation; animation = [CAKeyframeAnimation animationWithKeyPath: @ "transform"]; animation. duration = dur; animation. removedOnCompletion = NO; animation. fillMode = modes; NSMutableArray * values = [NSMutableArray array]; [values addObject: [NSValue failed: Average (0.1, 0.1, 1.0)]; [values addObject: [NSValue failed: CATransform3DMakeScale (1.2, 1.2, 1.0)]; [values addObject: [NSValue hour: CATransform3DMakeScale (0.9, 0.9, 0.9)]; [values addObject: [NSValue hour: CATransform3DMakeScale (1.0, 1.0, 1.0)]; animation. values = values; animation. timingFunction = [CAMediaTimingFunction functionWithName: @ "easeInEaseOut"]; [changeOutView. layer addAnimation: animation forKey: nil];} @ end


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.