怎么强制UIViewController从ipad竖屏横屏怎么调进入横屏

[求教][IOS orientation] IOS-7如何固定单个viewController的方向?
10:56:37 +08:00 · 14753 次点击
app需要支持两个屏幕方向,其中一些controller是固定竖屏,一些是固定横屏,我试了下stackoverflow中的这种方法, 我写了一个sample code 在GitHub上 :分别设置第一个VC(View Controller)仅仅支持LandscapeRight,第二个VC支持Portrait.问题在于:1. 第一个VC(View Controller) LandscapeRight。 [没问题] 2. 紧接着点击button进入第二个VC 还是LandscapeRight
[有问题] ,然后你尝试着晃动手机。第二个VC 将成为 Portrait。3. 再紧接着点击返回时, 第一个又是 LandscapeRight请问大家知不知道如何解决这个问题?
27 回复 &| &直到
08:00:00 +08:00
& & & 11:07:22 +08:00
第二个 VC 内,viewWillAppear: 内可以使用 [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait]
& & 11:09:34 +08:00
在 nav 那里下断点 你会发现 进了二级之后 没有触发 shouldAutorotate
& & & 11:13:36 +08:00
可以参考这篇文章:
& & 11:23:53 +08:00
@ 这个我发现了
& & 11:27:09 +08:00
@ 黑科技:
objc_msgSend([UIDevice currentDevice], @(setOrientation:), UIInterfaceOrientationPortrait); 写在 第二个页面里面 viewWillAppear 里面。。。。
然后加入#import &objc/message.h&
原来iOS5 的API iOS6 消失了= =
& & 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]];
[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 = CGRectZ
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 =
[UIView commitAnimations];
[UIViewController attemptRotationToDeviceOrientation];
}
& & 11:31:57 +08:00
iOS5 的API iOS6 消失了= = 这个蛮难
你的例子有sample 吗
& & 11:32:15 +08:00
@ 不work 啊
& & 11:56:05 +08:00
& & 12:00:49 +08:00
@ objc_msgSend([UIDevice currentDevice], @(setOrientation:), UIInterfaceOrientationPortrait); 这么写有问题,最后一个忘了加括号...
objc_msgSend([UIDevice currentDevice], @(setOrientation:), @(UIInterfaceOrientationPortrait));
否则永远都是portrait
& & 12:49:01 +08:00 via iPhone
我是这样做的
& & 13:43:53 +08:00
只 iOS 7 的话,ViewController 子类覆盖下面两个方法就 OK 了吧:
- (UIInterfaceOrientation)preferredInterfaceOrientationForP
- (NSUInteger)supportedInterfaceO
& & 15:41:56 +08:00
当pop的时候,也就是3,无法触发方法 shouldAutorotate
& & 15:42:39 +08:00
你看看我的GitHub code 就是 覆盖了方法
& & 15:43:59 +08:00
因为View Controller Is Hidden
& & 15:57:15 +08:00
我把你的code放进来 运行还是有问题
& & 16:15:44 +08:00
- shouldAutorotate 和 - supportedInterfaceOrientations 只有设备改变方向的时候才会被调用,光覆写着两个方法是不行的。
& & 17:02:20 +08:00
@ 还有什么做法吗?
txx我的sample code加了一段强制rotation的代码
& & 17:42:37 +08:00 via iPhone
@ pop出来你要旋转的还是nav bar,而不是你push出来的,你得明白了vc之间的逻辑关系才好对症下药
& & 18:25:37 +08:00
我看到你头像进来的。
& & 18:26:40 +08:00
能丢个code 出来看看吗? 我不是很理解你的意思
& & 18:34:54 +08:00 via iPhone
@ 为什么还没截贴呢 就因为我用了私有api么?
& & 19:18:39 +08:00
就是其他人也有说了一些方法,我试了不可行。 就问问什么原理
& & 22:46:40 +08:00
在你的NavigationController里面加几句话:
- (void)viewDidLoad
{
[super viewDidLoad];
self.delegate =
}
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
UIViewController *vc = [[UIViewController alloc] init];
[self presentViewController:vc animated:NO completion:NULL];
[vc dismissViewControllerAnimated:NO completion:NULL];
}
详情见http://stackoverflow.com/questions//presenting-navigation-controller-in-landscape-mode-is-not-working-ios-6-0
主要是,你一直是在push和pop,rootViewController始终NavigationController。不旋转设备,不会引起auto rotate动作,只能用一些其他奇葩的方法了。
你要用presentViewController,改变了rootViewController,就不会有这些问题了。
& & 23:12:48 +08:00
在CRPortaint类中加上这个来触发方向更新
- (void)viewWillAppear:(BOOL)animated
{
[self presentViewController:[UIViewController new] animated:NO completion:^{ [self dismissViewControllerAnimated:NO completion:nil]; }];
}
& & 07:28:36 +08:00
@ 其实这种交互设计不符合苹果的要求。具体到你这个例子,第一个viewController应该所有方向都支持,然后第二个可以固定一个方向。从第二个返回时,方向保持不变。
& & 08:29:12 +08:00
必须是modal 出来的 view 才可以变换设备方向。
navigation controller Pop Push的view 是 苹果设计HIG的原则。
& · & 2942 人在线 & 最高记录 3541 & · &
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.0 · 55ms · UTC 02:43 · PVG 10:43 · LAX 18:43 · JFK 21:43? Do have faith in what you're doing.ios 相机界面强制横屏
调用的相机默认是竖屏的,网上找了很多方法强制横屏都无效,以下代码经测试兼容ios78
自定义一个UIImagePickerController并且覆盖以下方法:
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationLandscapeL
- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskL
- (BOOL)shouldAutorotate {
return YES;
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if(UIInterfaceOrientationIsLandscape(interfaceOrientation)) {
return YES;
return NO;
要兼容ios8还需要在delegate中加入以下代码
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
NSString* strSubClass = [NSString stringWithUTF8String:object_getClassName(window.rootViewController.presentedViewController)];
if ([@&ImgTakeViewController& isEqualToString:strSubClass]) {
return UIInterfaceOrientationMaskA
return [application supportedInterfaceOrientationsForWindow:window];4168人阅读
整个项目是竖屏的,不能横屏,但是有个播放界面必须要横屏于是就开始找各种横屏的方法,最后在手机上好使了,但是在pad上横屏启动的时候界面是横屏显示,很是苦恼,就又开始了漫长的找资料,直接上代码
(1),plist文件,如图所示,第一项是建立项目时默认有的表示支持手机的屏幕方向(我把支持向右和向左的删了),第二项是后加的表示支持ipad(添加 Supported interface orientations (iPad))的旋转方向(同样我把向左向右,向上的给删了)
2,开始代码配置(主要讲解push界面的强制横屏)主要是借鉴简书这位大神的讲解
(1),AppDelegate.h里面
@property (assign , nonatomic) BOOL isForceL
@property (assign , nonatomic) BOOL isForceP
AppDelegate.m里面
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
if (self.isForceLandscape) {
return UIInterfaceOrientationMaskL
}else if (self.isForcePortrait){
return UIInterfaceOrientationMaskP
return UIInterfaceOrientationMaskP
(2),在相应的tabBarController里面(我是在viewController里面写的)
#import &UIKit/UIKit.h&
#import "BaseViewController.h"
@interface RootViewController : BaseViewController
@property (strong,nonatomic) UITabBarController *tabBarC
-(BOOL)shouldAutorotate{
return [self.tabBarCon.selectedViewController shouldAutorotate];
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
[self.tabBarCon.selectedViewController supportedInterfaceOrientations];
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
[self.tabBarCon.selectedViewController preferredInterfaceOrientationForPresentation];
#import &UIKit/UIKit.h&
@interface RootNavigationController : UINavigationController
@property (nonatomic , assign) UIInterfaceOrientation interfaceO
@property (nonatomic , assign) UIInterfaceOrientationMask interfaceOrientationM
-(BOOL)shouldAutorotate{
return YES;
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return self.interfaceOrientationMask;
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return self.interfaceOrientation;
(4)在父类viewController里面
- (void)viewDidLoad {
[super viewDidLoad];
[UIViewController attemptRotationToDeviceOrientation];
(5),在需要强制横屏的地方调用
- (void)viewWillAppear:(BOOL)animated{
self.navigationController.navigationBar.hidden = YES;
self.navigationController.tabBarController.tabBar.hidden = YES;
[self forceOrientationLandscape];
RootNavigationController *nav = (RootNavigationController *)self.navigationController;
nav.interfaceOrientation = UIInterfaceOrientationLandscapeR
nav.interfaceOrientationMask = UIInterfaceOrientationMaskLandscapeR
[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationLandscapeRight) forKey:@"orientation"];
[UIViewController attemptRotationToDeviceOrientation];
- (void)viewWillDisappear:(BOOL)animated{
[self forceOrientationPortrait];
RootNavigationController *navi = (RootNavigationController *)self.navigationController;
navi.interfaceOrientation = UIInterfaceOrientationP
navi.interfaceOrientationMask = UIInterfaceOrientationMaskP
[[UIDevice currentDevice] setValue:@(UIDeviceOrientationPortrait) forKey:@"orientation"];
[UIViewController attemptRotationToDeviceOrientation];
mark 横屏设置
- (void)forceOrientationLandscape{
AppDelegate *appdelegate=(AppDelegate *)[UIApplication sharedApplication].delegate;
appdelegate.isForceLandscape=YES;
appdelegate.isForcePortrait=NO;
[appdelegate application:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:self.view.window];
- (void)forceOrientationPortrait{
AppDelegate *appdelegate=(AppDelegate *)[UIApplication sharedApplication].delegate;
appdelegate.isForcePortrait=YES;
appdelegate.isForceLandscape=NO;
[appdelegate application:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:self.view.window];
总结,感觉这位大神已经总结的很到位了,希望可以帮到需要的人
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:42112次
排名:千里之外
原创:57篇
(1)(1)(1)(3)(8)(18)(27)(1)
(window.slotbydup = window.slotbydup || []).push({
id: '4740887',
container: s,
size: '250,250',
display: 'inlay-fix'项目需要,只有某个界面需要横屏显示,其它全只支持竖屏显示即可,网上资料很多,但是试过都不好用,最后发现是因为我的项目UIViewController外层是UINavigationVeiwController,只在UIViewController重载supportedInterfaceOrientations与shouldAutorotate 方法是不行的。
下面说明具体设置步骤:(参考)
Step 1:Info.plist中设置Supported interface orientations 为所有支持的方向,我是这样设置的:
Step 2:在自定义的navigationViewController中添加属性:
@property (nonatomic, assign) BOOL supportL
 初始化:
- (id)init
if (self = [super init])
[self setup];
- (void)setup
//其他设置
self.supportLandscape = NO;
 重载supportedInterfaceOrientations方法:
- (UIInterfaceOrientationMask) navigationControllerSupportedInterfaceOrientations:(UINavigationController *) navigationController{
if(self.supportLandscape){
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeR
return UIInterfaceOrientationMaskP
Step 3:我的项目中所有viewController都是由navigationController控制的,所以,只需要在需要支持横屏的viewController中进行设置就好了:
-(void) viewDidAppear:(BOOL) animated{
[SlideNavigationController sharedInstance].supportLandscape = YES;
-(void) viewDidDisappear:(BOOL) animated{
[SlideNavigationController sharedInstance].supportLandscape = NO;
 亲测好用~
阅读(...) 评论()iOS 部分界面强制横屏与强制竖屏 - 简书
iOS 部分界面强制横屏与强制竖屏
强制横屏(此方法为旋转视图)
- (void)loadView{
self.view = [[UIView alloc]init];
CGRect frame = [UIScreen mainScreen].
AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].
UIDeviceOrientation duration = [[UIDevice currentDevice]orientation];
if (duration == UIDeviceOrientationLandscapeLeft) {
app.window.transform = CGAffineTransformMakeRotation(M_PI*0.5);
[[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeLeft animated:YES];
app.window.transform = CGAffineTransformMakeRotation(M_PI*1.5);
[[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeRight animated:YES];
app.window.bounds = CGRectMake(0, 0, frame.size.height, frame.size.width);
app.window.transform = CGAffineTransformI
部分代码网址:https://github.com/CCSH
用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你能获得这些料: 知道setContentView()之后发生了什么? ... Android 获取 View 宽高的常用正确方式,避免为零 - 掘金 相信有很多...
用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金Cover 有什么料? 从这篇文章中你能获得这些料: 知道setContentView()之后发生了什么? ... Android 获取 View 宽高的常用正确方式,避免为零 - 掘金相信有很多朋友...
全宋词 多人 潘阆 酒泉子(十之一) 长忆钱塘,不是人寰是天上。万家掩映翠微间。处处水潺潺。 异花四季当窗放。出入分明在屏障。别来隋柳几经秋。何日得重游。 酒泉子(十之二) 长忆钱塘,临水傍山三百寺。僧房携杖遍曾游。闲话觉忘忧。 栴檀楼阁云霞畔。钟梵清宵彻天汉。别来遥礼只焚...
最近有一个项目,例如:A界面跳转到B界面,A界面是竖屏的,B界面进入就要横屏。 花了半天的时间在网上搜索解决方案,有些论坛的大牛也就贴两行代码,具体实现也没有,对我们这种菜鸟造成一万点真实伤害。为了避免后人在浪费时间,在这里我整理一下,并且上传Demo到GitHub。在iO...
不知不觉2017年的余额已经所剩无几了 下面是我这一年来收藏的关于IOS开发的一些知识点 . iOS功能 iOS 如何跳转到系统设置里的指定子功能界面 http://blog.csdn.net/jingfa1993/article/details/ iOS开...
前段时间接到大学同学电话,多年不联系了,得知她马上博士毕业了,她同寝室的姑娘们天天给她灌输要找关系呀,没关系进不了好的高校当老师呀!当然这种观点我并不认同,但整的她整个人完全是崩溃的状态! 她电话里说“我不知道我进不了好的高校当老师,我还能去干嘛!”听到这句话的那一瞬间,我...
最近想离开一个地方换到新的一个地方,开始新的生活。给身边最理智关系最好的盆友说了之后,我们就聊了半晚上。各自说到了最近的近况及将来的打算。那晚我失眠了,我也不知道在惆怅这什么,换一份新工作,可是我发现我找不到自己的目标,甚至忘记了自己的兴趣和爱好,不知道自己的起点和终点在哪...
一年就这样过去了,回首这过去的一年,收获的是年龄,逝去的是年华,坎坷也好,顺利也罢,都将划上圆满的句号!累也好,苦也好,只要平安就好。伤我的人原谅你,我伤的人对不起,爱我的人谢谢你,我爱的人祝福你。2017年,将是我人生再次的起航点!希望好的来,坏的走,健康来,疾病去。祝福...
概述 本文就Unity游戏项目性能优化作出了总结。包括Profile工具、Unity使用、机制设计、脚本编写等方面内容。本文的测试机型皆为iPhone6。为方便找出瓶颈目标帧率先提高为60fps,后面再看实际情况是否限帧30fps。本文的Unity版本为5.5.0f3或更新...
今天推介太宰治的一部作品,也是他最负盛名的中篇小说《人间失格》,又名《丧失为人的资格》。这部影响力非常大的作品发表于1948年,其实是一部自传体小说。纤细的自传体中透露出极致的颓废,是太宰治毁灭式的绝笔之作。在完成本篇作品之后,太宰治终归还是选择了投水的方式,为自己的生命画...

我要回帖

更多关于 ipad竖屏横屏怎么调 的文章

 

随机推荐