UIActivityIndi​​catorView没有出现在我的UITableViewCell中

时间:2019-06-03 12:28:19

标签: swift uitableview uiactivityindicatorview

我只想在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手动插入主线程的结果:

result without main thread

当我将UIActivityIndicatorView手动放入主线程时,结果如下:

   DispatchQueue.main.async {
      self._setActivityIndicatorConstraints()
   }

result with main thread

0 个答案:

没有答案