如何在iOS 8中设置UIAlertController的高度和宽度
我有一个带有文本文件的UIAlertController. 问题在于默认的UIAlertController的尺寸非常小.它的文字无法正确显示.
I have a UIAlertController with an TextFile. The problem is that the default UIAlertController is a very small size. It's text cannot be seen properly.
因此,我想增加UIAlertController的高度和宽度.换句话说,我想创建一个自定义UIAlertController.这样做的方式是什么?
So, I want to increase the height and width of the UIAlertController. In other words, I want to create a custom UIAlertController. What would be the way to do that?
我认为您不能设置大小.坏的解决方法是在邮件上设置\n
. UIAlertView也有相同的限制.
I do not think you can set the size. Bad workaround is, to set \n
on the message. UIAlertView also has same limitation.
我将建议使用UIPopoverController并实现您自己的dismiss
按钮,因为UIAlertController的目的更多是根据Apple文档向用户显示警报消息(短消息).我认为消息墙不可以视为警报消息.
I will suggest to use UIPopoverController and implement your own dismiss
button, since UIAlertController's purpose is more to display alert messages to the user (short message) per Apple Documentation. I do not think wall of message can be considered as alert message.
通常,我认为Apple设置了此限制,以提醒用户该视图是向用户显示短消息作为其UX的一部分.
Generally, I think this limitation is set by Apple as a reminder this view is to display short message to users as part of their UX.
使用示例代码编辑
首先,对不起,我的意思是UIPopoverPresentViewController
,而不是UIPopoverController
Edited with sample code
First, sorry, I mean UIPopoverPresentViewController
, not UIPopoverController
这是示例类:
@interface DemoPopOverPresentViewController : UIViewController
- (instancetype)initWithTitle:(NSString*)title message:(NSString*)message buttonTitle:(NSString*)buttonTitle;
@property NSString* titleText;
@property NSString* messageText;
@property NSString* buttonTitleText;
@property UILabel* titleLabel;
@property UILabel* textLabel;
@property UIButton* submitButton;
@end
@implementation DemoPopOverPresentViewController
- (instancetype)initWithTitle:(NSString*)title message:(NSString*)message buttonTitle:(NSString*)buttonTitle;
{
self = [super init];
if ( self ) {
_titleText = title;
_messageText = message;
_buttonTitleText = buttonTitle;
}
return self;
}
- (void)viewDidLoad;
{
[super viewDidLoad];
_titleLabel = [UILabel new];
[_titleLabel setTextAlignment:NSTextAlignmentCenter];
[_titleLabel setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleHeadline]];
[_titleLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[_titleLabel setText:_titleText];
[self.view addSubview:_titleLabel];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[_titleLabel]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_titleLabel)]];
_textLabel = [UILabel new];
[_textLabel setTextAlignment:NSTextAlignmentCenter];
[_textLabel setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleBody]];
[_textLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[_textLabel setNumberOfLines:0];
[_textLabel setText:_messageText];
[self.view addSubview:_textLabel];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[_textLabel]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_textLabel)]];
_submitButton = [UIButton buttonWithType:UIButtonTypeSystem];
[_submitButton setTitle:_buttonTitleText forState:UIControlStateNormal];
[_submitButton addTarget:self action:@selector(submitButtonTouched:) forControlEvents:UIControlEventTouchUpInside];
[_submitButton setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:_submitButton];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[_submitButton]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_submitButton)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[_titleLabel(<=44.0)]-16-[_textLabel]-16-[_submitButton(<=44.0)]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_titleLabel,_textLabel,_submitButton)]];
}
- (void)submitButtonTouched:(id)sender;
{
[self dismissViewControllerAnimated:YES completion:^{
}];
}
@end
然后在presentingViewController上,
Then on presentingViewController,
- 首先,它将需要实现
UIPopoverPresentationControllerDelegate
委托 -
然后初始化该类:
- first, it will need to implement
UIPopoverPresentationControllerDelegate
delegate then to initialise the class:
DemoPopOverPresentViewController* controller = [[DemoPopOverPresentViewController alloc] initWithTitle:@"Info" message:@"The quick brown fox jumps over the lazy dog" buttonTitle:@"Dismiss"];
controller.modalPresentationStyle = UIModalPresentationPopover;
// set the content size of your 'alert view'
controller.preferredContentSize = CGSizeMake(200.0, 150.0);
UIPopoverPresentationController* pc = [controller popoverPresentationController];
pc.sourceView = self.view;
pc.delegate = self;
pc.sourceRect = CGRectMake(self.view.frame.size.width/2.0, self.view.frame.size.height/2.0, 0.0, 0.0);
pc.permittedArrowDirections = NULL;
[self presentViewController:controller animated:YES completion:^{
}];
UIPopoverPresentationControllerDelegate
的实现委托方法:- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller
并返回UIModalPresentationNone
implement delegate method for UIPopoverPresentationControllerDelegate
: - (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller
and return UIModalPresentationNone