【问题标题】:Need Help Checking What Orientation iPad is in [duplicate]需要帮助检查 iPad 的方向 [重复]
【发布时间】:2011-10-30 06:38:52
【问题描述】:

可能重复:
iPhone orientation

根据 iPad 所处的方向,我需要设置表格视图的背景或将其移除,因为它不适用于弹出框。

我试过了,但没有运气:

if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft ){
    self.navigationController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]]; 
    self.tableView.backgroundColor = [UIColor clearColor];
    NSLog(@"test");

也许有更好的方法?看看table是master还是popover,然后设置背景?

【问题讨论】:

    标签: objective-c cocoa-touch ipad uitableview uiviewcontroller


    【解决方案1】:

    有几种方法可以或多或少地实现您想要的:

    1. UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
    2. 一种更骇人听闻的方法是检查状态栏的宽度。

    如果您选择选项 1,那么您可以像这样检查方向:

    switch (orientation) {
        case UIInterfaceOrientationPortrait:
            // Do something.
            break;
        case UIInterfaceOrientationPortraitUpsideDown:
            // Do something.
            break;
        case UIInterfaceOrientationLandscapeLeft:
            // Do something.
            break;
        case UIInterfaceOrientationLandscapeRight:
            // Do something.
            break;
    }
    

    编辑:要让您的应用程序自动旋转,您可以这样做:

    - (BOOL)willAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        if ((interfaceOrientation == UIDeviceOrientationLandscapeRight)) NSLog(@"Right");
        if ((interfaceOrientation == UIDeviceOrientationLandscapeLeft)) NSLog(@"Left");
        if ((interfaceOrientation == UIDeviceOrientationPortrait)) NSLog(@"Up");
        if ((interfaceOrientation == UIDeviceOrientationPortraitUpsideDown)) NSLog(@"Down");
        return YES;
    }
    

    【讨论】:

    • 好吧,如果我选择选项一。如何从该代码中检索方向并根据方向使用 if/else 语句?
    【解决方案2】:
    UIInterfaceOrientation orientation = [UIDevice currentDevice].orientation;
    

    【讨论】:

    • 这对我没有任何作用。
    • 它没有得到方向?
    • 不知道怎么用。我只是在 NSLog 方向上添加了这一行,但什么也没得到。每次方向改变时,我还需要它调用指定的方法。
    • 另一个答案包含一个 switch 语句,应该会给你一个想法。
    猜你喜欢
    • 2011-05-24
    • 1970-01-01
    • 2021-09-10
    • 2010-12-17
    • 1970-01-01
    • 2020-12-12
    • 2016-07-24
    • 1970-01-01
    相关资源
    最近更新 更多