如何水平对齐我的图像在中心 IOS 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9484342/
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 align my image in the center IOS horizontally
提问by London
Here is my image :
这是我的形象:
Obj *obj = ... obj has imageHref which is NSString
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:obj.imageHref]];
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
Depending on image size I can't figure out how to center it, any ideas ?
根据图像大小,我不知道如何将其居中,有什么想法吗?
This is all inside method :
这是所有内部方法:
- (void)drawRect:(CGRect)rect {
Obj *obj = ... obj has imageHref which is NSString
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:obj.imageHref]];
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
NSLog(@"%f", rect.size.width);
NSLog(@"%f", rect.size.height);
}
Where rect.size.width
prints 320.000000 and rect.size.height
prints 416.000000
其中rect.size.width
打印 320.000000 并rect.size.height
打印 416.000000
回答by Melvin
[image drawInRect:CGRectMake((self.view.frame.size.width/2) - (image.size.width/2),
(self.view.frame.size.height / 2) - (image.size.height / 2),
image.size.width,
image.size.height)];
edit:
编辑:
As you are already in a UIView:
由于您已经在 UIView 中:
[image drawInRect:CGRectMake((self.frame.size.width/2) - (image.size.width/2), (self.frame.size.height / 2) - (image.size.height / 2), image.size.width, image.size.height)];
回答by Damo
This or something very like this should centre horizontally.
这个或类似的东西应该水平居中。
CGPoint centerImageView = imageView.center;
centerImageView.x = self.view.center.x;
ImageView.center = centerImageView;
回答by ETech
It is possible to add a macro like this before @interface
in .h class file#define COUNTMID(X,Y) (float) (X-Y)/2
and then use it in code asfloat center COUNTMID(ParentView.frame.width,ChildView.frame.width)
to place your ChildView view at horizontal center of ParentView
I've also made a method, making a ChildView frame to be placed in the center of ParentView:
可以@interface
在 .h 类文件中添加这样的宏#define COUNTMID(X,Y) (float) (X-Y)/2
,然后在代码中使用它,float center COUNTMID(ParentView.frame.width,ChildView.frame.width)
以便将 ChildView 视图放置在 ParentView 的水平中心
我还制作了一个方法,将 ChildView 框架放置在中心父视图:
-(CGRect) CenterFrameOfView:(UIView *) ParentView forView:(UIView *) ChildView
{
CGRect result = CGRectMake(COUNTMID(ParentView.frame.size.width,ChildView.frame.size.width),COUNTMID(ParentView.frame.size.height,ChildView.frame.height),ChildView.frame.size.width,ChildView.frame.size.height);
return result;
}
Or with setFrame
或者使用 setFrame
-(void) CenterFrameOfView:(UIView *) ParentView forView:(UIView *) ChildView
{
CGRect result = CGRectMake(COUNTMID(ParentView.frame.size.width,ChildView.frame.size.width),COUNTMID(ParentView.frame.size.height,ChildView.frame.height),ChildView.frame.size.width,ChildView.frame.size.height);
[ChildView setFrame:result];
}
Hope it helps
希望能帮助到你