iphone - 以横向模式启动应用程序时 UIOrientation 不起作用

标签 iphone ios ipad uitableview uiinterfaceorientation

我已经创建了一个支持所有方向的应用程序。

我还在 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/

相关文章:

iphone - NSManagedObject 如何重新加载

iPhone 导航栏字体

iphone - iOS 7 警告中不推荐使用 transactionreceipt 的问题

ios - iOS-数据库SQLite类/插件/库

ios - 使用 Swift 4 及更高版本对十六进制数进行排序

ios - 如何在 UIScrollView 中使用 UIGestureRecognizer

ios - cellForItemAtIndexPath : 中不是唯一的单元格

iphone - iOS 上的用户体验流程

ios - 如果在 iOS 中禁用自动播放,Apple 的网站如何自动播放视频?

objective-c - UIKit.h 找不到错误