我在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];
答案 0 :(得分:6)
首先,使用您的网络视图创建一个视图控制器,我们将其称为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
自动添加的按钮,但这应该是微不足道的。
答案 1 :(得分:5)
您需要创建一个新类来完成此任务:
<强> 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];
注意:
答案 2 :(得分:0)
根据Apple Documentation,UIAlertView类旨在按原样使用,不支持子类化。此类的视图层次结构是私有的,不得修改。
要自定义尺寸,请使用JSSAlertView代替UIAlertView
答案 3 :(得分:0)
使用 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的宽度未更改。
答案 4 :(得分:0)
你不应该这样做,但是有一个黑客可以扩展整个UIAlertView。 UIAlertView始终显示在新窗口中,诀窍是缩放窗口。
[alert show]
alert.window.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.3, 1.3); // Must be called after [alert show]
答案 5 :(得分:0)
答案 6 :(得分:-1)
UIAlertView在iOS 9.0中已弃用。您需要开始使用UIAlertController。据我所知,我们可以增加UIAlertController的大小。我已经尝试了所有方法。
答案 7 :(得分:-1)
您必须由自己的第三方创建自定义视图:
因为在UIAlertView
和UIAlertController
框架调整不起作用。