![](/img/trans.png)
[英]Objective-c using UIInterfaceOrientationMask to control orientation
[英]Objective-c device orientation
我正在构建模板应用程序。 我在设备旋转/方向方面遇到一些问题。 我的appDelegate didFinishLaunching中包含以下内容:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
并在我的VC viewDidLoad中:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
...
//add the title bar
titleBar = [[OAI_TitleBar alloc] initWithFrame:CGRectMake(0.0, 0.0, 1024.0, 50.0)];
titleBar.titleBarTitle = @"OAI Template";
titleBar.hasAccount = YES;
titleBar.hasReset = YES;
titleBar.hasHome = YES;
[titleBar buildTitleBar];
[self.view addSubview:titleBar];
这是我的deviceOrientationDidChange方法:
//rotate the vc view
if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)) {
[self.view setFrame:CGRectMake(0.0, 0.0, 1024.0, 768.0)];
} else if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation)) {
[self.view setFrame:CGRectMake(0.0, 0.0, 768.0, 1024.0)];
}
//call the rotation management methods of various classes
[titleBar adjustForRotation:orientation];
}
当我旋转设备时,效果很好,视图将按预期调整。 但是,当应用启动时,x,y坐标是错误的-当我以纵向启动时记录框架时,VC视图的框架为{{0,20},{768,1004}},以横向模式启动时是{{20,0},{748,1024}}
标题栏框架是{{20,0},{748,1024}}用于风景和肖像(这是我编码的)。
但是,我在模拟器和设备上看到的内容完全不同。 您应该看到的是,顶部有一个黑色的栏(高度约50像素),左侧带有徽标,然后是一些按钮,然后该应用程序标题右对齐。 从下面的图片中可以看到,当应用程序以任一方向启动时,其打开的x / y坐标都不接近0/20。 任何帮助,将不胜感激。
在我看来,当应用程序以横向启动时,它将以纵向显示;当应用程序以纵向启动时,尽管显示正确,但其偏移约为20.0f。 任何帮助,将不胜感激。
解决了,我
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight;
}
在我的viewController中,确保正确的方向在我的plist中并且加载正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.