- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我发现很多问题强制 UIViewController 默认横向: How to force a UIViewController to Portrait orientation in iOS 6试试这个:
- (BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeLeft;
}
但是当我在 UINavigationController 中使用 UIViewController 时,我无法强制横向。请帮助!
*工作不顺利
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
UINavigationController *navControl = [[UINavigationController alloc] initWithRootViewController:self.viewController];
[navControl setNavigationBarHidden:YES];
self.window.rootViewController = navControl;
[self.window makeKeyAndVisible];
return YES;
}
*工作正常:
self.window.rootViewController = self.viewController;
最佳答案
最好的方法是创建扩展 UINavigationController 并在扩展类中编写您的方向函数。
类声明:
@interface MyNavigationControllerViewController : UINavigationController
@property(nonatomic, assign) UIInterfaceOrientation orientation;
@property(nonatomic, assign) NSUInteger supportedInterfaceOrientatoin;
@end
MyNavigationController 的实现
@implementation MyNavigationController
@synthesize supportedInterfaceOrientatoin = _supportedInterfaceOrientatoin;
@synthesize orientation = _orientation;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
_supportedInterfaceOrientatoin = UIInterfaceOrientationMaskLandscape;
_orientation = UIInterfaceOrientationLandscapeLeft;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return _supportedInterfaceOrientatoin;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return self.orientation;
}
@end
并使用像 MyNavigationController 这样的扩展作为你的 rootviewcontroller 的导航 Controller 。
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController *viewController;
@property (strong, nonatomic) MyNavigationControllerViewController *myNavController;
- (void) reloadAppDelegateRootViewControllerLandscape;
- (void) reloadAppDelegateRootViewController;
@end
因此您的应用程序委托(delegate) didfinishlounchingwithoptions 代码如下。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
self.myNavController = [[MyNavigationController alloc] initWithRootViewController:self.viewController];
[self.myNavController setNavigationBarHidden:YES];
self.window.rootViewController = self.myNavController;
[self.window makeKeyAndVisible];
return YES;
}
唯一可以为具有相同导航 Controller 的两个不同 View 提供不同方向的方法是重新加载导航 Controller 本身。所以如果你添加两个方法
- (void) reloadAppDelegateRootViewController{
[[[UIApplication sharedApplication].delegate window] setRootViewController:nil];
[(MyNavigationControllerViewController *)self.myNavController setOrientation:UIInterfaceOrientationPortrait];
[(MyNavigationControllerViewController *)self.myNavController setSupportedInterfaceOrientatoin:UIInterfaceOrientationMaskAll];
[[[UIApplication sharedApplication].delegate window] setRootViewController:self.myNavController];
}
- (void) reloadAppDelegateRootViewControllerLandscape{
[[[UIApplication sharedApplication].delegate window] setRootViewController:nil];
[(MyNavigationControllerViewController *)self.myNavController setOrientation:UIInterfaceOrientationLandscapeLeft];
[(MyNavigationControllerViewController *)self.myNavController setSupportedInterfaceOrientatoin:UIInterfaceOrientationMaskLandscape];
[[[UIApplication sharedApplication].delegate window] setRootViewController:self.myNavController];
}
并在推送和弹出 View 后调用这些函数。
注意:- 我不知道这是好方法还是坏方法。
关于ios - 为什么我在使用 UINavigationController 时不能强制横向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14658268/
即使我的设备旋转(纵向->横向->纵向),我也需要始终以横向模式录制视频。 取角度很简单,我用的是hyroscope。我的问题是总是横向录制视频。需要任何建议。 我正在使用 AVCaptureSess
我正在 iPad(1,2) 以及 Samsung Tab 2 上测试我的网络应用程序。我有不同的 CSS(外部)集可应用于 iPad 上的横向/纵向模式和选项卡 2 上的横向/纵向模式。 但我无法为
我正在将Snappy Bundle和Symfony 2.1一起使用。 我有一些问题未在此捆绑包的文档中找到: 如何设置方向? 有没有显示页码的方法? 这是我的捆绑包的config.yml: knp_s
我一直在寻找这个问题的答案,但找不到任何答案。显然,iPhone SDK 3.0 使得 UIImagePickerController 可以以横向模式显示成为可能 - 但我没有找到任何允许这样做的方法
我有 1 个 Storyboard,我在 Swift 3 中的 Xcode 8 上使用带有大小类的自动布局。 我有这个当前的肖像 View : 目前,这是 1 个带有标签的垂直堆栈 View 。 在横
我正为这个而焦头烂额。基本上,我需要我的菜单根据菜单的内容水平放置一个容器。所以,我知道 table 和 table-cell 属性可以做到这一点。但是,当我将其与 Superfish Menu 结合
我努力做到这一点: 但我无法正确获取第三列和翻转的标题。 我现在是这样的: 最佳答案 该解决方案适用于 IE9、Chrome、Firefox、Safari 和 Opera,不需要 JavaScrip
我想要一个水平方向的 WPF DataGrid,有人知道解决方案吗? 最佳答案 我之前已经这样做了,因为我们希望能够对 DataGrid 和 PropertyGrid 使用相同的控件。很多事情都必须改
我正在开发一个适用于智能手机的网站版本,在处理智能手机横向方向的媒体查询时遇到了一个小问题。对于纵向,我正在使用以下媒体查询并且它工作得很好: @media only screen and (max-
如何让“AVCaptureVideoPreviewLayer”在横向方向上正确显示?它在纵向模式下工作正常但不旋转,并且当父 View Controller 处于横向模式时显示旋转的相机捕获。 最佳答
这个问题在这里已经有了答案: Force "portrait" orientation mode (12 个回答) 关闭2年前。 我的第一款 Android 游戏即将完成,但我遇到了一个非常简单的问题
我的数据在其中一个字段中具有嵌套映射。例如,数据位于名为“customers”的表中,如下所示: 姓名:比尔·琼斯 地址:{"billing":{"street":"123 Main", "city"
我正在横向模式下运行应用程序。我需要注意什么才能使拍摄的屏幕截图也处于横向模式。 我在 iPhone 应用程序中使用以下代码,底部有标签栏并拍摄屏幕截图,但它始终仅处于纵向模式。为什么? UIWind
谁能举例说明如何将单点触控应用程序的 View 从纵向旋转为横向,反之亦然? 最佳答案 如果您在 Interface Builder 中设置 ui 元素的几何形状,请确保在 Size Inspecto
我正在使用简单的 MPMoviePlayerController。在纵向模式下,它嵌入到我的布局中。当我旋转我的设备时,我希望它全屏显示。在全屏模式下,有一个完成按钮,但它唯一的作用就是暂停视频。当点
我正在尝试读取 JSON 模式,但似乎无法弄清楚如何将所有先前对象等输出到层次结构中。 这是JSON响应方案 { "Shopping": { "Orders": { "OrderInfo": {
我想在我的布局中包含一个 SlidingDrawer,但我会将它放在布局的侧面。所以开口是从左到右,而不是从下往上。这怎么办? 最佳答案 this link可能会帮助您创建水平滑动抽屉。如果您正在使用
就在我以为我已经解决了所有问题时..我遇到了这个问题。 场景。 我有一个简单的 tableView。并在导航项的 titleView 中带有搜索栏。 SearchBar 通过 View Control
我有一个带有 MGSplitView 的应用程序,其中包含表格 View 和 UIWebView,该应用程序固定为横向。 Web View 有一个附加到 Web View 的 UITapGesture
我有一部 iPhone,我正在添加横向支持。之前只是肖像。 当键盘显示时,我无法将文本字段移开。 我使用的解决方案与记录在案的 Apple 解决方案非常相似 here . 该解决方案在纵向模式下工作正
我是一名优秀的程序员,十分优秀!