简体   繁体   English

UIViewContentModeScaleAspectFit不起作用

[英]UIViewContentModeScaleAspectFit doesn't work

I try to scale my image and i want use Aspect Fit. 我尝试缩放图像,并想使用Aspect Fit。 But it doesn't work 但这不起作用

solutionImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_solution@2x.png"]];
    // optional:
    solutionImageView.contentMode = UIViewContentModeScaleAspectFit; // content mode
    solutionImageView.center = CGPointMake((currentWidth)/2, currentHeight - (currentHeight/3.5));
    [self.view addSubview:solutionImageView];

屏幕截图

As arunit21 said in his comment, when you create an UIImageView with the initWithImage method, the size of frame of the view is calculated with image size. 就像arunit21在他的评论中说的那样,当您使用initWithImage方法创建UIImageView时,视图框架的大小将与图像大小一起计算。 So your image view is centered but bigger than you window. 因此,图像视图居中,但比窗口大。 You can do 你可以做

solutionImageView = [[UIImageView alloc] initWithFrame:yourFrame];
solutionImageView.image = [UIImage imageNamed:@"bg_solution"];
solutionImageView.contentMode = UIViewContentModeScaleAspectFit; // content mode
solutionImageView.center = CGPointMake((currentWidth)/2, currentHeight - (currentHeight/3.5));
[self.view addSubview:solutionImageView];

You can also just set your new frame after your initialization to set the correct size. 您也可以在初始化后设置新帧以设置正确的尺寸。

The @2x.png is not needed in [UIImage imageNamed:@"bg_solution"] . @2x.png不需要在[UIImage imageNamed:@"bg_solution"] With this method, the system will check if the screen is retina and look for the @2x image if it exists and take the normal one otherwise. 使用此方法,系统将检查屏幕是否为视网膜,并查找@2x图像(如果存在),否则采用正常图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM