在浏览stackoverflow寻找答案后,我决定问一个问题。
根据我的理解,我应该覆盖supportedInterfaceOrientation来处理方向。例如,我这样实现了它
- (NSUInteger)supportedInterfaceOrientations {
if (self.forceLandscape) {
return UIInterfaceOrientationMaskLandscape;
}
return UIInterfaceOrientationMaskPortrait;
}
这使控制器在呈现时以横向模式启动,并在默认情况下启用forceLandscape。然后有一个按钮可以改变按下按钮的方向
- (IBAction)buttonPress:(id)sender {
self.forceLandscape = !self.forceLandscape;
UIInterfaceOrientation o = UIInterfaceOrientationPortrait;
if (self.forceLandscape) {
o = UIInterfaceOrientationLandscape;
}
[UIApplication sharedApplication].statusBarOrientation = o;
}
按下按钮可以在纵向和横向模式之间切换。通过设置状态栏方向,它将调用supportedInterfaceOrientations来更改我的方向。它会在第一次按下按钮时调用方法并返回蒙版肖像,但它不会改变我的方向。这是我想解决的问题。希望有一个解决方法。
将状态栏方向更改为此代码
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: o] forKey:@"orientation"];
是否调用supportedInterfaceMethod并确实更改了方向。但是它只能工作一次并且它可以访问私有代码并且将被Apple拒绝,这是不可取的。
不确定此解决方案是否有效。在我的项目(iOS6,7)中,我修正了方向,所以我不需要强制改变方向。但是,我发现UIViewController中的一个函数“尝试将设备方向旋转到正确的方向
-(BOOL)shouldAutorotate {
FLog(@"");
if (self.forceLandscape) { //force to landscape
return NO;
} else {
return YES; //let's application rotate it self
}
}
-(NSUInteger)supportedInterfaceOrientations {
if (self.forceLandscape) {
return UIInterfaceOrientationMaskLandscapeLeft;
} else {
//You can just allow Portrait.
return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskPortrait;
}
}
// Notifies when rotation begins, reaches halfway point and ends.
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
//save new orientation
_myOrientation = toInterfaceOrientation;
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
}
- (IBAction)buttonPress:(id)sender {
self.forceLandscape = !self.forceLandscape;
if (self.forceLandscape) {
_myOrientation = UIInterfaceOrientationMaskLandscapeLeft
} else {
//just do nothing
}
//call this to update orientation
[UIViewController attemptRotationToDeviceOrientation];
}