我有一个带有TextFile的UIAlertController。问题是默认的UIAlertController是一个非常小的尺寸。它的文字无法正常看到。
所以,我想增加UIAlertController的高度和宽度。换句话说,我想创建一个自定义的UIAlertController。这样做的方法是什么?
我不认为你可以设置大小。不好的解决方法是,在邮件上设置\n
。 UIAlertView也有同样的限制。
我建议使用UIPopoverController并实现自己的dismiss
按钮,因为UIAlertController的目的是为每个Apple文档向用户显示警报消息(短消息)。我不认为消息墙可以被视为警报消息。
一般来说,我认为这个限制是由Apple设定的,提醒这个视图是向用户显示短信,作为其用户体验的一部分。
用示例代码编辑首先,抱歉,我的意思是UIPopoverPresentViewController
,而不是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
然后在presentViewController上,
UIPopoverPresentationControllerDelegate
委托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
我知道这已关闭但我发现你可以像这样添加alertviewcotnroller.view的约束
var height:NSLayoutConstraint = NSLayoutConstraint(item: alertController.view, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: self.view.frame.height * 0.80)
alertController.view.addConstraint(height);
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
}]];
UIViewController *viewController = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
if ( viewController.presentedViewController && !viewController.presentedViewController.isBeingDismissed ) {
viewController = viewController.presentedViewController;
}
**NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:alert.view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationLessThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:viewController.view.frame.size.height*0.8f];
[alert.view addConstraint:constraint];**
[viewController presentViewController:alert animated:YES completion:^{
}];
这当然可以帮到你。 (斯威夫特4.2)
let alert = UIAlertController(title: "Alert", message: "Some message", preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action) in
}))
alert.addAction(UIAlertAction(title: "Some", style: .default))
alert.addAction(UIAlertAction(title: "Some", style: .default))
let height:NSLayoutConstraint = NSLayoutConstraint(item: alert.view, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: view.frame.height)
alert.view.addConstraint(height);
present(alert, animated: true)