我有一個奇怪的錯誤發生。我試圖用一個特定的框架和圖像以編程方式初始化UIImageView。我的圖像是60x60,但我試圖將其調整到屏幕上的30x30,這似乎是不可能的。如何設置特定尺寸爲UIImageView
下工作,但顯示爲60x60圖像:
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo.png"]];
[imgView setContentMode:UIViewContentModeScaleAspectFit];
// imgView.frame = CGRectMake(100, 200, 30, 30);
imgView.center = CGPointMake(pointTouch.x, pointTouch.y);
[self.view addSubview:imgView];
而下面只是沒有在屏幕上顯示的所有東西:
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo.png"]];
[imgView setContentMode:UIViewContentModeScaleAspectFit];
imgView.frame = CGRectMake(100, 200, 30, 30);
imgView.center = CGPointMake(pointTouch.x, pointTouch.y);
[self.view addSubview:imgView];
有什麼不對?
編輯:對於那些有同樣的問題,這裏是工作代碼:
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo.png"]];
imgView.frame = CGRectMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2, 30, 30);
imgView.center = CGPointMake(pointTouch.x, pointTouch.y);
[imgView setContentMode:UIViewContentModeScaleAspectFill];
[self.view addSubview:imgView];
如果您不更改'imgView.center',它顯示嗎? – gWiz
您是否使用自動佈局? –
@gWiz:不,不工作 – anthoprotic