ios怎么控制某一个ios 根视图控制器切换横屏

iOS 问题:本人是个菜鸟,IOS8.1
问题:有很多画面强制其中一个画面横屏,找了很多资料,还是没有实现,求大神帮帮忙。 谢谢 - Code4App.com
本人是个菜鸟,IOS8.1
问题:有很多画面强制其中一个画面横屏,找了很多资料,还是没有实现,求大神帮帮忙。 谢谢
共有 1 个回答
登录后方可回复
登录后方可回答iOS 全局禁止横屏,但视频播放界面选择性横屏的解决办法
有时我们的APP并没有适配横屏的需求,但是在个别视频播放界面,我们需要在播放视频的时候横屏,退出全屏的时候不能横屏,但是有时候并没有原生API并没有给出解决方案。
这个解决方法比较容易
在 APPDelegate.h 文件中增加属性:是否支持横屏
是否允许横屏的标记 */
@property (nonatomic,assign)BOOL allowR
在 APPDelegate.m 文件中增加方法,控制全部不支持横屏
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if (self.allowRotation) {
UIInterfaceOrientationMaskAllButUpsideD
return UIInterfaceOrientationMaskP
这样在其他界面想要横屏的时候,我们只要控制 allowRotation 这个属性就可以控制其他界面进行横屏了。
//需在上面#import &AppDelegate.h&
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.allowRotation = YES;
//不让横屏的时候 appDelegate.allowRotation = NO;即可
所以这里可以使用 UIWindow 的通知,就可以解决
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(begainFullScreen) name:UIWindowDidBecomeVisibleNotification object:nil];//进入全屏
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endFullScreen) name:UIWindowDidBecomeHiddenNotification object:nil];//退出全屏
在退出全屏时,增加逻辑让其强制竖屏,这样当全屏播放的时候,点击 down(&完成&) 时,就会自动变成竖屏了。
// 进入全屏
-(void)begainFullScreen
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.allowRotation = YES;
// 退出全屏
-(void)endFullScreen
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.allowRotation = NO;
//强制归正:
if ([[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 =UIInterfaceOrientationP
[invocation setArgument:&val atIndex:2];
[invocation invoke];
亦可以建一baseViewController 其他控制器继承base 在base里设置禁止横屏 某一页面需要横屏 设置一下即可主题 : iOS特定界面强制横屏的问题
级别: 新手上路
UID: 372135
可可豆: 55 CB
威望: 52 点
在线时间: 199(时)
发自: Web Page
来源于&&分类
iOS特定界面强制横屏的问题&&&
&& 最近对接一个外包项目,首页点击跳转到 我自己的Controller,我自己的controller又把外包SDK的controller 的view ,addsubView到我的上,然后我的viewController重写了横屏的代码,测试测出了如下问题:图一中就是被点击的界面,点击之后present一下,会发现有时候出现图二的问题,但是是一瞬间的,等present的页面出来后,就横屏了,横屏代码如下:- (UIInterfaceOrientationMask)supportedInterfaceOrientations {return UIInterfaceOrientationMaskLandscapeR}- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {return UIInterfaceOrientationLandscapeR}我分析了一下,可能是因为图一的界面设置为竖屏,present 和横屏的代码同时执行,但是横屏先走了,导致我支持横屏的界面还没盖上去,就横屏了,就会漏出图二这种情况,包括下半部分的黑色也不是window,其中我就想着分开一下两个的调用时机,上边的两个方法是重写VC的,是系统自己觉得什么时候调用的,后来看到了一种私有api,后来改为kvo或者runtime来调用就可避免私有api的使用,代码如下:&&NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];[[UIDevice currentDevice]setValue:value forKey:@&orientation&]; 图一
图二 但是还是出现图二这种问题,谁有好的解决方案没
级别: 新手上路
可可豆: 59 CB
威望: 59 点
在线时间: 20(时)
发自: Web Page
应该是addsubview,里子view还是竖屏的frame,应该在layout后,再设置一下子view的frame,
级别: 新手上路
UID: 564060
可可豆: 163 CB
威望: 123 点
在线时间: 79(时)
发自: Web Page
楼主解决了吗?
级别: 新手上路
UID: 564060
可可豆: 163 CB
威望: 123 点
在线时间: 79(时)
发自: Web Page
回 楼主(周彦彤) 的帖子
这个是怎么产生的,能看下你的present是写在哪的吗
级别: 新手上路
UID: 372135
可可豆: 55 CB
威望: 52 点
在线时间: 199(时)
发自: Web Page
回 1楼(keruiyun) 的帖子
还没解决,优化了一下,少了很多,但是还是有时候会出来
级别: 新手上路
可可豆: 7 CB
威望: 7 点
在线时间: 154(时)
发自: Web Page
目测是View没有用autolayout,或者没有用对
级别: 骑士
UID: 384085
可可豆: 431 CB
威望: 393 点
在线时间: 605(时)
发自: Web Page
我也觉得引用 引用第5楼kedengguo于 15:42发表的&&:目测是View没有用autolayout,或者没有用对
关注本帖(如果有新回复会站内信通知您)
发帖、回帖都会得到可观的积分奖励。
按"Ctrl+Enter"直接提交
关注CocoaChina
关注微信 每日推荐
扫一扫 关注CVP公众号
扫一扫 浏览移动版iOS开发中控制屏幕旋转的编写方法小结
转载 &更新时间:日 09:52:22 & 作者:念茜
这篇文章主要介绍了iOS开发中控制屏幕旋转的编写方法小结,包括横竖屏切换时视图所出现的问题等经常需要注意的地方,需要的朋友可以参考下
在iOS5.1 和 之前的版本中, 我们通常利用 shouldAutorotateToInterfaceOrientation: 来单独控制某个UIViewController的旋屏方向支持,比如:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation&
&&& return (interfaceOrientation == UIInterfaceOrientationPortrait);&
但是在iOS6中,这个方法被废弃了,使用无效。
shouldAutorotateToInterfaceOrientation:
Returns a Boolean value indicating whether the view controller supports the specified orientation. (Deprecated in iOS 6.0. Override the supportedInterfaceOrientations andpreferredInterfaceOrientationForPresentation methods instead.)
实践后会发现,通过supportedInterfaceOrientations的单独控制是无法锁定屏幕的。
-(NSUInteger)supportedInterfaceOrientations&
&&& return UIInterfaceOrientationMaskP&
多次实验后总结出控制屏幕旋转支持方向的方法如下:
子类化UINavigationController,增加方法
- (BOOL)shouldAutorotate&
&&& return self.topViewController.shouldA&
- (NSUInteger)supportedInterfaceOrientations&
&&& return self.topViewController.supportedInterfaceO&
并且设定其为程序入口,或指定为 self.window.rootViewController
随后添加自己的view controller,如果想禁止某个view controller的旋屏:(支持全部版本的控制)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation&
&&& return (interfaceOrientation == UIInterfaceOrientationPortrait);&
-(BOOL)shouldAutorotate&
&&& return NO;&
-(NSUInteger)supportedInterfaceOrientations&
&&& return UIInterfaceOrientationMaskP&
如果想又开启某个view controller的全部方向旋屏支持:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation&
&&& return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);&
-(NSUInteger)supportedInterfaceOrientations&
&&& return UIInterfaceOrientationMaskAllButUpsideD&
-(BOOL)shouldAutorotate&
&&& return YES;&
从而实现了对每个view controller的单独控制。
顺便提一下,如果整个应用所有view controller都不支持旋屏,那么干脆:
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window&
&&&& return UIInterfaceOrientationMaskP&
横竖屏切换,视图乱了怎么办?
首先,我们必须了解一下下列4种状态,它们被用来描述设备旋转方向:
对于旋屏的处理,大致分为如下几种情况和思路:
也许,你不需要旋屏支持,而希望锁定屏幕
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation&
&&& return NO;&
也许,你需要支持旋屏,或者支持部分方向的旋屏
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {&&
&&&&&& return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);&
也许,你的view有张背景图,旋屏时系统帮助你拉伸了图片,但是却没有管你的其它部件,比如button,你希望直接改变button的大小和位置
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration&
&&& if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {&
&&&&&&& NSLog(@"现在是竖屏");&
&&&&&&& [btn setFrame:CGRectMake(213, 442, 340, 46)];&
&&& if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {&
&&&&&&& NSLog(@"现在是横屏");&
&&&&&&& [btn setFrame:CGRectMake(280, 322, 460, 35)];&
也许,你并不希望用绝对坐标去约束控件,而是希望让它通过旋转自己适应屏幕的旋转
- (void)viewDidLoad&
&&& [super viewDidLoad];&
&&& // Do any additional setup after loading the view, typically from a nib.&
&&& UIDevice *device = [UIDevice currentDevice];&&
&&& [device beginGeneratingDeviceOrientationNotifications];&
&&& //利用 NSNotificationCenter 获得旋转信号 UIDeviceOrientationDidChangeNotification&
&&& NSNotificationCenter *ncenter = [NSNotificationCenter defaultCenter];&&
&&& [ncenter addObserver:self selector:@selector(orientationChanged) name:UIDeviceOrientationDidChangeNotification object:device];&
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation&
&&& return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);&
-(void)rotation_btn:(float)n&
&&& UIButton *robtn = self.&
&&& robtn.transform = CGAffineTransformMakeRotation(n*M_PI/180.0);&
-(void)orientationChanged&
&&& UIDeviceOrientation orientaiton = [[UIDevice currentDevice] orientation];&
&&& switch (orientaiton) {&
&&&&&&& caseUIDeviceOrientationPortrait:&&&&&&&&&&&&&&
&&&&&&&&&&& [self& rotation_btn:0.0];&
&&&&&&&&&&&&
&&&&&&& caseUIDeviceOrientationPortraitUpsideDown:&&&
&&&&&&&&&&& [self& rotation_btn:90.0*2];&
&&&&&&&&&&&&
&&&&&&& caseUIDeviceOrientationLandscapeLeft:&&&&&&
&&&&&&&&&&& [self& rotation_btn:90.0*3];&
&&&&&&&&&&&&
&&&&&&& caseUIDeviceOrientationLandscapeRight:&&&
&&&&&&&&&&& [self& rotation_btn:90.0];&
&&&&&&&&&&&&
&&&&&&& default:&
&&&&&&&&&&&&
也许,你需要autoresizesSubviews = YES
也许,你希望横竖屏有不同的布局效果,需要准备2份Subview,在不同状态去替换
当然不要忘记,需要调节设定图示中的1、2处,
来帮助我们完成自己想要的适应效果。Example 动画呈现的很清晰,^_^ 我就不再啰嗦了。
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具IOS 7 设置某一个UIViewController横屏,其他坚屏的问题
1.先在plist文件设置支持的方向(可以在项目的general 里面勾选设置)
2.IOS7 与 ios6的横屏坚屏设置相同,但与ios5不一样,IOS5用shouldAutorotateToInterfaceOrientation方法,而ios6 ios7,用supportedInterfaceOrientations and/or shouldAutorotate 代替。
shouldAutorotate表示是否可以横竖屏
supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation表示支持哪几种屏方向改变,支持多种时用“|”分隔。
备注:设置了shouldAutorotate为NO时,而某一页面supportedInterfaceOrientations和preferredInterfaceOrientationForPresentation支持多种时,从其他页面返回时屏的方向会与其他页面相同时(如果支持的话))
3.如果是用UItabViewControler ,UINavigationController包含UIViewController时要category,重写这几个方法,并返回当页页面的的这几个方法状态
没有更多推荐了,
加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!

我要回帖

更多关于 ios 获取父视图控制器 的文章

 

随机推荐