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.
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?
I do not think you can set the size. Bad workaround is, to set \n
on the message. UIAlertView also has same limitation.
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.
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.
Edited with sample code
First, sorry, I mean UIPopoverPresentViewController
, not UIPopoverController
Here is the sample class:
@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
Then on presentingViewController,
UIPopoverPresentationControllerDelegate
delegatethen 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:^{
}];
implement delegate method for UIPopoverPresentationControllerDelegate
: - (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller
and return UIModalPresentationNone