ios 如何在 iPhone 屏幕中间显示活动指示器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8090579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display activity indicator in middle of the iphone screen?
提问by srinivas
When we draw the image, we want to display the activity indicator. Can anyone help us?
当我们绘制图像时,我们希望显示活动指示器。任何人都可以帮助我们吗?
回答by userar
UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
activityView.center=self.view.center;
[activityView startAnimating];
[self.view addSubview:activityView];
回答by Rogerio Chaves
If you are using Swift, this is how you do it
如果您使用的是 Swift,这就是您的操作方式
let activityView = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)
activityView.center = self.view.center
activityView.startAnimating()
self.view.addSubview(activityView)
回答by Suragch
Code updated for Swift 3
为 Swift 3 更新了代码
Swift code
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)
}
}
Notes
笔记
You can center the activity indicator in middle of the screen by adding it as a subview to the root
view
rather thanmyBlueSubview
.activityIndicator.center = CGPoint(x: self.view.bounds.size.width/2, y: self.view.bounds.size.height/2) self.view.addSubview(activityIndicator)
You can also create the
UIActivityIndicatorView
in the Interface Builder. Just drag an Activity Indicator Viewonto the storyboard and set the properties. Be sure to check Hides When Stopped. Use an outlet to callstartAnimating()
andstopAnimating()
. See this tutorial.
您可以通过将活动指示器作为子视图添加到根
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()
。请参阅本教程。
Remember that the default color of the
LargeWhite
style is white. So if you have a white background you can't see it, just change the color to black or whatever other color you want.activityIndicator.color = UIColor.black
A common use case would be while a background task runs. Here is an example:
// 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() } }
请记住,
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() } }
回答by D33pN16h7
This is the correct way:
这是正确的方法:
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];
Setup your autoresizing mask if you support rotation. Cheers!!!
如果您支持旋转,请设置您的自动调整大小蒙版。干杯!!!
回答by Anand
Try this way
试试这个方法
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.frame = CGRectMake(10.0, 0.0, 40.0, 40.0);
activityIndicator.center = super_view.center;
[super_view addSubview: activityIndicator];
[activityIndicator startAnimating];
回答by DAC84
If you want to add a text along with activity indicator please look at http://nullpointr.wordpress.com/2012/02/27/ios-dev-custom-activityindicatorview/
如果您想添加文本和活动指示器,请查看http://nullpointr.wordpress.com/2012/02/27/ios-dev-custom-activityindicatorview/
回答by Orkhan Huseynov
Swift 4, Autolayout version
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),
])
}
回答by Vasily Bodnarchuk
Swift 3, xcode 8.1
斯威夫特 3,Xcode 8.1
You can use small extension to place UIActivityIndicatorView in the centre of UIViewand inherited UIView classes:
您可以使用小扩展将UIActivityIndicatorView放置在 UIView和继承 UIView 类的中心:
Code
代码
extension UIActivityIndicatorView {
convenience init(activityIndicatorStyle: UIActivityIndicatorViewStyle, color: UIColor, placeInTheCenterOf parentView: UIView) {
self.init(activityIndicatorStyle: activityIndicatorStyle)
center = parentView.center
self.color = color
parentView.addSubview(self)
}
}
How to use
如何使用
let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge, color: .gray, placeInTheCenterOf: view)
activityIndicator.startAnimating()
Full Example
完整示例
In this example UIActivityIndicatorView placed in the centre of the ViewControllers view:
在这个例子中 UIActivityIndicatorView 放置在 ViewControllers 视图的中心:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge, color: .gray, placeInTheCenterOf: view)
activityIndicator.startAnimating()
}
}
extension UIActivityIndicatorView {
convenience init(activityIndicatorStyle: UIActivityIndicatorViewStyle, color: UIColor, placeInTheCenterOf parentView: UIView) {
self.init(activityIndicatorStyle: activityIndicatorStyle)
center = parentView.center
self.color = color
parentView.addSubview(self)
}
}
回答by Shaheen Ghiassy
If you want to center the spinner using AutoLayout, do:
如果要使用 AutoLayout 将微调器居中,请执行以下操作:
UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[activityView startAnimating];
[self.view addSubview:activityView];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:activityView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:activityView
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0.0]];
回答by Minakshi
You can set the position like this
你可以这样设置位置
UIActivityIndicatorView *activityView =
[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
activityView.frame = CGRectMake(120, 230, 50, 50);
[self.view addSubview:activityView];
Accordingly change the frame size.....
相应地改变帧大小.....