[英]UIImageView does not follow device orientation
我有一個帶有幾個子視圖的scrollView。 子視圖的定義如下:
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGRect rect = imageView.frame;
rect.size.height = screenRect.size.height;
rect.size.width = screenRect.size.width;
imageView.frame = screenRect;
[scrollView addSubview:imageView];
這工作正常,直到我將界面方向更改為橫向。 imageView仍處於縱向模式,將圖像切成兩半。 我該怎么做才能將肖像高度和寬度更改為橫向高度和寬度?
在代碼中添加此行並檢查:
[imageView setAutoresizesSubviews:YES];
[imageView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[imageView setAutoresizesSubviews:YES];
[imageView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
也檢查此鏈接 。
幾點想法:
而不是[[UIScreen mainScreen] bounds]
,我用self.view.bounds
。 這(a)將分解狀態欄,導航欄等內容; (b)在旋轉時,寬度和高度會自動更新以反映新尺寸。
如果您希望為您調整imageview的大小,通常將其autoresizingMask
設置為類似UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleWidth
UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleWidth
。
您可能需要檢查imageview的contentMode
並將其設置為類似UIViewContentModeScaleAspectFit
以便始終顯示整個圖像。 這樣,如果你有一個肖像圖像,它將始終以縱向顯示而不會失真,而不會被切斷。
如果您不喜歡旋轉設備時圖像視圖本身已調整為橫向的事實,則可以在iOS5的viewWillLayoutSubviews
中重置其框架(在iOS4中,使用willAnimateRotationToInterfaceOrientation
)。
您需要偵聽UIDeviceOrientationDidChangeNotification
在這里閱讀更多相關信息: http : //developer.apple.com/library/ios/#documentation/uikit/reference/UIDevice_Class/Reference/UIDevice.html
檢測到更改后,再次調用相同的代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.