use*_*rar 145
UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc]     
        initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    activityView.center=self.view.center;
    [activityView startAnimating];
    [self.view addSubview:activityView];
Rog*_*ves 42
如果您使用的是Swift,这就是您的操作方法
let activityView = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)
activityView.center = self.view.center
activityView.startAnimating()
self.view.addSubview(activityView)
Sur*_*gch 14
代码已更新为Swift 3
SWIFT代码
import UIKit
class ViewController: UIViewController {
    let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.whiteLarge)
    @IBOutlet weak var myBlueSubview: UIView!
    @IBAction func showButtonTapped(sender: UIButton) {
        activityIndicator.startAnimating()
    }
    @IBAction func hideButtonTapped(sender: UIButton) {
        activityIndicator.stopAnimating()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        // set up activity indicator
        activityIndicator.center = CGPoint(x: myBlueSubview.bounds.size.width/2, y: myBlueSubview.bounds.size.height/2)
        activityIndicator.color = UIColor.yellow
        myBlueSubview.addSubview(activityIndicator)
    }
}
笔记
您可以将活动指示器置于屏幕中间,方法是将其作为子视图添加到根目录view而不是myBlueSubview.
activityIndicator.center =  CGPoint(x: self.view.bounds.size.width/2, y: self.view.bounds.size.height/2)
self.view.addSubview(activityIndicator)
您也可以UIActivityIndicatorView在Interface Builder中创建它.只需将活动指示器视图拖到故事板上并设置属性即可.务必检查隐藏时的隐藏.使用插座呼叫startAnimating()和stopAnimating().请参阅本教程.
请记住,LargeWhite样式的默认颜色为白色.因此,如果您有白色背景,则无法看到它,只需将颜色更改为黑色或任何其他颜色即可.
activityIndicator.color = UIColor.black
一个常见的用例是运行后台任务.这是一个例子:
// start animating before the background task starts
activityIndicator.startAnimating()
// background task
DispatchQueue.global(qos: .userInitiated).async {
    doSomethingThatTakesALongTime()
    // return to the main thread
    DispatchQueue.main.async {
        // stop animating now that background task is finished
        self.activityIndicator.stopAnimating()
    }
}
D33*_*6h7 13
这是正确的方法:
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.center = CGPointMake(self.view.frame.size.width / 2.0, self.view.frame.size.height / 2.0);
[self.view addSubview: activityIndicator];
[activityIndicator startAnimating];
[activityIndicator release];
如果支持旋转,请设置自动调整遮罩.干杯!!!
小智 5
Swift 4,自动布局版本
func showActivityIndicator(on parentView: UIView) {
    let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .gray)
    activityIndicator.startAnimating()
    activityIndicator.translatesAutoresizingMaskIntoConstraints = false
    parentView.addSubview(activityIndicator)
    NSLayoutConstraint.activate([
        activityIndicator.centerXAnchor.constraint(equalTo: parentView.centerXAnchor),
        activityIndicator.centerYAnchor.constraint(equalTo: parentView.centerYAnchor),
    ])
}
| 归档时间: | 
 | 
| 查看次数: | 124671 次 | 
| 最近记录: |