[英]Swift - IOS : UIalertController position
我正在尝试创建一个自定义alertView,但我遇到了Alert的大小问题,所以我的问题是:我怎么知道左角的X位置和右角的x位置?
You can access properties on the frame of the view. 您可以访问视图框架上的属性。
alertView.frame.minX
will get you the left corner X and alertView.frame.maxX
will get you the right corner X. alertView.frame.minX
会让你左上角X和alertView.frame.maxX
会让你右上角X.
You can get x position something like, 你可以得到像x这样的位置,
x = self.view.frame.size.width/2.0 - yourCustomAlertView.frame.size.width/ 2.0
and same for y also, 和y一样,
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, 你可以添加
width constraint
就像你添加了height 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.