Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a UIAlertView bigger in iOS?

I am displaying a disclaimer UIAlertView in my iOS app, but the default AlertView size in iOS is very small. How can I make it bigger?

It seems like something that should be simple, but from what info I have searched for there does not seem to be a way to do it?

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];
like image 861
James Avatar asked Apr 27 '17 13:04

James


2 Answers

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

Then you can present it either 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 though. You will have to create a view in the center for your content, add a border for that view and keep everything around semitransparent.

You can also use a 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 also has to implement:

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

The source view is usually the button which opens the popover but I am using the parent view to ensure the popover is centered.

You will also have to add the buttons which are added automatically by UIAlertView but that should be trivial.

like image 146
Sulthan Avatar answered Nov 13 '22 19:11

Sulthan


You'll need to create a new class to accomplish this:

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 implemented cancel button- should give you an idea how to implement other buttons if you desire.
  • Nothing stops you from making the WebAlertView larger than its super view, so keep that in mind. There's also no accounting for length of title or cancelButton strings.
  • As others have mentioned, UIAlertView is deprecated.
  • Sulthan has given a good answer if you want to use a ViewController. My answer may be better if you really want something that works like UIAlertView.
like image 42
mmd1080 Avatar answered Nov 13 '22 19:11

mmd1080