简体   繁体   中英

UIViewContentModeScaleAspectFit doesn't work

I try to scale my image and i want use 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. 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"] . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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