我只想在UIActivityIndicatorView
中显示一个UITableViewCell
,但它没有出现
当我将UIActivityIndicatorView
放在主线程中时,其他视图将消失。这很奇怪,因为UIActivityIndicatorView
在我手动添加之前已经在主线程上
override init(style: UITableViewCellStyle, reuseIdentifier: String?)
{
super.init(style: style, reuseIdentifier: reuseIdentifier)
// Set subviews constraints
self._setMainContainerViewConstraints(container: self.contentView)
}
/**
** MARK: - Private constraints methods
*/
private func _setMainContainerViewConstraints(container: UIView) {
self._mainContainerView.backgroundColor = .blue
self._mainContainerView.translatesAutoresizingMaskIntoConstraints = false
self.contentView.addSubview(self._mainContainerView)
// Set borders constraints
self._mainContainerView.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant: 16).isActive = true
self._mainContainerView.leftAnchor.constraint(equalTo: self.contentView.leftAnchor, constant: 16).isActive = true
self._mainContainerView.rightAnchor.constraint(equalTo: self.contentView.rightAnchor, constant: -16).isActive = true
self._mainContainerView.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: -16).isActive = true
// Add subviews
self._setActivityIndicatorConstraints()
}
private func _setActivityIndicatorConstraints() {
if Thread.isMainThread {
print("Main thread")
}
self._activityIndicator.backgroundColor = .green
self._activityIndicator.translatesAutoresizingMaskIntoConstraints = false
self._mainContainerView.addSubview(self._activityIndicator)
// Set borders constraints
self._activityIndicator.topAnchor.constraint(equalTo: self._mainContainerView.topAnchor, constant: 16).isActive = true
self._activityIndicator.leftAnchor.constraint(equalTo: self._mainContainerView.leftAnchor, constant: 16).isActive = true
self._activityIndicator.rightAnchor.constraint(equalTo: self._mainContainerView.rightAnchor, constant: -16).isActive = true
self._activityIndicator.bottomAnchor.constraint(equalTo: self._mainContainerView.bottomAnchor, constant: -16).isActive = true
self._activityIndicator.heightAnchor.constraint(equalToConstant: 30).isActive = true
self._mainContainerView.bringSubview(toFront: self._activityIndicator)
self._activityIndicator.startAnimating()
self._activityIndicator.isHidden = false
}
“主线程”出现在控制台中,因此我不需要手动将UIActivityIndicatorView
放入主线程
无需将UIActivityIndicatorView
手动插入主线程的结果:
当我将UIActivityIndicatorView
手动放入主线程时,结果如下:
DispatchQueue.main.async {
self._setActivityIndicatorConstraints()
}