如何在不旋转设备的情况下单击按钮更改设备方向
Posted
技术标签:
【中文标题】如何在不旋转设备的情况下单击按钮更改设备方向【英文标题】:How can i change the device orientation on button click without rotating the device 【发布时间】:2017-12-23 06:51:06 【问题描述】:我想在不旋转设备的情况下单击按钮更改设备方向。
这是我的代码:
-(void)funcLandscapeBtnTapped:(UIButton *)button
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:NO];
提前致谢。
【问题讨论】:
从上面的代码,应该发生什么? 什么都没有发生屏幕仅保持纵向模式。我希望屏幕将其方向更改为横向。 【参考方案1】:试试这个代码,它的工作原理,
您的按钮操作:
isEnablePortraidMode = true;
NSNumber *number = [NSNumber numberWithInt:UIDeviceOrientationLandscapeLeft];
NSNumber *StatusBarOrientation = [NSNumber numberWithInt:UIInterfaceOrientationMaskLandscapeLeft];
[UIViewController attemptRotationToDeviceOrientation];
[[UIDevice currentDevice] setValue:number forKey:@"orientation"];
[[UIApplication sharedApplication] performSelector:@selector(setStatusBarOrientation:) withObject:StatusBarOrientation];
[UIViewController attemptRotationToDeviceOrientation];
并在 Appdelegate 中添加此方法
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
if(isEnablePortraidMode)
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscape;
else
return UIInterfaceOrientationMaskPortrait;
【讨论】:
【参考方案2】:试试这个
AppDelegate.h
@property BOOL restrictRotation;
AppDelegate.m
#define APPDELEGATE ((AppDelegate*)[[UIApplication sharedApplication] delegate])
- (UIInterfaceOrientationMask )application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
if (self.restrictRotation == YES)
return UIInterfaceOrientationMaskLandscape;
else
if (isiPhone || isiPod)
return UIInterfaceOrientationMaskPortrait;
else
return UIInterfaceOrientationMaskLandscape;
YourViewController.m
-(void)funcLandscapeBtnTapped:(UIButton *)button
APPDELEGATE.restrictRotation = YES;
[[UIDevice currentDevice] setValue:
[NSNumber numberWithInteger: UIInterfaceOrientationLandscapeRight]
forKey:@"orientation"];
[self shouldAutorotate];
- (BOOL)shouldAutorotate
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if ( orientation == UIInterfaceOrientationLandscapeRight
| orientation == UIInterfaceOrientationLandscapeLeft)
return NO;
return YES;
【讨论】:
现在如何在 AVPlayerviewcontroller 中获得默认的全屏按钮操作?它在我们显示PlaybackControls = true 时出现。以上是关于如何在不旋转设备的情况下单击按钮更改设备方向的主要内容,如果未能解决你的问题,请参考以下文章
如何在不重新加载窗口的情况下单击按钮时在 JSP 中进行更新查询?
如何在不使用 Xpath Selenium Python 的情况下单击
如何在不注册其后面的框的单击事件的情况下单击一个html框?