我有一個包含UIImageView的UIView。 UIImageViews就像應用程序的品牌徽標一樣工作。當我旋轉設備時,包含UIView調整自己以對應屏幕的風景或肖像比例。UIImageView和它的UIImage縮放比例沒有額外的填充
我想要實現的是讓UIImageView相應地縮放,並保持左邊距上的比例。
這是頂白色的「旗幟」的實際代碼:
UIView *topBanner = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, height_topBanner)];
[topBanner setAutoresizingMask:(UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin)];
[topBanner setBackgroundColor:[UIColor whiteColor]];
topBanner.autoresizesSubviews = YES;
// the logo
UIImage *topBanner_logo = [UIImage imageNamed:@"logo.png"];
float logoAspectRatio = topBanner_logo.size.width/topBanner_logo.size.height;
topBanner_logoView = [[UIImageView alloc] initWithFrame:CGRectMake(topBanner.frame.size.width/100*3, topBanner.frame.size.height/100*7, (topBanner.frame.size.height/100*86)*logoAspectRatio, topBanner.frame.size.height/100*86)];
[topBanner_logoView setImage:topBanner_logo];
topBanner_logoView.backgroundColor = [UIColor blueColor];
topBanner_logoView.contentMode = UIViewContentModeScaleAspectFit;
[topBanner_logoView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleRightMargin)];
[topBanner addSubview:topBanner_logoView];
[self.view addSubview:topBanner];
這是我的出發點:肖像的iPad上啓動:
這是當我發生了什麼在風景中旋轉它:
正如你所看到的,UIImage的比例是好的,但我得到額外的邊框(我設置UIImageView的背景顏色來突出顯示它),因爲UIImageView伸展自身以跟隨其容器大小的變化,並且UIImage適合UIImageView並且放在它的中心。
相同的 - 逆轉 - 當我在橫向模式下直接啓動應用情況:
然後轉動它:
...和我得到的標誌額外邊界在頂部和底部。
我確實看到我可以編寫一個函數來重新計算每次輪換更改的每個大小,但是我問自己是否有一種方法來設置UIImageView和UIImage使其工作,而不會黑客自動旋轉/調整iOS的程序。聽起來很簡單!
但我想要標誌按比例調整大小,不想要的行爲是UIImageView伸展自己,在兩側創建此「填充」。 – flip79