如何找到屏幕的视图控制器

时间:2016-06-10 20:54:44

标签: ios viewcontroller

我看过以下代码,允许某人获取设备的屏幕方向:

    if(UIInterfaceOrientationIsPortrait(viewController.interfaceOrientation)) {
    //do portrait work
} else if(UIInterfaceOrientationIsLandscape(viewController.interfaceOrientation)){
    //do landscape work
}

问题是我不知道用什么替代“viewController”占位符代码。值得注意的是,我正在制作键盘应用程序,因此我不确定是否可以使用可能适用于其他情况的相同视图控制器。我可以用什么控制器来判断屏幕的方向?

1 个答案:

答案 0 :(得分:0)

你可以通过两种方式完成。

UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
    //portrait    
}

if (orientation == UIInterfaceOrientationLandscapeRight || orientation == UIInterfaceOrientationLandscapeLeft) {
    //landscape    
}


要么

if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)) {
    //landscape    
} else {
    //portrait
}