2014-02-17 62 views
1

在Xcode中5.0.2我創建a universal app拖着一個UIScrollViewUIImageView到iPhone和iPad故事板(這裏fullscreen):新增的UIScrollView,並在UIImageView的故事板 - 圖像被壓扁,並且不滾動

Xcode screenshot

然後我試圖加載由下面的代碼圖像board.png(版權維基百科共同性,Denelson83)在ViewController.m

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    UIImage *img = [UIImage imageNamed:@"board"]; 
    NSLog(@"%s: img=%@ (%f x %f)", __PRETTY_FUNCTION__, 
      img, img.size.width, img.size.height); 
    _imageView.image = img; 
    _scrollView.contentSize = img.size; 
} 

但是結果被壓扁,圖像不能滾動:

-[ViewController viewDidLoad]: img=<UIImage: 0x10912d770> (1000.000000 x 961.500000) 

app screenshot

有什麼建議嗎?我懷疑圖像在底部被切斷,因爲我運行的是3.5英寸的iPhone模擬器?如何解決這個問題呢?

UPDATE:

我想四0約束添加滾動視圖,因此,它需要整個屏幕,但是當我旋轉用CMD-箭模擬器 - 滾動視圖沒有按」噸調整大小(此處fullscreen of Xcode):

constraints screenshot

rotated app

更新2:

當我改變UIImageView「視圖模式」爲「左上」在Xcode故事板和源代碼如下:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    UIImage *img = [UIImage imageNamed:@"board"]; 
    _imageView.image = img; 
    _imageView.frame = CGRectMake(0, 0, img.size.width, img.size.height); 
    _scrollView.contentSize = img.size; 
} 

然後我得到下面的結果,不能滾動:

app screenshot

和不能旋轉或者(沒有填滿屏幕):

rotated app

+1

您是否嘗試過不同的內容模式,如「imageView.contentMode = UIViewContentModeScaleAspectFit;」?另外你的uiimageview界限是什麼? – Roecrew

+1

1.嘗試禁用滾動視圖的'auto resize subviews'屬性。 – Eyeball

+1

2.嘗試禁用自動佈局或進行更改之後或內部 - viewDidLayoutSubviews – Eyeball

回答

5

請記住,故事板圖像視圖已經從故事板設置了其大小,並且在將代碼加載到代碼中後,您從不調整任何大小。所以,這是從我的頭頂,但嘗試:

UIImage *image = [UIImage imageNamed:@"board"]; 
_imageView.image = image; 
_imageView.bounds = CGRectMake(0, 0, image.size.width, image.size.height); 


- (void)viewDidLayoutSubviews 
{ 
    _scrollView.contentSize = _imageView.bounds.size; 
} 
+0

不幸的是'_imageView.bounds = CGRectMake(0,0,img.size.width,img.size.height ); _scrollView.contentSize = img.size;'沒有區別。 –

+1

請再試一次。我更新了評論,以便稍後設置contentSize,這對我有用。 – eobet