2017-04-27 7 views
5

iOSアプリケーションで免責事項UIAlertViewを表示していますが、iOSのデフォルトのAlertViewサイズは非常に小さいです。どうすればそれを大きくすることができますか?iOSでUIAlertViewをもっと大きくするにはどうすればよいですか?

シンプルにする必要がありますが、私が検索した情報から、それを行う方法がないようです。

コード、

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

カスタムコントローラを作成して提示します。 'UIAlertView'を使用しないでください(あなたは実際にカスタムビューを追加するために' UIAlertView'をハックしています)。 – Sulthan

+2

カスタムビューを使用する –

+0

'UIAlertView'は' UIAlertController'のために推奨されなくなりました。 – Losiowaty

答えて

0

Apple Documentationによれば、UIAlertViewクラスはそのままであり、サブクラス化をサポートしていない使用されることが意図されます。このクラスのビュー階層はプライベートであり、変更することはできません。

サイズをカスタマイズするには、JSSAlertView代わりにUIAlertView

+0

サンプルコードをご覧ください – James

6

ファーストを使用して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; 
} 

ソースビューは通常、ポップオーバーを開くボタンですが、私は親ビューを使用していますpopoverが確実に中央に配置されます。

また、UIAlertViewによって自動的に追加されるボタンを追加する必要がありますが、それは簡単なはずです。

-1

UIAlertViewはiOS 9.0では非推奨です。 UIAlertControllerの使用を開始する必要があります。私の知る限りでは、UIAlertControllerのサイズを増やすことができます。私はすべての方法を試みました。

-1

およびUIAlertControllerフレーム調整が機能しないため、独自のサードパーティ製カスタムビューを作成する必要があります。 。

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の幅が固定されています。 Use NSLayoutConstraint UIAlertControllerの幅は変更されません。

3

あなたは、この達成するために新しいクラスを作成する必要があります:

WebAlertViewを。時間

#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のようなものを望むなら、私の答えはより良いかもしれません。
0

あなたはそうするはずはありませんが、UIAlertView全体を拡大縮小するためのハックがあります。 UIAlertViewは常に新しいウィンドウに表示され、トリックはウィンドウを拡大/縮小することです。

[alert show] 
alert.window.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.3, 1.3); // Must be called after [alert show] 
関連する問題