开发者

Need Help Checking What Orientation iPad is in [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

iPhone orientation

Depending on what orientation the iPad is in, I need to either set the table view's background or remove it because it doesn't work with the popover.

I tried this b开发者_Python百科ut no luck:

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

Maybe there is a better way? To see if table is master or popover, then set background?


There are several ways to achieve what you want, more or less:

  1. UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
  2. A more hack-ish method would be to check the width of the status bar.

If you go with option 1, then you can check the orientation like so:

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

EDIT: To get your application to automatically rotate, you can do this:

- (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;
}


UIInterfaceOrientation orientation = [UIDevice currentDevice].orientation;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜