在UIAlertController中定位微调器

时间:2015-07-21 02:02:28

标签: swift

我有UIAlertController标题和微调器。我需要在标题下方显示微调器,但我很难定位它。事实上,我将spinner.center设置为屏幕的中心,但实际上它出现在右下角。

var alert: UIAlertController = UIAlertController(title: "Fetching New Music", message: nil, preferredStyle: UIAlertControllerStyle.Alert)

var spinner: UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)

spinner.color = UIColor.yellowColor()

spinner.startAnimating()

alert.view.addSubview(spinner)

var height:NSLayoutConstraint = NSLayoutConstraint(item: alert.view, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: self.view.frame.height * 0.15)

alert.view.addConstraint(height)

// Here is where I need help!          
spinner.center = CGPointMake((self.view.frame.width / 2) - (spinner.frame.width / 2), (self.view.frame.height / 2) - (spinner.frame.height / 2))

self.presentViewController(alert, animated: true, completion: nil)

1 个答案:

答案 0 :(得分:0)

这两行合在一起毫无意义:

alert.view.addSubview(spinner)
spinner.center = CGPointMake((self.view.frame.width / 2) - (spinner.frame.width / 2), (self.view.frame.height / 2) - (spinner.frame.height / 2))

您似乎没有理解framebounds之间的根本区别。如果要将微调器添加为警报视图的子视图(如第一行所示),则微调器center的位置(如第二行所示)的定义与警报视图的界限,而非某些其他对象的框架