活动指示器不在TableView的中心

时间:2017-01-26 06:28:22

标签: ios swift swift3 uiactivityindicatorview

我想在activity indicator中使用TableView文件显示NIB/XIB,但显示的不是中心,而是放在右侧

我做:

override func viewDidLoad() {
    super.viewDidLoad()

let actInd: UIActivityIndicatorView = UIActivityIndicatorView()
    actInd.frame = CGRect(x: 0.0, y: 0.0, width: 40.0, height: 40.0)
    actInd.center = self.view.center
    actInd.hidesWhenStopped = true
    actInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.whiteLarge
    self.view.addSubview(actInd)
    actInd.startAnimating()
}

3 个答案:

答案 0 :(得分:1)

let actInd: UIActivityIndicatorView = UIActivityIndicatorView()

override func viewDidLoad() {
    super.viewDidLoad()

    actInd.frame = CGRect(x: 0.0, y: 0.0, width: 40.0, height: 40.0)
    actInd.hidesWhenStopped = true
    actInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.whiteLarge
    self.view.addSubview(actInd)
    actInd.startAnimating()
}


override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    actInd.center = self.view.center
}

/* Following two functions are acceptable also.
override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    actInd.center = self.view.center
}

override func viewDidLayoutSubviews() { 
    super.viewDidLayoutSubviews()
    actInd.center = self.view.center
}
*/

答案 1 :(得分:1)

你可以:

  • 在您的XIB / Storyboard
  • 中添加UIActivityIndicator
  • 将其设置为hidden(在故事板中)
  • 创建IBOutlet
  • 当你想要显示/隐藏它时,
  • 切换spinner.isHidden

答案 2 :(得分:1)

这种方法对我有用。

    if let window = UIApplication.shared.keyWindow { 
        activityIndicator.frame = CGRect(x: window.frame.width/2, y: window.frame.height/2, width: 0, height: 0)
        activityIndicator.hidesWhenStopped = true
        activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.white

        window.addSubview(activityIndicator)
    }

希望有所帮助:)