[英]Why doesn't UIViewContentModeScaleAspectFit work in a UIImageView inside UIScrollView? See Image attached
[英]UIViewContentModeScaleAspectFit doesn't work
我嘗試縮放圖像,並想使用Aspect Fit。 但這不起作用
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];
就像arunit21在他的評論中說的那樣,當您使用initWithImage
方法創建UIImageView時,視圖框架的大小將與圖像大小一起計算。 因此,圖像視圖居中,但比窗口大。 你可以做
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];
您也可以在初始化后設置新幀以設置正確的尺寸。
在@2x.png
不需要在[UIImage imageNamed:@"bg_solution"]
。 使用此方法,系統將檢查屏幕是否為視網膜,並查找@2x
圖像(如果存在),否則采用正常圖像。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.