如题,最近一个app架构为 nav + tabbar ,需求是 在点击tabbar中的一个菜单项时,弹出网页,该网页需要横屏显示,其他页面不变  都保持竖屏。

XCode Version 7.2.1

网上一搜,都说到在nav或者tabbar中设置以下3个方法。

-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait ;
}
- (BOOL)shouldAutorotate
{
return NO;
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}

确实实现了横屏的需求,但是发现了一个bug:

当app在横屏的时候 运行app  界面就是横屏了,虽然进入关闭网页时可以显示回正常竖屏,但是。。。。

最终 参考了一个老外的做法。传送门

该实现方式主要代码为:在AppDelegate中添加以下方法

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
// Get topmost/visible view controller
UIViewController *currentViewController = [self topViewController]; // Check whether it implements a dummy methods called canRotate
if ([currentViewController respondsToSelector:@selector(canRotate)]) {
// Unlock landscape view orientations for this view controller if ([currentViewController isKindOfClass:[ViewController1 class]]) {
if(((ViewController1 *)currentViewController).isShowPortrai){
return UIInterfaceOrientationMaskPortrait;
}else{
return UIInterfaceOrientationMaskLandscapeRight;
}
}
return UIInterfaceOrientationMaskLandscapeRight;
} // Only allow portrait (standard behaviour)
return UIInterfaceOrientationMaskPortrait;
} - (UIViewController*)topViewController {
return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
} - (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController {
if ([rootViewController isKindOfClass:[UITabBarController class]]) {
UITabBarController* tabBarController = (UITabBarController*)rootViewController;
return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
} else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController* navigationController = (UINavigationController*)rootViewController;
return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
} else if (rootViewController.presentedViewController) {
UIViewController* presentedViewController = rootViewController.presentedViewController;
return [self topViewControllerWithRootViewController:presentedViewController];
} else {
return rootViewController;
} }

其中改造部分为:

if(((ViewController1 *)currentViewController).isShowPortrai){
return UIInterfaceOrientationMaskPortrait;
}else{
return UIInterfaceOrientationMaskLandscapeRight;
}

完美实现此效果:进入app时竖屏,进入网页时横屏,关闭网页时返回竖屏。 特此记录

注:只需在项目中设置下图(默认设置),参考demo实现即可。 其中左右旋转看需求设置

demo地址

iOS 屏幕旋转 nav+tabbar+present(网页) 2016的更多相关文章

  1. iOS屏幕旋转 浅析

    一.两种orientation 了解屏幕旋转首先需要区分两种orientation 1.device orientation 设备的物理方向,由类型UIDeviceOrientation表示,当前设备 ...

  2. 【转】IOS屏幕旋转与View的transform属性之间的关系,比较底层

    iTouch,iPhone,iPad设置都是支持旋转的,如果我们的程序能够根据不同的方向做出不同的布局,体验会更好. 如何设置程序支持旋转呢,通常我们会在程序的info.plist中进行设置Suppo ...

  3. IOS屏幕旋转思路和实践

    这段时间同事在做一个直播项目,项目有个需求:一个界面需要手动设置屏幕的方向,设置好之后方向不能变化.完成这个需求花了特别大的精力,归因是网上关于屏幕旋转的知识比较凌乱,解决问题花费不少时间,最后决定把 ...

  4. ios 屏幕旋转的问题

    在ios6之前我们旋转屏幕只需要实现shouldAutorotateToInterfaceOrientation就行了 - (BOOL)shouldAutorotateToInterfaceOrien ...

  5. iOS屏幕旋转

    三种方法 需求:全局主要是竖屏 个别界面需要横屏

  6. 【转】IOS设备旋转的内部处理流程以及一些【优化建议】

    加速计是整个IOS屏幕旋转的基础,依赖加速计,设备才可以判断出当前的设备方向,IOS系统共定义了以下七种设备方向: typedef NS_ENUM(NSInteger, UIDeviceOrienta ...

  7. ios实现屏幕旋转的方法

    1.屏蔽AppDelegate下面的屏幕旋转方法 #pragma mark - 屏幕旋转的 //- (UIInterfaceOrientationMask)application:(UIApplica ...

  8. iOS实现屏幕旋转

    iOS实现屏幕旋转有两种方式 1. 应用本身支持 2. 手动旋转UIView (这种方式我没找到旋转 系统控件的方法 如:键盘.导航.UIAlertView) 如果你只是要把竖屏的播放器,做成支持横屏 ...

  9. 监听iOS检测屏幕旋转状态,不需开启屏幕旋转-b

    -(void)rotation_icon:(float)n { UIButton *history_btn= [self.view viewWithTag:<#(NSInteger)#>] ...

随机推荐

  1. 关于z-index的总结

    z-index的作用 很多时候需要把一个元素覆盖到另一个元素之上,比如登入弹出框等,这个时候就需要z-index属性出场了.所以呢,z-index就是调节层的显示优先级,决定哪个显示在最上方.作用范围 ...

  2. Lombok(1.14.8) - @NoArgsConstructor, @RequiredArgsConstructor & @AllArgsConstructor

    @NoArgsConstructor @NoArgsConstructor,提供一个无参的构造方法. package com.huey.hello.bean; import java.util.Dat ...

  3. Asp_CRUD

    Asp_增删改查.逻辑流程 启动服务器. 地址为127.0.0.1 端口为随机分配 2607 然后在浏览器中输入http://localhost:2670/CRUD_main.ashx 浏览器像服务器 ...

  4. 第六节 ADO.NET

    ADO.NET是一组为.NET编程人员提供数据访问服务的类,封装在System.Data.dll 中,是.NET框架的一部分,它是一种数据访问技术. 使用SQLServer数据提供程序 类 说明 Sq ...

  5. 和阿文一起学H5——H5工具、素材

    字体: 一般的H5工具都会提供一部分字体,如果提供的字体中没有自己想用的字体,可以在PPT或PS中进行加工,然后另存为图片,再导入到H5工具中去. 字全生成器: 1.http://www.diyizi ...

  6. SQL获取刚插入的记录的自动增长列ID的值

    假设表结构如下: CREATE TABLE TestTable ( id int identity, CreatedDate datetime ) SQL2005获得新增行的自动增长列的语句如下: i ...

  7. CodeBlock使用技巧

    CodeBlock是一款采用C++编写的完全开源.功能强大的IDE,工欲善其事必先利其器,为了更加方便后期的开发调试,下面先就网上的一些 官方主页地址为:    http://www.codebloc ...

  8. CICS日志---内存问题

    Level 9 COCITOOL_XA: Connected! [2014-01-09 19:46:24.296834][22347888] Level 0 TestPerormence: GDAO ...

  9. Science论文"Clustering by fast search and find of density peaks"学习笔记

    "Clustering by fast search and find of density peaks"是今年6月份在<Science>期刊上发表的的一篇论文,论文中 ...

  10. 广度优先算法BFS

    package myalgorithm; import java.util.Arrays; import java.util.LinkedList; import java.util.Queue; / ...