How can I make UIAlertView bigger in iOS?

I am showing a rejection of the UIAlertView in my iOS application, but the default AlertView size in iOS is very small. How can I do this more?

Something seems to be simple, but from what information I was looking for, there seems to be no way to do this?

code

UIAlertView* alert = [[UIAlertView alloc] initWithTitle: @"Disclaimer" message: nil delegate: self cancelButtonTitle: @"Accept" otherButtonTitles: nil]; UIWebView* webView = [[UIWebView alloc] init]; [webView setFrame:CGRectMake(0, 0, 280, 140)]; [webView loadHTMLString: html baseURL: nil]; UIView* view = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 280, 140)]; [view addSubview: webView]; [alert setValue: view forKey: @"accessoryView"]; [alert show]; 
+5
source share
7 answers

First create a view controller with your web view, call it MyWebViewController .

Then you can imagine it as a full-screen controller:

 MyWebViewController* alertController = [[MyWebViewController alloc] init]; alertController.view.backgroundColor = [UIColor.lightGrayColor colorWithAlphaComponent:0.2]; alertController.modalPresentationStyle = UIModalPresentationOverFullScreen; [self presentViewController:alertController animated:YES completion:nil]; 

This is a full screen controller. You will need to create a center view for your content, add a border for this view and keep everything around translucent.

You can also use popover:

 UIView *sourceView = self.view; MyWebViewController* alertController = [[MyWebViewController alloc] init]; alertController.modalPresentationStyle = UIModalPresentationPopover; alertController.preferredContentSize = CGRectInset(self.view.bounds, 20, 100).size; alertController.popoverPresentationController.canOverlapSourceViewRect = YES; alertController.popoverPresentationController.sourceView = sourceView; alertController.popoverPresentationController.sourceRect = CGRectMake(CGRectGetMidX(sourceView.bounds), CGRectGetMidY(sourceView.bounds), 0, 0); alertController.popoverPresentationController.permittedArrowDirections = 0; alertController.popoverPresentationController.delegate = self; [self presentViewController:alertController animated:YES completion:nil]; 

The delegate must also implement:

 - (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller traitCollection:(UITraitCollection *)traitCollection { return UIModalPresentationNone; } 

The original view is usually a button that opens the popover, but I use the parent view to ensure that the popover is centered.

You will also need to add buttons that are automatically added using the UIAlertView , but this should be trivial.

+6
source

To do this, you need to create a new class:

WebAlertView.h

 #import <UIKit/UIKit.h> @interface WebAlertView : UIView - (id)initWithTitle:(NSString *)title webView:(UIWebView *)webView delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle; - (void)show; @end @protocol WebAlertViewDelegate <NSObject> @optional - (void)webAlertViewCancel:(WebAlertView *)alertView; @end 

WebAlertView.m

 #import "WebAlertView.h" @interface WebAlertView() @property (weak, nonatomic) UIWebView *webView; @property (weak, nonatomic) NSString *title; @property (weak, nonatomic) NSString *cancelButtonTitle; @property (weak, nonatomic) id delegate; @property (strong, nonatomic) UIView *curtainView; @end @implementation WebAlertView - (id)initWithTitle:(NSString *)title webView:(UIWebView *)webView delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle { CGFloat titleViewHeight = 64.0; CGFloat cancelButtonHeight = 44.0; if ((self = [super initWithFrame:CGRectMake(0, 0, webView.frame.size.width, webView.frame.size.height+titleViewHeight+cancelButtonHeight)])) { self.backgroundColor = [UIColor groupTableViewBackgroundColor]; _webView = webView; _title = title; _cancelButtonTitle = cancelButtonTitle; _delegate = delegate; CGRect titleViewFrame = self.frame; titleViewFrame.size.height = titleViewHeight; UILabel *label = [[UILabel alloc] initWithFrame:titleViewFrame]; label.text = title; label.font = [UIFont boldSystemFontOfSize:16.0]; label.textAlignment = NSTextAlignmentCenter; [self addSubview:label]; CGRect webViewFrame = _webView.frame; webViewFrame.origin.y = titleViewHeight; _webView.frame = webViewFrame; [self addSubview:_webView]; CGRect cancelButtonFrame = self.frame; cancelButtonFrame.size.height = cancelButtonHeight; cancelButtonFrame.origin.y = self.frame.size.height - cancelButtonHeight; UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; button.frame = cancelButtonFrame; [button setTitle:cancelButtonTitle forState:UIControlStateNormal]; button.titleLabel.font = [UIFont boldSystemFontOfSize:16.0]; [button addTarget:self action:@selector(buttonTouchUpInside) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button]; } return self; } - (void)show { if ([_delegate isKindOfClass:[UIViewController class]]) { UIViewController *delegateViewController = (UIViewController *)_delegate; if (!_curtainView) { _curtainView = [[UIView alloc] initWithFrame:delegateViewController.view.bounds]; _curtainView.backgroundColor = [UIColor blackColor]; _curtainView.alpha = 0.5; } [delegateViewController.view addSubview:_curtainView]; self.center = delegateViewController.view.center; [delegateViewController.view addSubview:self]; } } - (void)drawRect:(CGRect)rect { self.layer.cornerRadius = 16.0; self.clipsToBounds = YES; } - (void)buttonTouchUpInside { [_curtainView removeFromSuperview]; [self removeFromSuperview]; if([_delegate respondsToSelector:@selector(webAlertViewCancel:)]) { [_delegate webAlertViewCancel:self]; } } @end 

How to use:

 UIWebView* webView = [[UIWebView alloc] init]; [webView setFrame:CGRectMake(0, 0, 280, 140)]; NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.google.com"]]; [webView loadRequest:urlRequest]; WebAlertView *webAlertView = [[WebAlertView alloc] initWithTitle:@"Disclaimer" webView:webView delegate:self cancelButtonTitle:@"Accept"]; [webAlertView show]; 

Notes:

  • Only an implemented cancel button - should give you an idea of ​​how to implement other buttons if you want.
  • There is nothing stopping you from making WebAlertView bigger than its super-view, so keep that in mind. It also does not take into account the length of the header or cancelButton lines.
  • As already mentioned, UIAlertView is deprecated.
  • The sultan gave a good answer if you want to use ViewController. My answer might be better if you really want something that works like a UIAlertView.
+4
source

According to the Apple Documentation , the UIAlertView class is intended to be used as is and does not support subclasses. The presentation hierarchy for this class is private and should not be changed.

To adjust the size, use JSSAlertView instead of UIAlertView

0
source

Use NSLayoutConstraint and create a fixed UIAlertController size in height and width.

Below is the code for this:

 UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"abcd" message:@"edfg" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *action = [UIAlertAction actionWithTitle:@"Done" style:UIAlertActionStyleDefault handler:nil]; [alert addAction:action]; NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:alert.view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:self.view.frame.size.height * 0.8]; NSLayoutConstraint *width = [NSLayoutConstraint constraintWithItem:alert.view attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1 constant:self.view.frame.size.width * 0.5]; [alert.view addConstraint:width]; [alert.view addConstraint:height]; [alert.view setBackgroundColor:[UIColor blackColor]]; [self presentViewController:alert animated:YES completion:nil]; 

Fixed width of UIAlertController . The use of the NSLayoutConstraint width of the UIAlertController has not changed.

0
source

You should not do this, but there is a hack to the whole UIAlertView. UIAlertView is always displayed in a new window, and the trick is scaling the window.

 [alert show] alert.window.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.3, 1.3); // Must be called after [alert show] 
0
source

UIAlertView is deprecated in iOS 9.0. You need to start using the UIAlertController. As far as I know, we can increase the size of the UIAlertController. I tried all the ways.

-1
source

you need to create a custom view by your own third party: because In UIAlertView and UIAlertController the frame setting does not work.

-1
source

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


All Articles