簡體   English   中英

在UITableView上滾動時,UIActivityIndi​​catorView消失

[英]UIActivityIndicatorView goes away when scrolling on UITableView

我在Swift的UITableViewController中創建了一個UIActivityIndi​​catorView,如下所示:

indicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) as UIActivityIndicatorView

indicator.center = self.view.center

indicator.hidesWhenStopped = true

indicator.style = UIActivityIndicatorView.Style.white

indicator.backgroundColor = UIColor(white: 0.0, alpha: 0.6)

indicator.layer.cornerRadius = 15

self.view.addSubview(indicator)

self.indicator.startAnimating()

這就像一個魅力,但是當它運行並且在UITableView Controller上滾動時,UIActivityIndi​​catorView不會隨之滾動。 如何使UIActivityIndi​​catorView與UITableViewController一起滾動。

您可以將其添加到窗口中

let wind = (UIApplication.shared.delegate as! AppDelegate).window! 
wind.addSubview(indicator)

去除

indicator.removeFromSuperview()

您也可以在vc中使用

override func scrollViewDidScroll(_ scrollView: UIScrollView) {
    indicator.frame.origin = CGPoint(x: indicator.frame.origin.x, y: UIScreen.main.bounds.height / 2 - 50 + scrollView.contentOffset.y)
}

您的問題和標題似乎提出了不同的問題。 在問題中,您詢問如何使其隨滾動移動,在標題中,您詢問如何使其不滾動。

有幾種方法可以做到這一點(滾動)。 一種方法是實現func scrollViewDidScroll(_ scrollView: UIScrollView) { }並在滾動視圖(UITableView畢竟是scrollView)滾動時移動活動指示器。 另一種方法(我個人更喜歡)是將活動指示器放在表格視圖單元格中。 單元格始終以表格視圖滾動。

您需要做的是將indicator.center設置為其超級視圖范圍的中心。 這些值在相同的坐標系中(這里是cell.like的坐標系)。

indicator.setCenter(CGPointMake(CGRectGetMidX(cell.like.bounds),
                             CGRectGetMidY(cell.like.bounds)));

我認為UITableView背后的指標。 您是否嘗試將指示器放在前面?

self.view.bringSubview(toFront: indicator)

實際上,它滾動時沒有任何問題,所以我認為您的代碼中沒有錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM