在我的应用程序中,我使用objc_msgSend来改变整个设备的方向。在应用程序中有2个按钮(横向和纵向)。每当用户点击景观,那么应用程序应该是在肖像landscape.Similar。
但是这种方法不会每次都改变方向。它有时有效,有时无效。
代码是:-
- (void)updateOrientation
{
NSInteger mode = -1;
switch (appDel.orientationMode)
{
case 2:
{
UIDeviceOrientation orientation = (UIDeviceOrientation)[UIApplication sharedApplication].statusBarOrientation;
if (orientation == UIDeviceOrientationLandscapeLeft ||
orientation == UIDeviceOrientationLandscapeRight
|| orientation == 0)
{
// do nothing
}
else if(orientation == UIDeviceOrientationPortrait) {
mode = UIInterfaceOrientationLandscapeRight ;
}
else if (orientation == UIDeviceOrientationPortraitUpsideDown ) {
mode = UIInterfaceOrientationLandscapeLeft;
}
}
break;
case 3:
{
UIDeviceOrientation orientation = (UIDeviceOrientation)[UIApplication sharedApplication].statusBarOrientation;
if (orientation == UIDeviceOrientationPortrait ||
orientation == UIDeviceOrientationPortraitUpsideDown
|| orientation == 0)
{
// do nothing
}
else if (orientation == UIDeviceOrientationLandscapeRight) {
mode = UIInterfaceOrientationPortraitUpsideDown ;
}
else if (orientation == UIDeviceOrientationLandscapeLeft) {
mode = UIInterfaceOrientationPortrait ;
}
}
break;
}
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)] && (mode != -1))
{
objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), mode);
}
}发布于 2015-02-16 18:27:15
当您更改方向时,请尝试此代码
NSNumber *value = [NSNumber numberWithInt:YourDesiredOrientation];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];https://stackoverflow.com/questions/26356551
复制相似问题