ios - 如何在 iOS 中使 UIAlertView 变大?

标签 ios objective-c user-interface uialertview

我在我的 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];

最佳答案

首先,使用您的 WebView 创建一个 View Controller ,我们称它为 MyWebViewController

然后您可以将其显示为全屏 Controller :

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

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

虽然这是一个全屏 Controller 。您必须在您的内容中心创建一个 View ,为该 View 添加边框并使所有内容保持半透明。

您还可以使用弹出框:

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

委托(delegate)人还必须实现:

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

源 View 通常是打开弹出窗口的按钮,但我使用父 View 来确保弹出窗口居中。

您还必须添加由 UIAlertView 自动添加的按钮,但这应该是微不足道的。

关于ios - 如何在 iOS 中使 UIAlertView 变大?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43659662/

相关文章:

ios - 从 Xamarin.iOS 中的 TableView 单元格以编程方式推送 segue

ios - 命令因信号 : Segmentation Fault 11 -- Xcode 8 Swift 3 而失败

java - 如何阻止 Swing 打开新框架?

Java - 强制文本字段格式 - UX - 00 :00:00;00

ios - 如何获取 UITextView 中光标之前的字符?

ios - SpriteKit中的加速度计不起作用

objective-c - 将xcode项目编译成库文件

ios - 如何在不中断游戏的情况下正确初始化 GameCenter 登录

objective-c - 当我用 webview 显示 viewcontroller 时 SIGKILL

过滤 ForEach 时,SwiftUI 导航链接不起作用