为了适配 iPhone X,我应该写多少代码?0。
我需要写多少代码来适配 iPhone X?0。
越来越多的应用为每个不同的视图控制器设置自定义导航栏,而不是一个通用的、全局的导航栏。
这个项目只是帮助开发者以巧妙的方式解决这个问题,开发者使用这个导航控制器的方式就像以前一样,你可以为每个视图控制器提供一个独立的导航栏。
越来越多的应用为每个 VC 设置单独的导航栏,而不是之前那样使用一个全局统一的导航栏,因为不同的 VC 有不同的视觉样式,前一个是蓝色的,后一个也许要做成红色、透明,或者干脆没有导航栏。
尽管开发者可以在每个 VC 的 - (void)viewWillAppear
(想想为什么不是 - (void)viewDidLoad
) 方法中设置自己所需的样式,但是在同一个导航栏上来回修改,稍不注意就会导致样式混乱。另一种实现方式,是隐藏全局导航条,每个 VC 自己通过 addSubview:(UIView *)view
的方式自己设置导航条。这种实现是可行的,但是使用不方便,如:
self.navigationItem.rightBarButtonItem
等来设置导航按钮,而必须自己手动向 navigationBar
上加;self.title
来修改导航标题,而必须自己添加监听;navigationBarHidden
;contentInsets
。等等。
本项目提供一种透明的方式,让开发者像以前一样使用导航器,同时,每个 push
进来的 VC 有自己独立的导航条。
支持自定义导航栏类
支持撤回(unwind)
支持旋转
支持启用和禁用交互式回退
支持 Interface Builder
每个 VC 支持自定义的 navigationBarClass
支持 unwind
(不知道什么是 unwind
?请参考:这里)
支持转屏
支持禁用交互式返回
支持 Interface Builder
建议将 RTRootNavigationController
设置为您的根视图控制器
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIViewController *yourController = ...;
self.window.rootViewController = [[RTRootNavigationController alloc] initWithRootViewController:yourController];
return YES;
}
您可以实现以下方法来自定义返回栏按钮项(推荐)
- (UIBarButtonItem *)rt_customBackItemWithTarget:(id)target
action:(SEL)action
{
return [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Back", nil)
style:UIBarButtonItemStylePlain
target:target
action:action];
}
或者只需将 useSystemBackBarButtonItem
设置为 YES 并使用默认项。
要运行示例项目,请先将仓库克隆,然后从示例目录首先运行 pod install
。
您的 ViewController 层次结构将更改为
RTRootNavigationController
`- RTContainerViewController
| `- RTContainerNavigationController
| `- YourViewController1
`- RTContainerViewController
`- RTContainerNavigationController
`- YourViewController2
因此,如果访问 self.navigationController
,则返回一个容器导航控制器,其 viewControllers
总是 1,即 self
。相反,您必须使用 self.rt_navigationController.rt_viewController
来获取所有同级项目,如所述 这里 和 这里。
RTRootNavigationController 可以通过 CocoaPods 获得。要安装
它,只需将以下行添加到您的 Podfile 中
pod "RTRootNavigationController"
rickytan,[email protected]
- (void)viewWillAppear
中做处理RTRootNavigationController 采用 MIT 许可证。有关更多信息,请参阅 LICENSE 文件。