xcode 在 UIImageView 中居中 UIActivityIndicatorView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10781291/
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
Center UIActivityIndicatorView in a UIImageView
提问by Wasim
I've created a custom subclass of UIImageView
and in one method I want to add an activity indicator to the center while the image is loading. I'm using the following code, but the activity monitor is positioned well outside of the UIImageView
. What can I do to center it exactly within the bounds of the UIImageView
?
我已经创建了一个自定义子类,UIImageView
并且在一种方法中我想在加载图像时向中心添加一个活动指示器。我正在使用以下代码,但活动监视器位于UIImageView
. 我该怎么做才能将它完全集中在 的范围内UIImageView
?
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
indicator.autoresizingMask =
UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleBottomMargin |
UIViewAutoresizingFlexibleLeftMargin;
indicator.center = self.center;
[self addSubview:indicator];
[indicator startAnimating];
回答by Vladimir
center
property is view's coordinates in coordinate space of its superview. So you need to set coordinates of your indicator in the coordinate space of imageView. Correct way will be:
center
属性是视图在其父视图坐标空间中的坐标。因此,您需要在 imageView 的坐标空间中设置指标的坐标。正确的方法是:
indicator.center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
回答by Oleg Trakhman
indicator.center = [self convertPoint:self.center fromView:[self superview]];
You only need to convert point because of this (UIView Class Reference):
因为这个,你只需要转换点(UIView Class Reference):
@property(nonatomic) CGPoint center Discussion
The center is specified within the coordinate system of its superviewand is measured in points.
@property(nonatomic) CGPoint 中心讨论
中心在其父视图的坐标系内指定, 并以点为单位进行测量。
回答by Fran Sevillano
indicator.center = imageView.center;
indicator.center = imageView.center;