Iphone viewDidLoad中报告的方向不正确
因此,我有一个应用程序,需要根据设备的方向加载不同的图像作为背景图像。我在viewDidLoad中有以下代码:Iphone viewDidLoad中报告的方向不正确,iphone,objective-c,ios,ipad,Iphone,Objective C,Ios,Ipad,因此,我有一个应用程序,需要根据设备的方向加载不同的图像作为背景图像。我在viewDidLoad中有以下代码: BOOL isLandScape = UIDeviceOrientationIsLandscape(self.interfaceOrientation); if (isLandScape) { self.bgImage.image = [UIImage imageNamed:@"login_bg748.png"]; } 出于某种原因,即使模拟器在横向启动,这
BOOL isLandScape = UIDeviceOrientationIsLandscape(self.interfaceOrientation);
if (isLandScape)
{
self.bgImage.image = [UIImage imageNamed:@"login_bg748.png"];
}
出于某种原因,即使模拟器在横向启动,这个布尔值仍然是错误的。我检查了一下,它总是报告处于纵向模式,不管模拟器的实际方向如何。有人知道为什么这不起作用吗
在界面方向的shouldAutoRotateForInterfaceOrientation中,我有以下内容:
if (UIDeviceOrientationIsLandscape(interfaceOrientation))
{
self.bgImage.image = [UIImage imageNamed:@"login_bg748.png"];
} else
{
self.bgImage.image = [UIImage imageNamed:@"login_bg1004.png"];
}
return YES;
这段代码确实有效,只是启动时出错了。执行一次旋转后,它工作正常。原因是
viewDidLoad
太早了。每个应用程序都以纵向模式启动,然后旋转到横向模式。调用viewDidLoad
时,旋转尚未发生。您希望使用延迟性能,或将测试置于didRotateFromInterfaceOrientation
或类似模式中。请参见我书中的解释:
首先在函数中
应该自动旋转到接口方向
您只需返回YES
现在使用这个函数
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
{
//landscape view login
}
else
{
//portrait View logic
}
}
如果您已经在横向视图或纵向视图中,则在viewDidLoad
功能中
-(void)viewDidLoad
{
if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
{
//landscape view code
}
else
{
//portrait view code
}
}
希望这会有所帮助我使用了didRotateFromInterfaceOrientation,效果很好。谢谢你,伙计!如果每个应用程序都以纵向模式启动,那么为什么要在viewDidLoad中这样检查呢?我怀疑每个应用程序都是以纵向模式启动的。这取决于需求。例如,在iPhone 6+、iPhone 7+上,您可以在横向模式下使用手机,因此如果用户在横向模式下启动应用程序,则
viewdiload
中的代码将负责向用户显示适当的视图,如果用户在使用应用程序时更改为纵向模式,我们有自己的didRotateFromInterfaceOrientation
来处理它