2017-07-25 54 views
0

我想在每個集合視圖單元內放置一個UIActivityIndi​​catorView,因爲它下載圖像。我有它出現在每個單元格中,但它拒絕自己居中。它停留在左上角。我怎樣才能讓它正確居中?如何在UICollectionViewCell中集中UIActivityIndi​​catorView?

這裏是我如何做它:

extension UIView { 

    func showActivityIndicator(onView: UIView, withIndicator: UIActivityIndicatorView) { 
     withIndicator.frame = CGRect(x: onView.frame.midX - 20, y: onView.frame.midY - 20, width: 40, height: 40) 
     withIndicator.activityIndicatorViewStyle = .whiteLarge 
     withIndicator.center = onView.center 
     onView.addSubview(withIndicator) 
     withIndicator.startAnimating() 
    } 
} 

我調用該函數裏面cellForItemAtIndexPath像:

showActivityIndicator(onView: cell.contentView, withIndicator: activityInd) 

但沒有我這樣做會從左上角移動。有什麼建議?

+0

爲什麼你將一個視圖傳遞給一個擴展到'UIView'的方法?你應該把它命名爲'cell.contentView.showActivityIndi​​cator(activityInd)',方法簽名應該是'func showActivityIndi​​cator(_ indicatorView:UIActivityIndi​​catorView)'。然後,在方法中所有對'onView'的引用都應該用'self'替換。 – rmaddy

+0

我發現網上很快,但好點,我改變了。謝謝! –

回答

3

試試這個

withIndicator.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2); 
+0

這工作,並且是最簡單的答案。 –

+0

謝謝@CodyLucas很高興我可以幫忙,你介意接受答案是否正確:) –

+0

是的,它只是需要一分鐘才能被允許。 –

1

您需要添加約束來居中它。例如使用NSLayoutAnchor

您需要將translatesAutoresizingMaskIntoConstraints設置爲false來設置您的約束條件。並將其添加到視圖後設置的約束(提示代碼中的註釋):

extension UIView { 

    func showActivityIndicator(onView: UIView, withIndicator: UIActivityIndicatorView) { 
     withIndicator.frame = CGRect(x: onView.frame.midX - 20, y: onView.frame.midY - 20, width: 40, height: 40) 
     withIndicator.activityIndicatorViewStyle = .whiteLarge 

     // set translatesAutoresizingMaskIntoConstraints to false to set your constraints 
     withIndicator.translatesAutoresizingMaskIntoConstraints = false 
     onView.addSubview(withIndicator) 
     withIndicator.startAnimating() 

     // add the constraints to center the indicator 
     withIndicator.centerXAnchor.constraint(equalTo: onView.centerXAnchor).isActive = true 
     withIndicator.centerYAnchor.constraint(equalTo: onView.centerYAnchor).isActive = true 
    } 

} 
0

我建議使用約束(又名自動佈局):

indicator.translatesAutoresizingMaskIntoConstraints = false 
"your cell".addSubview(indicator) 

let widthConstraint = NSLayoutConstraint(item: indicator, attribute: .Width, relatedBy: .Equal, 
               toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 250) 

let heightConstraint = NSLayoutConstraint(item: indicator, attribute: .Height, relatedBy: .Equal, 
                toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100) 

let xConstraint = NSLayoutConstraint(item: indicator, attribute: .CenterX, relatedBy: .Equal, toItem: self.tableView, attribute: .CenterX, multiplier: 1, constant: 0) 

let yConstraint = NSLayoutConstraint(item: indicator, attribute: .CenterY, relatedBy: .Equal, toItem: self.tableView, attribute: .CenterY, multiplier: 1, constant: 0) 

NSLayoutConstraint.activateConstraints([widthConstraint, heightConstraint, xConstraint, yConstraint]) 
相關問題