根据UIViewController这里的文档,
使用[UIViewController attemptRotationToDeviceOrientation]强制系统尝试将界面旋转到新方向.我试图通过以下方式强制界面从纵向旋转到横向:
forceLandscape在尝试旋转之前设置标志[UIViewController attemptRotationToDeviceOrientation]-supportedInterfaceOrientations在我的视图控制器和检查标志,以改变支撑取向,如这种力量-supportedInterfaceOrientations再次被召唤,看起来像
- (NSUInteger)supportedInterfaceOrientations {
return self.forceLandscape ? UIInterfaceOrientationMaskLandscapeLeft : UIInterfaceOrientationMaskPortrait;
}
Run Code Online (Sandbox Code Playgroud)
即使-supportedInterfaceOrientations被调用并返回新方向,界面也不会旋转.我已经确认该项目允许所有方向,并且该视图控制器是窗口的根视图控制器.有没有其他人遇到这个问题并找到了解决方案?我知道所有修复这个问题(提出和解雇模态等),但如果可能的话,我想要一个干净的解决方案.我已在iOS 6和7上验证了此问题.
以下是重现的完整代码:
@interface ALTViewController ()
@property (nonatomic, assign) BOOL forceLandscape;
@end
@implementation ALTViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.forceLandscape = NO;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button setTitle:@"button" forState:UIControlStateNormal];
[button …Run Code Online (Sandbox Code Playgroud) 我的应用程序使用导航控制器,主页导航栏中有图像.背景图像是通过推杆完成的
[navigationController.navigationBar setBackgroundImage:navImage forBarMetrics:UIBarMetricsDefault];
[_window addSubview:navigationController.view];
Run Code Online (Sandbox Code Playgroud)
在应用程序委托中.导航栏显示此图像正常.
每个其他视图在导航栏中都没有图像.因此,我需要在按下任何其他视图控制器时删除此背景图像.有谁知道如何做到这一点?
在线有很多关于添加或更改导航栏图像的答案,但这个问题有点不同.先感谢您.