2013-11-21 95 views
0

我有一個奇怪的錯誤發生。我試圖用一個特定的框架和圖像以編程方式初始化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]; 
+0

如果您不更改'imgView.center',它顯示嗎? – gWiz

+0

您是否使用自動佈局? –

+0

@gWiz:不,不工作 – anthoprotic

回答

1

開始實驗

imgView.center = CGPointMake(view.bounds.x/2, view.bounds.y/2); 
imgView.bounds = view.bounds; 

看看是否填滿了超級視圖。然後分別嘗試CGPointMake(pointTouch.x, pointTouch.y)CGPointMake(30,30)

我猜這種行爲有事情做與warningframe

如果變換屬性不是恆等變換,這個屬性的值是不確定的,因此應被忽略。

+0

感謝您的幫助!有效! – anthoprotic

+0

我編輯了完整答案的帖子 – anthoprotic

0

你可以嘗試用一組框架初始化UIImageView,那麼之後設置圖像:

UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 200, 30, 30)]; 
[imgView setContentMode:UIViewContentModeScaleAspectFit]; 
imgView.image = [UIImage imageNamed:@"logo.png"]; 
imgView.center = CGPointMake(pointTouch.x, pointTouch.y); 
[self.view addSubview:imgView]; 
+0

不工作的殘疾人。即使我刪除了行imgView.center = CGPointMake(pointTouch.x,pointTouch.y); ,屏幕上沒有任何東西被添加 – anthoprotic