UIAlertView buttons (subviews) not showing up in iOS 7

UIAlertView works fine in ios 6 with the following code. But when it comes to ios 7, subviews (yes and no buttons in my code) are not displayed, when alertview is called, only a text message is displayed. will someone tell me how to solve this problem?

viewController.m file

[Utilities prCustomAlert:@"Textmessage" inTitle:@"Alert view title" delegate:self inTag:300]; CustomAlertView *alertView = [Utilities sharedUtility].customAlertView; alertView.numberOfBtns = 2; UIButton *btn= (UIButton *)[alertView viewWithTag:10]; [btn setTitle:@"no" forState:UIControlStateNormal]; [btn addTarget:self action:@selector(dontlogout) forControlEvents:UIControlEventTouchDown]; btn = (UIButton *)[alertView viewWithTag:11]; [btn setTitle:@"yes" forState:UIControlStateNormal]; [btn addTarget:self action:@selector(logout) forControlEvents:UIControlEventTouchDown]; [Utilities displayCustomAlertForDelegate:self]; 

UIAlertView.m File

  CGRect viewFrame = self.frame; CGRect buttonFrame = button.frame; if(self.numberOfBtns==2){ CGRect labelFrame = [self viewWithTag:15].frame; button.frame = CGRectMake(10, 0, 40, 30); button.hidden = NO; //yes... btn = (UIButton *)[self viewWithTag:11]; btn.frame = CGRectMake(60, 0, 40, 30); btn.hidden = NO; //no.. btn = (UIButton *)[self viewWithTag:10]; btn.hidden = YES; } 
+6
source share
3 answers

We can add subviews to a UIAlerView by adding a subview to the presented ViewController when the UIAlertView is presented. I accessed the UIAlertView as follows:

NSArray * subviews = [UIApplication sharedApplication] .keyWindow.rootViewController.presentedViewController.view.subviews;

I created a subclass of UIAlerView:

Header file:

 @interface MLKLoadingAlertView : UIAlertView - (id)initWithTitle:(NSString *)title; @end 

Implementation File:

 #import "MLKLoadingAlertView.h" #define ACTIVITY_INDICATOR_CENTER CGPointMake(130, 90) @implementation MLKLoadingAlertView - (id)initWithTitle:(NSString *)title { if ( self = [super init] ) { self.title = title; self.message = @"\n\n"; [self setDelegate:self]; } return self; } // You can Customise this based on your requirement by adding subviews. - (void)didPresentAlertView:(UIAlertView *)alertView { NSArray *subviews = [UIApplication sharedApplication].keyWindow.rootViewController.presentedViewController.view.subviews; if( subviews.count > 1 ) { // iOS while presenting an alertview uses a presening view controller. That controller view has several subviews. I have picked one // subview from it which has frame similar to the alertview frame. UIView *presentedView = [subviews objectAtIndex:1]; UIActivityIndicatorView *customActivityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; [customActivityIndicator startAnimating]; customActivityIndicator.center = ACTIVITY_INDICATOR_CENTER; [presentedView addSubview:customActivityIndicator]; } } @end 

In - (void) didPresentAlertView: (UIAlertView *) alertView method I added subviews to the UIAlertView, referring to the Viewed View Controller.

You can find an explanation and sample code for this on Here

+1
source

I think the root of the problem in iOS7 is that Apple has changed the appearance mechanism of UIAlertView. From this point on, any manifestation of alertView follows after starting two private view dispatchers

 _UIModalItemAppViewController _UIModalItemsPresentingViewController 

In other words, now UIAlertView is not a pure view - it is part of some complex set of view controllers with a full controller cycle.

But the good news is that you can change the accessory in your customContentView in the standard warning view

 [alertView setValue:customContentView forKey:@"accessoryView"]; 

Note that you must call this before [alertView show].

+1
source

What you did was always wrong. You are not allowed to add your own routines to the UIAlertView. The good news is that you don’t have to in iOS 7! The new custom transition animation mechanism allows you to create your own view that will only view alerts, but since this is your view, you can put whatever you like into it, for example:

enter image description here

Notice how the fake "alert view" floats in front of the original view on the screen shot to the right and darkens the screen behind it, just like the actual alert view. But it consists solely of user-generated content; The "real" kind of warning can never contain an image and a switch!

For code creating this view that you can easily adapt for your purposes, see my github site: https://github.com/mattneub/custom-alert-view-iOS7

+1
source

Source: https://habr.com/ru/post/954435/


All Articles