iOS:负载时的设备方向

| 看来,当我的应用加载时,它不知道其当前方向:
UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIDeviceOrientationPortrait) {
    NSLog(@\"portrait\");// only works after a rotation, not on loading app
}
旋转设备后,我将获得正确的方向,但是在加载应用程序时,如果不更改方向,似乎使用
[[UIDevice currentDevice] orientation]
不知道当前方向。 首次加载应用程序时,还有另一种方法可以检查此问题吗?     
已邀请:
编辑:我误读了你的问题。这将使您可以在某些方向上启动应用程序。刚意识到您正在尝试确定发布的方向。 有一种方法可以检查ѭ​​2ѭ上的状态栏方向:
[[UIApplication sharedApplication] statusBarOrientation];
原始答案 尝试在plist文件中设置应用程序接受的设备方向:
<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationPortrait</string>
    <string>UIInterfaceOrientationLandscapeLeft</string>
    <string>UIInterfaceOrientationLandscapeRight</string>
</array>
这将表明您的应用程序支持纵向(底部的主页按钮),左横向和右横向。 然后,在您的UIViewControllers中,您需要重写
shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)
方法,以在应用程序旋转时返回YES:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

     return interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight;
}
如果设备处于您支持的方向之一,这将告诉UIViewController自动旋转。如果您还想支持上下颠倒的方向(顶部带有“主页”按钮的人像),则将其添加到您的plist中,然后从此方法中返回YES即可。 让我们知道结果如何。     
我认为这会起作用:
 [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];UIInterfaceOrientation orientation = [UIDevice currentDevice].orientation;
根据UIDevice参考: 引用: \“除非通过调用beginGeneratingDeviceOrientationNotifications启用了定向通知,否则此属性的值始终返回0” 最初,我假定此属性始终包含当前方向,但显然并非如此。我猜想在其他通常访问direction属性的情况下,我们会在后台处理打开通知的操作,因此,这显然不需要在应用程序委托中手动完成     
尚未有人涉及的一件事是,您将
UIDeviceOrientation
类型存储在
UIInterfaceOrientation
变量中。它们是不同的,不应视为平等。请注意,
UIDeviceOrientationLeft
等于
UIInterfaceOrientationRight
(因为接口旋转的方向与设备相反)。     
您可以通过在内部插入以下通知来完成此操作
-(void)viewDidLoad

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkRotation:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
然后将以下方法放入您的类中
-(void)checkRotation:(NSNotification*)notification
{
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
    {
         //Do your textField animation here
    }
}
上面的方法将检查ipad或iPhone的状态栏的方向,并根据该方向以所需的方向制作动画。     
负载时,设备方向可以是
.Unknown
.FaceUp
。为了确定是纵向还是横向,我将the16用作后备,如下所示:
    var portraitOrientation = UIDevice.currentDevice().orientation == .Portrait

    if UIDevice.currentDevice().orientation == .Unknown || UIDevice.currentDevice().orientation == .FaceUp {
        portraitOrientation = UIApplication.sharedApplication().statusBarOrientation == .Portrait
    }
这样,我可以确保
portraitOrientation
总是告诉我该设备是否处于纵向模式,否则,它将处于横向模式。即使是第一次加载应用程序。     
为了从状态栏获取方向,在plist文件中启用所有方向也很重要。     
Swift 3基于@Marjin的代码。
var portraitOrientation = UIDevice.current.orientation == .portrait

if UIDevice.current.orientation == .unknown || UIDevice.current.orientation == .faceUp {
     portraitOrientation = UIApplication.shared.statusBarOrientation == .portrait
}

if(portraitOrientation)
{
     // Portrait
}
else
{

}
    
尝试使用加速度计获取其读数,UIAccelerometer,获取sharedAccelerometer,设置其委托,获取读数并从那里确定方位。     
尝试了全部,没有良好的结果。因此,就像在ipad上一样,我所做的就是将所有工作留给splitViewController方法来使barButton无效: 对于肖像:
- (void)splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController: (UIPopoverController *)pc { NSlog(@\"portrait\");}
对于风景:
- (void)splitViewController:(UISplitViewController *)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem{ NSlog(@\"landscape\");}
这总是在负载下工作。     
问题是ѭ22有时会错误地报告设备的方向。 而是使用
[[UIApplication sharedApplication]statusBarOrientation]
(即
UIInterfaceOrientation
),要进行检查,您需要使用
UIInterfaceOrientationIsLandscape(orientation)
希望这可以帮助。     
我仍然使用适用于iPhone 4的此工作代码段:
-(void)deviceOrientationDidChange:(NSNotification *)notification{

//Obtaining the current device orientation
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];    

int value = 0;

if(orientation == UIDeviceOrientationPortrait)
{
    value = 0;

}else if(orientation == UIDeviceOrientationLandscapeLeft)
{
    value = 90;

}else if(orientation == UIDeviceOrientationLandscapeRight)
{

    value = -90;

}

CGAffineTransform cgCTM = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(value));
[photoImageView setTransform:cgCTM];
}     
这是真正的答案。应用启动时,其方向未知。它使用shouldAutorotateToInterfaceOrientation和supportedInterfaceOrientations来决定要选择的方向。 观看当我在iPhone 5.0模拟器中启动示例应用程序并使用下面的代码和“支持的界面方向\”以及所有4种可能的方向旋转它时:
20:44:08.218 RotationTestApp Supported orientation: Portrait
20:44:08.222 RotationTestApp Supported orientation: Portrait (upside-down)
20:44:08.225 RotationTestApp Supported orientation: Landscape (home button on the right)
20:44:08.225 RotationTestApp Supported orientation: Landscape (home button on the left)
20:44:08.226 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (current device orientation: UIDeviceOrientationUnknown, interface orientation wants: UIInterfaceOrientationPortrait)
20:44:08.237 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (current device orientation: UIDeviceOrientationUnknown, interface orientation wants: UIInterfaceOrientationPortrait)
20:44:08.239 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (current device orientation: UIDeviceOrientationUnknown, interface orientation wants: UIInterfaceOrientationPortrait)
20:44:08.240 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (current device orientation: UIDeviceOrientationUnknown, interface orientation wants: UIInterfaceOrientationPortrait)
20:44:09.817 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationLandscapeLeft)
20:44:09.833 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationLandscapeLeft)
20:44:11.030 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationPortraitUpsideDown)
20:44:11.040 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationPortraitUpsideDown)
20:44:12.599 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationLandscapeRight)
20:44:12.609 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationLandscapeRight)
20:44:13.301 RotationTestApp shouldAutorotateToInterfaceOrientation: YES (device orientation: UIDeviceOrientationPortraitUpsideDown)
我见过很多代码片段,但是它们都无法正常工作(iPad和iPhone,iOS 5.0+)。 与其在try-this-try-that上摸索,不如将以下内容放在根vc中:
#define ToNSString_BEGIN(T) \\
NSString* T##ToNSString(T valueParameter) { \\
switch (valueParameter) {

#define ToNSString_VALUE(value) \\
case value: return @#value

#define ToNSString_END(T) \\
} \\
return @\"(unknown)\"; \\
}

// NSString* UIInterfaceOrientationToNSString(UIInterfaceOrientation);
ToNSString_BEGIN(UIInterfaceOrientation);
ToNSString_VALUE(UIInterfaceOrientationPortrait);           // 1
ToNSString_VALUE(UIInterfaceOrientationPortraitUpsideDown); // 2
ToNSString_VALUE(UIInterfaceOrientationLandscapeLeft);      // 3
ToNSString_VALUE(UIInterfaceOrientationLandscapeRight);     // 4
ToNSString_END  (UIInterfaceOrientation);

// NSString* UIDeviceOrientationToNSString(UIDeviceOrientation);
ToNSString_BEGIN(UIDeviceOrientation);
ToNSString_VALUE(UIDeviceOrientationUnknown);               // 0
ToNSString_VALUE(UIDeviceOrientationPortrait);              // 1
ToNSString_VALUE(UIDeviceOrientationPortraitUpsideDown);    // 2
ToNSString_VALUE(UIDeviceOrientationLandscapeLeft);         // 3
ToNSString_VALUE(UIDeviceOrientationLandscapeRight);        // 4
ToNSString_VALUE(UIDeviceOrientationFaceUp);                // 5
ToNSString_VALUE(UIDeviceOrientationFaceDown);              // 6
ToNSString_END  (UIDeviceOrientation);



// Change this custom method to alter auto-rotation behavior on all supported iOS versions and platforms.
- (BOOL)allowAutoRotate:(UIInterfaceOrientation)interfaceOrientation
{
    NSUInteger interfaceOrientationAsMask = (1<<interfaceOrientation);
    return interfaceOrientationAsMask & [self supportedInterfaceOrientations];
}

// Reads from the project\'s-Info.plist
- (NSUInteger)supportedInterfaceOrientations
{
    static NSUInteger orientationsResult;

    if (!orientationsResult) {
        NSArray *supportedOrientations = [[NSBundle mainBundle] objectForInfoDictionaryKey:@\"UISupportedInterfaceOrientations\"];

        for (id orientationString in supportedOrientations) {
            if ([orientationString isEqualToString:@\"UIInterfaceOrientationPortrait\"]) {
                orientationsResult |= UIInterfaceOrientationMaskPortrait;
                NSLog(@\"Supported orientation: Portrait\");
            } else if ([orientationString isEqualToString:@\"UIInterfaceOrientationPortraitUpsideDown\"]) {
                orientationsResult |= UIInterfaceOrientationMaskPortraitUpsideDown;
                NSLog(@\"Supported orientation: Portrait (upside-down)\");
            } else if ([orientationString isEqualToString:@\"UIInterfaceOrientationLandscapeRight\"]) {
                orientationsResult |= UIInterfaceOrientationMaskLandscapeRight;
                NSLog(@\"Supported orientation: Landscape (home button on the left)\");
            } else if ([orientationString isEqualToString:@\"UIInterfaceOrientationLandscapeLeft\"]) {
                orientationsResult |= UIInterfaceOrientationMaskLandscapeLeft;
                NSLog(@\"Supported orientation: Landscape (home button on the right)\");
            } else {
                NSLog(@\"Unrecognized orientation \'%@\' in mainBundle plist, key UISupportedInterfaceOrientations\", orientationString);
            }
        }
    }
   return orientationsResult;
}

// iOS 6+ (not yet used in 6.0.1)
- (BOOL)shouldAutorotate
{
    UIDeviceOrientation interfaceOrientationFromDevice = [UIDevice currentDevice].orientation;
    BOOL result = [self allowAutoRotate:interfaceOrientationFromDevice];
    NSString *currentDeviceOrientation = UIDeviceOrientationToNSString(interfaceOrientationFromDevice);
    NSLog(@\"shouldAutorotate: %s (current orientation %@)\", result ? \"YES\" : \"NO\", currentDeviceOrientation);
    return result;
}

// iOS 2.0 - 5.1 (iOS 6+ deprecated, 6.0.1 still works)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    NSString* orientationString;
    UIDeviceOrientation interfaceOrientationFromDevice = [UIDevice currentDevice].orientation;

    if ((int)interfaceOrientation != (int)interfaceOrientationFromDevice) {
        orientationString = [NSString stringWithFormat:@\"current device orientation: %@, interface orientation wants: %@\",
                             UIDeviceOrientationToNSString(interfaceOrientationFromDevice),
                             UIInterfaceOrientationToNSString(interfaceOrientation)
                             ];
    } else {
        orientationString = [NSString stringWithFormat:@\"device orientation: %@\", UIDeviceOrientationToNSString(interfaceOrientationFromDevice)
                             ];
    }

    BOOL result = [self allowAutoRotate:interfaceOrientation];
    NSLog(@\"shouldAutorotateToInterfaceOrientation: %s (%@)\",
          result ? \"YES\" : \"NO\",
          orientationString);
    return result;
}
不使用当前方向的segue动画仍然存在棘手的问题。我的猜测是,对每个VC进行子类化,然后对pop上的push / notify委托进行一些定位将是一种方法。 同样重要: 应该AutorotateToInterfaceOrientation不起作用 横向模式下的tabBarController和navigationControllers,第二集     
试试这个。它为我工作。当时的didfinishedlaunch方法很陈旧,无法检测到设备方向。默认情况下将其作为人像。所以。我用来检查统计栏的方向。我测试此代码。将其放在appdeleget中的didfinishedlaunch方法中。 UIInterfaceOrientation方向= [UIApplication sharedApplication] .statusBarOrientation;
if(orientation == 0) {//Default orientation
    //UI is in Default (Portrait) -- this is really a just a failsafe.

    NSLog(\"for portrait\");


}else if(orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{

    NSLog(\"portrait\");
}else if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{

    NSLog(\"Landscap\");
}
    

bab

试试这个
[[UIApplication sharedApplication] statusBarOrientation];
或在应用程序委托中实现这一点
(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
}
有用     
对于那些希望为Swift 3或4寻求答案的人,只需在viewDidLoad()块内添加该代码即可。
`let orientation = UIApplication.shared.statusBarOrientation
 if orientation == .portrait {
        // portrait   
 } else if orientation == .landscapeRight || orientation == 
.landscapeLeft{
         // landscape     
 }`
    
上面的每个人都发布了非常有效的答案:但作为更新:苹果公司的看法:您应该使用UIStatusBar方向读取设备的当前方向: 检查设备当前方向的一种方法是在
viewDidLoad
方法内部使用int值:
    int orientationType = [[UIDevice currentDevice] orientation];
在哪里考虑以下内容。 。 。  -1 =纵向(正确向上)  -2 =纵向倒置  -3 =景观(右)  -4 =风景(左) 然后可以在检测到方向后使用
IF
语句来调用方法,依此类推: 希望这对某人有所帮助     

要回复问题请先登录注册