簡體   English   中英

如何在iOS中使UIAlertView更大?

[英]How can I make a UIAlertView bigger in iOS?

我在我的iOS應用程序中顯示免責聲明UIAlertView,但iOS中的默認AlertView大小非常小。 我怎樣才能讓它更大?

這似乎應該是簡單的東西,但從我搜索的信息來看似乎沒有辦法做到這一點?

碼,

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];

首先,使用您的Web視圖創建一個視圖控制器,我們將其MyWebViewController

然后你可以將它作為全屏控制器呈現:

MyWebViewController* alertController = [[MyWebViewController alloc] init];
alertController.view.backgroundColor = [UIColor.lightGrayColor colorWithAlphaComponent:0.2];
alertController.modalPresentationStyle = UIModalPresentationOverFullScreen;

[self presentViewController:alertController animated:YES completion:nil];

這是一個全屏控制器。 您必須在中心為您的內容創建一個視圖,為該視圖添加邊框並將所有內容保持為半透明。

你也可以使用一個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];

代表還必須實施:

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

源視圖通常是打開彈出窗口的按鈕,但我使用父視圖來確保彈出窗口居中。

您還必須添加由UIAlertView自動添加的按鈕,但這應該是微不足道的。

您需要創建一個新類來完成此任務:

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

如何使用:

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];

筆記:

  • 只有實現取消按鈕 - 如果您願意,應該讓您知道如何實現其他按鈕。
  • 沒有什么能阻止你使WebAlertView比它的超級視圖更大,所以請記住這一點。 還沒有考慮title或cancelButton字符串的長度。
  • 正如其他人所提到的,UIAlertView已被棄用。
  • 如果你想使用ViewController,Sulthan給出了一個很好的答案。 如果你真的想要像UIAlertView這樣的東西,我的答案可能會更好。

根據Apple文檔 ,UIAlertView類旨在按原樣使用,不支持子類化。 此類的視圖層次結構是私有的,不得修改。

要自定義大小,請使用JSSAlertView而不是UIAlertView

使用NSLayoutConstraint並使UIAlertController的高度和寬度固定大小。

以下是代碼:

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];

但是UIAlertController的寬度是固定的。 使用NSLayoutConstraint UIAlertController的寬度沒有改變。

你不應該這樣做但是有一些黑客可以擴展整個UIAlertView。 UIAlertView始終顯示在新窗口中,技巧是縮放窗口。

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

UIAlertView在iOS 9.0中已棄用。 您需要開始使用UIAlertController。 據我所知,我們可以增加UIAlertController的大小。 我已經嘗試了所有方法。

你必須由自己的第三方創建自定義視圖:因為在UIAlertViewUIAlertController框架調整不起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM