我在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];
这是一个全屏控制器。您将必须在中心为内容创建一个视图,为该视图添加一个边框,并将所有内容保持在半透明的范围内。
您也可以使用弹出窗口:
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大于其超级视图,因此请记住这一点。标题的长度或取消按钮字符串也没有考虑。
- 正如其他人所提到的,UialertView被弃用。
- 如果您想使用ViewController,Sulthan给出了一个很好的答案。如果您真的想要像Uialertview这样有效的东西。
根据Apple文档,UialerTview类旨在用作IS,并且不支持子分类。此类的视图层次结构是私人的,不得修改。
要自定义大小,请使用JSSALERTVIEW而不是UialertView
uialertView在iOS 9.0中弃用。您需要开始使用UialertController。据我所知,我们可以增加uialertController的大小。我已经尝试了所有的方式。
您必须通过自己的第三方创建自定义视图:因为在UIAlertView
和UIAlertController
框架调整不起作用。
使用 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的宽度未更改。
您不应该这样做,但是有hack可以扩展整个uialertView。uialertView始终显示在新窗口中,诀窍是扩展窗口。
[alert show]
alert.window.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.3, 1.3); // Must be called after [alert show]