2016-11-18 51 views
1

在我的應用我使用alamofire從Web API集成到我的應用程序解析JSON數據。我想在數據加載時添加加載微調器。我有一個看看:加載微調的Alamofire

Alamofire Loading Indicator

但只顯示上方的小網絡圖標。任何幫助都會很棒。

+0

您是否在尋找UIActivityIndi​​cator?您必須將其添加爲子視圖,然後調用開始或停止刷新。 –

+0

你能舉個例子嗎? – rob

+0

@rob檢查這一項http://stackoverflow.com/a/38457815/6433023它不是斯威夫特3,但你可以通過小的變化 –

回答

1

您正在尋找活動的指標視圖UIActivityIndicatorView。這必須手動添加。您可以通過故事板添加它,並啓用hidesWhenStopped,以便指示器在停止時不可見。

在任何情況下,您必須在請求開始前手動呼叫startAnimating,然後在請求完成時手動呼叫stopAnimating

做這一切編程會是這個樣子:

// Assuming in the view controller 
     let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .gray) // Create the activity indicator 
     view.addSubview(activityIndicator) // add it as a subview 
     activityIndicator.center = CGPoint(x: view.frame.size.width*0.5, y: view.frame.size.height*0.5) // put in the middle 
     activityIndicator.startAnimating() // Start animating 
     request.perform { data, error in 
      activityIndicator.stopAnimating() // On response stop animating 
      activityIndicator.removeFromSuperview() // remove the view 
      // ... process data 
     } 
+0

我會的「UIActivityIndi​​catorView」添加到我的tableView把它,將這段代碼進入相同的ViewController的TableView中 – rob

+0

你可以,但我不認爲它。在表格視圖中,指標會隨着內容一起滾動(可能是你想要的)。代碼會很相似,但是您將刪除所有「視圖」調用,因爲表視圖已經是視圖。因此,例如,而不是「view.addSubview(activityIndi​​cator)」你會叫「addSubview(activityIndi​​cator)」 –

+0

以表視圖和活動指示燈工作時也看看到UIRefreshControl。您可以在表視圖有一個屬性來設置刷新控件。然後,只需調用tv.refreshControl.beginRefreshing()。這主要用於下拉刷新。 –