[求教][IOS orientation] IOS-7如何固定单个viewController的方向?

2014-01-19 10:56:37 +08:00
 parkman
app需要支持两个屏幕方向,其中一些controller是固定竖屏,一些是固定横屏,我试了下stackoverflow中的这种方法,
http://stackoverflow.com/questions/12520030/how-to-force-a-uiviewcontroller-to-portait-orientation-in-ios-6

我写了一个sample code 在GitHub上 https://github.com/cseparkman/onlyTest:
分别设置第一个VC(View Controller)仅仅支持LandscapeRight,第二个VC支持Portrait.

问题在于:
1. 第一个VC(View Controller) LandscapeRight。 [没问题]
2. 紧接着点击button进入第二个VC 还是LandscapeRight [有问题] ,然后你尝试着晃动手机。第二个VC 将成为 Portrait。
3. 再紧接着点击返回时, 第一个又是 LandscapeRight


请问大家知不知道如何解决这个问题?
17141 次点击
所在节点    iDev
27 条回复
Kai
2014-01-19 11:07:22 +08:00
第二个 VC 内,viewWillAppear: 内可以使用 [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait]
txx
2014-01-19 11:09:34 +08:00
在 nav 那里下断点 你会发现 进了二级之后 没有触发 shouldAutorotate
Kai
2014-01-19 11:13:36 +08:00
parkman
2014-01-19 11:23:53 +08:00
@txx 这个我发现了
txx
2014-01-19 11:27:09 +08:00
@parkman 黑科技:
objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientationPortrait); 写在 第二个页面里面 viewWillAppear 里面。。。。

然后加入#import <objc/message.h>

原来iOS5 的API iOS6 消失了= =
bytelee
2014-01-19 11:28:52 +08:00
直接贴给你方法看看吧:
- (void)setOristation:(UIDeviceOrientation)oritation
{
if (oritation != [[UIDevice currentDevice] orientation] && [[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)])
{
SEL selector = NSSelectorFromString(@"setOrientation:");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[UIDevice currentDevice]];
int val = oritation;
[invocation setArgument:&val atIndex:2];
[invocation invoke];

CGRect bounds = [[UIScreen mainScreen] bounds];

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
[UIView setAnimationCurve:2];
CGRect frame = CGRectZero;
if(oritation == UIDeviceOrientationLandscapeLeft)
{
frame = CGRectMake(bounds.size.width-20, 0, 20, bounds.size.height);
}
else if(oritation == UIDeviceOrientationLandscapeRight)
{
frame = CGRectMake(0, 0, 20, bounds.size.height);
}
else if(oritation == UIDeviceOrientationPortraitUpsideDown)
{
frame = CGRectMake(0, bounds.size.height-20, bounds.size.width, 20);
}
else if(oritation == UIDeviceOrientationPortrait)
{
frame = CGRectMake(0, 0, bounds.size.width, 20);
}
self.statusBarView.frame = frame;
[UIView commitAnimations];
}
[UIViewController attemptRotationToDeviceOrientation];
}
parkman
2014-01-19 11:31:57 +08:00
@txx
iOS5 的API iOS6 消失了= = 这个蛮难
你的例子有sample 吗
parkman
2014-01-19 11:32:15 +08:00
@Kai 不work 啊
txx
2014-01-19 11:56:05 +08:00
txx
2014-01-19 12:00:49 +08:00
@parkman objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientationPortrait); 这么写有问题,最后一个忘了加括号...

objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), @(UIInterfaceOrientationPortrait));

否则永远都是portrait
qdvictory
2014-01-19 12:49:01 +08:00
dorentus
2014-01-19 13:43:53 +08:00
只 iOS 7 的话,ViewController 子类覆盖下面两个方法就 OK 了吧:

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation;
- (NSUInteger)supportedInterfaceOrientations;

ref: https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/RespondingtoDeviceOrientationChanges/RespondingtoDeviceOrientationChanges.html
parkman
2014-01-19 15:41:56 +08:00
@qdvictory 当pop的时候,也就是3,无法触发方法 shouldAutorotate
parkman
2014-01-19 15:42:39 +08:00
@dorentus 你看看我的GitHub code 就是 覆盖了方法
parkman
2014-01-19 15:43:59 +08:00
parkman
2014-01-19 15:57:15 +08:00
@qdvictory
我把你的code放进来 运行还是有问题
PrideChung
2014-01-19 16:15:44 +08:00
- shouldAutorotate 和 - supportedInterfaceOrientations 只有设备改变方向的时候才会被调用,光覆写着两个方法是不行的。
parkman
2014-01-19 17:02:20 +08:00
@PrideChung 还有什么做法吗?

txx我的sample code加了一段强制rotation的代码
qdvictory
2014-01-19 17:42:37 +08:00
@parkman pop出来你要旋转的还是nav bar,而不是你push出来的,你得明白了vc之间的逻辑关系才好对症下药
ZircoN
2014-01-19 18:25:37 +08:00
我看到你头像进来的。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/97577

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX