How to Change the Height of Uitextfield in Uialertcontroller in Swift

How can I change the height of UITextField in UIAlertController in Swift?

This works with a constraint.

alertController.addTextField { textField in
let heightConstraint = NSLayoutConstraint(item: textField, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 100)
textField.addConstraint(heightConstraint)
}

result of adding the constraint

How to set the height of UITextField in the UIAlertController?

you have add below code:-

alertController.addTextField(configurationHandler: {(textField : UITextField!) -> Void in
textField.placeholder = ""
let heightConstraint = NSLayoutConstraint(item: textField, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 120)
textField.addConstraint(heightConstraint)
})

Hope it help you.

It's duplicate

How to change UITextField Font size within an Alert

Change textField.minimumFontSize = 100 line to textField.font = UIFont(name: "*whateverFontNameYouAreUsing*", size: 30). Let me know if this worked. Thanks.

check this code and font size in simulator here

Swift - size of textfield on UIAlertcontrller

Try :

    let reportAlertController = UIAlertController(title: "Report", message: "If you find anything to report, please write it in the text box below and press send. To cancel report, press cancel.", preferredStyle: .Alert)
reportAlertController.addTextFieldWithConfigurationHandler({ (tf) -> Void in
tf.frame.size.height = 1500
tf.placeholder = "Report detail(s) here"
})

reportAlertController.addAction(UIAlertAction(title: "Send", style: UIAlertActionStyle.Default, handler: nil))
reportAlertController.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: nil))
presentViewController(reportAlertController, animated: true, completion: nil)

reportAlertController.addAction(UIAlertAction(title: "Send", style: UIAlertActionStyle.Default, handler: nil))
reportAlertController.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: nil))
presentViewController(reportAlertController, animated: true, completion: { () -> Void in
self.layoutIfNeeded() //Update frame
})

UIAlertController - change size of text fields and add space between them

UIAlertController views are intended to be simple and not customizable. If you make your own presented view controller, then the view belongs to you and you can do anything you like.

How to set height and width of a UIAlertController in IOS 8

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,

  • 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:^{

    }];
  • implement delegate method for UIPopoverPresentationControllerDelegate: - (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller and return UIModalPresentationNone



Related Topics



Leave a reply



Submit