我想做什么
我想要的是应用程序顶部的 ImageView ,我可以控制它的外观。例如,在主页中我不需要横幅,但在另一个 View 中我需要。这意味着如果我确实想要横幅,我必须向下移动整个 View 。
我做了什么
以下内容位于我的应用程序委托(delegate)中。基本上,我将横幅的 customTabBarController View 向下移动 145 像素。
if ([self.customTabBarController interfaceOrientation] == UIInterfaceOrientationPortrait) {
[customTabBarController.view setFrame:CGRectMake(0, 145, 768,1024-145)];
banner = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 768, 145)];
}
else if ([self.customTabBarController interfaceOrientation] == UIInterfaceOrientationPortraitUpsideDown) {
[customTabBarController.view setFrame:CGRectMake(0,0, 768,1024-145)];
banner = [[UIImageView alloc] initWithFrame:CGRectMake(0, 768+145, 768, 145)];
}
banner.image = image;
banner.hidden = NO;
[self.window addSubview:banner];
问题是什么
现在,我已经完成了纵向部分,但是当我尝试更改方向时,图像也会随之更改方向;这意味着当我将 iPad 更改为横向和上下颠倒时,图像会侧向倾斜。基本上,它始终位于 iPad 的一侧。
有人可以帮忙吗?也欢迎提出其他方法的建议。
编辑:我意识到我没有实现横向的 setFrame,但我想首先完成颠倒纵向的实现,但代码不起作用。应用程序颠倒时的样子是横幅位于底部,图像颠倒。
最佳答案
请参阅“Orientation Problem while using insertSubview”。通常,您不应将 View 插入 UIWindow
本身。它不旋转。您应该将它们插入到 window.rootViewController.view
或下面,以便自动获得正确的转换。否则,您需要使用“wrong orientation on UIViewController while animating it”中给出的 effectiveTransform
等方法自行计算有效变换。但只要有可能,请使用 Root View 。它为您发挥所有魔力。
关于objective-c - UIImageView 和窗口方向问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6749101/