将 UIImage 安装到屏幕中央



我有一个视图控制器,其中有一个UIImageview被子视图。UII法师有不同的大小和方向。但我希望能够将它们放在屏幕中间。我试图为 UIImageview 定义一个框架,但我无法正确获得 CGRectMake 的 4 个参数。

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);

有一个属性"center":你可以做这样的事情:

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

将图像视图设置为您希望所有图像的大小。对我来说听起来像您希望它是全设备屏幕。

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

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

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

这应该会导致图像缩小或放大以适合图像视图的框架。

最新更新