我有一个包含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