将 CGRect 在视图中居中

发布于 2024-09-18 14:34:59 字数 374 浏览 6 评论 0原文

我有一个图像显示在 CGRect 中。如何使矩形在视图中居中?

这是我的代码:

UIImage * image = [UIImage imageNamed:place.image];
CGRect rect = CGRectMake(10.0f, 90.0f, image.size.width, image.size.height);

UIImageView * imageView = [[UIImageView alloc] initWithFrame:rect];
[imageView setImage:image];

我尝试过 imageView.center 但没有效果。

提前致谢。

I have an image displaying in a CGRect. how do I center the rect in the view?

here's my code:

UIImage * image = [UIImage imageNamed:place.image];
CGRect rect = CGRectMake(10.0f, 90.0f, image.size.width, image.size.height);

UIImageView * imageView = [[UIImageView alloc] initWithFrame:rect];
[imageView setImage:image];

I've tried imageView.center but to no effect.

thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

终难愈 2024-09-25 14:34:59

查看:

https://developer.apple.com/documentation/coregraphics/cggeometry

你可以这样做:

CGPoint center = CGPointMake(CGRectGetMidX(mainRect), CGRectGetMidY(mainRect));

Looking at:

https://developer.apple.com/documentation/coregraphics/cggeometry

You can do:

CGPoint center = CGPointMake(CGRectGetMidX(mainRect), CGRectGetMidY(mainRect));
裸钻 2024-09-25 14:34:59

UIView 的 center 是一个属性,而不是一个方法。

您需要计算要放置它的实际位置。您可以通过设置视图的框架或设置其中心来完成此操作,这在您的情况下更简单。

您没有说明然后将 imageView 设为哪个视图的子视图。如果您将其放入名为 superview 的内容中,您可以这样做:

CGPoint superCenter = CGPointMake(CGRectGetMidX([superview bounds]), CGRectGetMidY([superview bounds]));
[imageView setCenter:superCenter];

UIView's center is a property, not a method.

You need to calculate the actual location you want to put it in. You can do this by setting the frame of the view, or by setting its center, which is simpler in your case.

You don't say what view you then go make imageView a subview of. If you're putting it inside something called superview, you could do this:

CGPoint superCenter = CGPointMake(CGRectGetMidX([superview bounds]), CGRectGetMidY([superview bounds]));
[imageView setCenter:superCenter];
我爱人 2024-09-25 14:34:59

根据父视图矩形的大小和原点将 imageView 放置在父视图中。

给定一个名为parentView的父视图:

float parentRect = parentView.frame;
float imageRect = imageView.frame;

imageRect.origin.x = (int)(parentView.origin.x + (parentRect.size.width - imageRect.size.width) / 2);
imageRect.origin.y = (int)(parentView.origin.y + (parentRect.size.height- imageRect.size.height) / 2);
imageView.frame = imageRect;

将原点转换为int可确保居中图像不模糊(尽管它可能会偏离中心一个子像素量)。

Position the imageView in the parentView based on the size and origin of the parentView's rect.

Given a parent view named parentView:

float parentRect = parentView.frame;
float imageRect = imageView.frame;

imageRect.origin.x = (int)(parentView.origin.x + (parentRect.size.width - imageRect.size.width) / 2);
imageRect.origin.y = (int)(parentView.origin.y + (parentRect.size.height- imageRect.size.height) / 2);
imageView.frame = imageRect;

Casting the origins to int ensures that your centered image is not blurry (though it may be off center by a subpixel amount).

回忆凄美了谁 2024-09-25 14:34:59
CGRect r = thingToCenter.frame;
r.origin = parentView.bounds.origin;
r.origin.x = parentView.bounds.size.width / 2  - r.size.width / 2;
r.origin.y = parentView.bounds.size.height / 2  - r.size.height / 2 + 12;
thingToCenter.frame = r;
CGRect r = thingToCenter.frame;
r.origin = parentView.bounds.origin;
r.origin.x = parentView.bounds.size.width / 2  - r.size.width / 2;
r.origin.y = parentView.bounds.size.height / 2  - r.size.height / 2 + 12;
thingToCenter.frame = r;
无所谓啦 2024-09-25 14:34:59
CGRect CGRectIntegralCenteredInRect(CGRect innerRect, CGRect outerRect) {
    CGFloat originX = outerRect.origin.x + ((outerRect.size.width - innerRect.size.width) * 0.5f);
    CGFloat originY = outerRect.origin.y + ((outerRect.size.height - innerRect.size.height) * 0.5f);
    return CGRectIntegral(CGRectMake(originX, originY, innerRect.size.width, innerRect.size.height));
}
CGRect CGRectIntegralCenteredInRect(CGRect innerRect, CGRect outerRect) {
    CGFloat originX = outerRect.origin.x + ((outerRect.size.width - innerRect.size.width) * 0.5f);
    CGFloat originY = outerRect.origin.y + ((outerRect.size.height - innerRect.size.height) * 0.5f);
    return CGRectIntegral(CGRectMake(originX, originY, innerRect.size.width, innerRect.size.height));
}
我们的影子 2024-09-25 14:34:59

对于斯威夫特 4:

CGPoint center = CGPoint(x: buttonRect.midX, y: buttonRect.midY)

For Swift 4:

CGPoint center = CGPoint(x: buttonRect.midX, y: buttonRect.midY)
空城之時有危險 2024-09-25 14:34:59

如果经常调用该函数,为了简单起见,您还可以使用此处所示的宏。

#define Center(x) CGPointMake(CGRectGetMidX(x), CGRectGetMidY(x));

然后像这样使用它

CGPoint myPoint = Center(myRect);

If the function is called frequently, you can also use macros as shown here for simplicity.

#define Center(x) CGPointMake(CGRectGetMidX(x), CGRectGetMidY(x));

Then use it like

CGPoint myPoint = Center(myRect);
爱殇璃 2024-09-25 14:34:59

雨燕4

let center = CGPoint(x: bounds.midX, y: bounds.midY)

Swift 4

let center = CGPoint(x: bounds.midX, y: bounds.midY)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文