2016-03-30 32 views
0

你好,我想表明我的UIViewController一個UIActivityIndicatorView正是這樣如何添加UIActivityIndi​​catorView背後黑色背景

enter image description here

我怎樣繪製UIViewIndicator這樣上圖中

我曾嘗試這

func showIndicatorView(){ 

     let loadingIndicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 50, 50)) 
     loadingIndicator.layer.cornerRadius = 05 

     loadingIndicator.opaque = false 
     loadingIndicator.backgroundColor = UIColor(white: 0.0, alpha: 0.6) 
     loadingIndicator.center = self.view.center; 
     loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray 
     loadingIndicator.color = UIColor.whiteColor() 
     loadingIndicator.startAnimating() 
     self.view.addSubview(loadingIndicator) 


    } 

回答

2

你可以把它放在另一個有黑色背景的視圖。像這樣的東西。如果您需要,也可以在該視圖中添加「加載...」標籤。

func showIndicatorView(){ 

    let loadingIndicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 50, 50)) 
    let backgroundView = UIView() 

    backgroundView.layer.cornerRadius = 05 
    backgroundView.clipsToBounds = true 
    backgroundView.opaque = false 
    backgroundView.backgroundColor = UIColor(white: 0.0, alpha: 0.6) 

    loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray 
    loadingIndicator.color = UIColor.whiteColor() 
    loadingIndicator.startAnimating() 

    let loadingLabel = UILabel() 
    loadingLabel.text = "Loading..." 
    let textSize: CGSize = loadingLabel.text!.sizeWithAttributes([NSFontAttributeName: loadingLabel.font ]) 

    loadingLabel.frame = CGRectMake(50, 0, textSize.width, textSize.height) 
    loadingLabel.center.y = loadingIndicator.center.y 

    backgroundView.frame = CGRectMake(0, 0, textSize.width + 70, 50) 
    backgroundView.center = self.view.center; 

    self.view.addSubview(backgroundView) 
    backgroundView.addSubview(loadingIndicator) 
    backgroundView.addSubview(loadingLabel) 
} 
+0

是的,我需要裝載..還...你能不能請補充說明,以及在你的代碼 – hellosheikh

+0

嗯,我可以把那也不過。 ..你使用XIB或故事板嗎?它的方式更簡單,更清潔,而不是從代碼中完成。 – Jelly

+0

我正在使用故事板 – hellosheikh