【发布时间】:2015-11-08 18:19:22
【问题描述】:
可能有十几个标题相似的 SO 问题,据我所知,我已经采纳了他们每个人的建议,但没有运气。当我将设备从纵向旋转到横向时,我想更改背景图像。但是,在我尝试过的每个实验中,横向的 UIImageView 都保持纵向大小。
我正在使用自动布局,在 IB 中配置如下:
我的视图层次结构是这样配置的:
当我旋转设备时,我想旋转 UIView viewBackground 中的所有内容(图像和所有按钮)。我相信我可以通过约束来管理按钮的移动,但需要自己旋转 UIImageView image view background。所以,我添加了以下代码:
-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
int scaleFactor = self.traitCollection.displayScale;
NSString *source = @"drawn";
NSString *orientation;
if (size.width > size.height)
{
orientation = @"Landscape";
}
else
{
orientation = @"Portrait";
}
NSString *platform;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
{
platform = @"iPad";
}
else
{
platform = @"iPhone";
}
NSString *filename = [NSString stringWithFormat:@"%@ %@ %@ %.0fx%.0f.png", source, platform, orientation, size.width * scaleFactor, size.height * scaleFactor];
// CONFIRMED: we have assembled the correct new image name.
viewBackground.frame = CGRectMake(0, 0, size.width * scaleFactor, size.height * scaleFactor);
viewBackground.autoresizesSubviews = YES;
viewBackground.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
imageViewBackground.autoresizesSubviews = YES;
imageViewBackground.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
imageViewBackground.image = [UIImage imageNamed:filename];
}
问题:正确的图像出现在屏幕上,但如上所述,它仍然具有纵向尺寸。我确信图像会随着艺术品的显着不同而发生变化,元素水平堆叠与垂直堆叠。但是新图片在横向模式下被水平切断。
我尝试将各种狂野的值放入newRect,但它们都没有任何区别。即使我从根本上改变了newRect 中的原点,图像也不会真正移动。所以我目前的理论是我对视图大小的更改被忽略/覆盖。
问题:我错过了什么阻止图像大小调整为横向模式?还有其他一些我没有解决的自动调整大小方面吗?
【问题讨论】:
标签: ios objective-c uiviewcontroller autolayout screen-orientation