Anthony Anthony - 2 months ago 69
Swift Question

Swift - IOS : UIalertController position

I'm trying to create a custom alertView but i'm facing a problem with the size of the Alert
so my question is :
how could i know the X position of the left corner and the x position of the right corner ?

Answer

You can get x position something like,

  x = self.view.frame.size.width/2.0 - yourCustomAlertView.frame.size.width/ 2.0

and same for y also,

  y = self.view.frame.size.height/2.0 - yourCustomAlertView.frame.size.height/ 2.0

Update : (As Asked in comment)

You can add width constraint same as you have added height constraint so your code will be look like,

    var height:NSLayoutConstraint = NSLayoutConstraint(item: alertController.view, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 200);

    var width : NSLayoutConstraint = NSLayoutConstraint(item: alertController.view, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 300);

    alertController.view.addConstraint(height);

     alertController.view.addConstraint(width);