当我设置它时,这看起来很简单,但我无法解释为什么状态栏和导航栏之间存在差距.此外,包含的视图看起来可能正确对齐,而只是向下移动的导航栏.差距看起来像状态栏的大小,所以我期望与它有关系,但我不知道什么.
以下是设置导航控制器的代码:
- (void)viewDidLoad { [super viewDidLoad]; advancedVC = [[AdvancedSearchFormVC alloc] initWithNibName:@"AdvancedSearchForm" bundle:nil]; UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:advancedVC]; nav.navigationBar.tintColor = [UIColor defaultNavBarTint]; nav.navigationBar.topItem.title = NSLocalizedString(@"SearchTitle",nil); UIBarButtonItem *searchButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"SearchButton",nil) style:UIBarButtonItemStylePlain target:self action:@selector(refreshPropertyList:)]; nav.navigationBar.topItem.rightBarButtonItem = searchButton; self.view = nav.view; }
rootViewController使用来自xib文件的视图,我已经模拟了状态栏,导航栏和标签栏.
解决方法
问题的确是,导航控制器总是期望为状态栏留出空间,这是20像素的差距.我搜索了一段时间后才发现这个解决方案有效:
//nav is assumed to be a subclass or instance of UINavigationController nav.view.frame = CGRectOffset(nav.view.frame,0.0,-20.0); //you can then add the navigation's view as a subview to something else
我最初发现了一个答案,这是对导航栏的看法的偏移,但它没有奏效.当您对导航控制器的实际视图执行此操作时,它将起作用.
我使用这种技术将导航控制器从另一个笔尖添加到我的主要笔尖的空视图,因此我可以将其放在主屏幕的任何位置作为子视图.通过使用空视图作为我的主要笔尖上的占位符和定位框架,我创建一个单独的笔尖和类来管理导航,管理用于处理其屏幕的其他笔尖.这样我可以解决经典的“如何添加一个横幅,图像或自定义视图上方导航控制器”,而导航控制器作为子视图…在iOS 5中是具体的.
还值得一提的是,我使用应用程序委托来存储和访问所有其他控制器,所以它们被一个持久性实例保留,我可以从任何类访问.在所有控制器的应用程序委托中创建和合成一些属性,并在viewDidLoad中创建实例.这样我可以在任何地方引用我应用程序中的所有控制器,方法是添加:
//this shows how to store your navigation controllers in the app delegate //assumes you've added 2 properties (UINavigationController*)"navController" and (UIViewController*)"rootController" in your app delegate //...don't forget to add #import "AppDelegate.h" to the top of the file AppDelegate *app = (AppDelegate*)[[UIApplication sharedApplication] delegate]; [app.navController pushViewController: app.rootController animated:YES]; //now apply the offset trick to remove the status gap app.navController.view.frame = CGRectOffset(app.navController.view.frame,-20.0);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。