我已经创建了一个支持所有方向的应用程序。
我还在 viewDidLoad
方法中创建了一个表格和搜索栏(不使用 xib)。我还编写了以下代码来调整表格 View 和搜索栏的位置。
当我以纵向 View 启动我的应用程序时它工作正常,当我旋转到横向 View 时,它完美地调整了 View 。
但是当我以横向模式启动我的应用程序时, View 没有调整,而是采用纵向 View 的定位。但是,当我以纵向旋转然后横向旋转时,一切正常。
同时为了确保捕获正确的方向,我在该方法中编写了 NSLog
,它向我显示了正确的值。
同时为了确保显式调用此方法 (adjustViewsForOrientation
),我还在 viewDidLoad
中调用了该方法。
[self adjustViewsForOrientation:self.interfaceOrientation];
下面是剩余的代码片段:
- (void) adjustViewsForOrientation:(UIInterfaceOrientation)orientation {
if (orientation == UIInterfaceOrientationLandscapeLeft ||
orientation == UIInterfaceOrientationLandscapeRight) {
aTableView.frame = CGRectMake(705, 220, 320, 280);
sBar.frame=CGRectMake(805, 0, 220, 40);
NSLog(@"inside landscape");
}
else if (orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown) {
aTableView.frame = CGRectMake(450, 220, 320, 280);
sBar.frame=CGRectMake(550,3,220,40);
}
}
- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration {
// [self adjustViewsForOrientation]
[self adjustViewsForOrientation:toInterfaceOrientation];
NSLog(@"landscap");
}
最佳答案
试试这个方法,它可能会有所帮助。在 viewDidLoad 方法中注册 UIDeviceOrientationDidChange:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(adjustViewsForOrientation:) name:UIDeviceOrientationDidChangeNotification object:nil];
同时更改 -(void) adjustViewsForOrientation:(UIInterfaceOrientation)orientation
到 -(void) adjustViewsForOrientation:(NSNotification *)notification
并通过获取方向值得到新的方向
例如UIDeviceOrientation newDeviceOrientation = [通知方向];
关于iphone - 以横向模式启动应用程序时 UIOrientation 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8805399/