将UIImage安装到屏幕中心

时间:2013-09-13 12:07:40

标签: ios uiimageview

我有一个viewcontroller,其中UIImageview是子视图。 UIImages有不同的尺寸和方向。但我想能够将它们放在屏幕中间。 我试图为UIImageview定义一个框架,但是我无法正确获得CGRectMake的4个参数。

3 个答案:

答案 0 :(得分:5)

UIImage *backgroundLogoImage = [UIImage imageNamed:@"center_logo.png"];
UIImageView *backgroundLogoImageView = [[UIImageView alloc] init];
backgroundLogoImageView.frame = CGRectMake((self.view.frame.size.width / 2) - (backgroundLogoImage.size.width / 2), (self.view.frame.size.height / 2) - (backgroundLogoImage.size.height / 2), backgroundLogoImage.size.width, backgroundLogoImage.size.height);

答案 1 :(得分:1)

有一个属性“中心”:你可以这样做:

UIImageView * img = [[UIImageView alloc] init];
img.center = self.view.center;

答案 2 :(得分:1)

将图像视图设置为您想要所有图像的大小。听起来像你想让它成为完整的设备屏幕。

  imageView.frame = self.view.frame; // or what ever;
  // if its a custom size,set size and then call imageView.center = self.view.center;

将图像添加到imageView,但设置内容模式以允许缩放。

 UIImage *image = [UIImage imageNamed:@"something.png"];
 image.contentMode = UIViewContentModeScaleAspectFit;
 image.clipsToBounds = YES;
 imageView.image = image;

这会导致图像缩小或缩小以适合imageView的帧。