Swift - IOS : UIalertController 位置

Swift - IOS : UIalertController position

我正在尝试创建自定义 alertView,但我遇到了 Alert 大小的问题 所以我的问题是: 我怎么知道左角的X位置和右角的X位置?

您可以访问视图框架上的属性。 alertView.frame.minX 将为您提供左角 X,alertView.frame.maxX 将为您提供右角 X。

你可以获得类似的 x 位置,

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

y 也一样,

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

更新:(如评论中所述)

您可以像添加 height constraint 一样添加 width constraint,这样您的代码将类似于

    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);