UIImageView 和窗口方向问题
我想要做什么
我想要的是应用程序顶部的图像视图,我可以控制它的外观。例如,在主页中我不需要横幅,但在另一个视图中我需要。这意味着如果我确实想要横幅,我必须向下移动整个视图。
我所做的
以下内容位于我的应用程序委托中。基本上,我将横幅的 customTabBarController 视图向下移动 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,但我想首先完成颠倒纵向的实现,但代码不起作用。应用程序颠倒时的外观是横幅位于底部且图像颠倒。
WHAT I WANT TO DO
What I want is an imageview on top of the application, which I can control it's appearance. For example, in the homepage I don't want the banner, but in another view I do. That means that if I do want to have the banner, I have to shift down the whole view.
WHAT I'VE DONE
The following is within my App Delegate. Basically, I am shifting the view of the customTabBarController down 145 px for the banner.
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];
WHAT THE PROBLEM IS
Now, I've done the portrait orientation part, but when I try to change orientations, the image changes orientation along too; meaning the image turns sideways when I change the iPad to landscape and upside down. Basically, it stays at one side of the iPad at all times.
Can anyone please help? Suggestions on other methods are welcome too.
EDIT: I realize I didn't implement the setFrame for landscape orientation, but I wanted to finish implementing it for upside down portrait orientation first, but the code does not work. What the application looks like when it's upside down is the banner is on the bottom and the image is upside down.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请参阅“使用 insertSubview 时的方向问题”。通常,您不应将视图插入
UIWindow
本身。它不旋转。您应该将它们插入到window.rootViewController.view
或下面,以便自动获得正确的转换。否则,您将需要使用“UIViewController 动画时方向错误。"但只要有可能,请使用根视图。它为您发挥所有魔力。See "Orientation Problem while using insertSubview." You should generally not insert views into the
UIWindow
itself. It does not rotate. You should insert them into thewindow.rootViewController.view
or below so that you will get the right transform automatically. Otherwise, you will need to calculate the effective transform yourself using a method likeeffectiveTransform
given in "wrong orientation on UIViewController while animating it." Whenever possible, though, use the root view. It does all the magic for you.您是否考虑过将所有这些放入支持多个方向的 UIViewController 中?您可以使用自动调整大小蒙版来在不同方向上获得正确的大小。
Have you thought about putting all that into a UIViewController which supports multiple orientations? You might use an autoresizing mask to achieve the correct size in the different orientations.
您可以注册方向更改通知并将 CGTransform 应用于您的横幅 UIView
You can register for orientation change notifications and apply a CGTransform to your banner UIView