Skip to content

About

Implicitly make every view controller has its own navigation bar

Topics

Resources

Stars

2.1k stars

Watchers

49 watching

Forks

Repository files navigation

RTRootNavigationController

CI Version License Platform

iOS 15

Apple has changed some behavior of UINavigationBar(see)on iOS 15, developers can override this on demands:

iOS 15 上苹果改变了导航条的部分默认行为,开发者可以自己重写:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // setup appearance
    if (@available(iOS 15.0, *)) {
        [[UINavigationBar appearance] setScrollEdgeAppearance:({
            UINavigationBarAppearance *app = [UINavigationBarAppearance new];
            [app configureWithDefaultBackground];
            // init app property
            // app.backgroundColor = xxx;
            // app.shadowColor = xxx;
            app;
        })];
    } else {
        // Fallback on earlier versions
    }

    return YES;
}

iPhone Duo

The Example app automatically runs on iPhone Duo using the UIScene lifecycle, without device-specific navigation code. The screenshot shows the simulator's primary display.

示例应用使用 UIScene 生命周期,可在 iPhone Duo 上自动运行,无需针对设备编写额外的导航代码。截图展示的是模拟器的主显示屏。

Example app running on the primary display of the iPhone Duo simulator

To capture new screenshots and a simulator-only app bundle, run the Capture iPhone Duo screenshot workflow and download its artifacts. The workflow captures both display outputs in the default simulator state; it does not switch folded states.

iPhone X

How many lines of code should I write to fit in iPhone X? Zero.

我需要写多少代码来适配 iPhone X?0。

iphone-x

Introduction

More and more apps use custom navigation bar for each different view controller, instead of one common, global navigation bar.

This project just help develops to solve this problem in a tricky way, develops use this navigation controller in a farmilar way just like you used to be, and you can have each view controller a individual navigation bar.

越来越多的应用为每一个 VC 设置单独的导航条,而不是之前那样使用一个全局统一的导航条,因为不同的 VC 有不同的视觉样式,前一个是蓝色的,后一个也许要做成红色、透明,或者干脆没有导航条。

虽然开发者可以在每个 VC 的 - (void)viewWillAppear (想想为什么不是 - (void)viewDidLoad) 方法中设置自己所需的样式,但是在同一个导航条上来回修改,稍不注意就会导致样式混乱。另一种实现方式,是隐藏全局那个导航条,每个 VC 自己通过 addSubview:(UIView *)view 的方式自己设置导航条。这种实现是可行的,但是使用不方便了,如:

  • 无法使用 self.navigationItem.rightBarButtonItem 等来设置导航按钮,而必须自己手动往 navigationBar 上加;
  • 无法使用 self.title 来修改导航标题,而必须自己添加监听;
  • 无法方便地设置 navigationBarHidden;
  • 无法方便地自动调整 contentInsets。

等等。

本项目提供一种透明的方式,让开发者像以前一样使用导航器,同时,每个 push 进来的 VC 有自己独立的导航条。

Features

  • Custom navigation bar class support

  • Unwind support

  • Rotation support

  • Interactive pop enable and disable support

  • Interface Builder support

  • 每个 VC 支持自定义的 navigationBarClass

  • 支持 unwind(不知道什么是 unwind?请参考:这里)

  • 支持转屏

  • 支持禁用交互式返回

  • 支持 Interface Builder

screenshot

scrreecap

Usage

As an advise, please set RTRootNavigationController as your rootViewController:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    UIViewController *yourController = ...;
    self.window.rootViewController = [[RTRootNavigationController alloc] initWithRootViewController:yourController];
    return YES;
}

you can implement following method to customize back bar button item (Recommended):

- (UIBarButtonItem *)rt_customBackItemWithTarget:(id)target
                                          action:(SEL)action
{
    return [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Back", nil)
                                            style:UIBarButtonItemStylePlain
                                           target:target
                                           action:action];
}

or just set useSystemBackBarButtonItem to YES and use the default one.

When built with the iOS 27.1 SDK or later, simulated back items can appear in vertical bars on iPhone Duo. Text-only items temporarily use a back chevron when a vertical bar is available. Custom back views should fit the bar's width; set axisBehavior to UIBarButtonItemAxisBehaviorHorizontalOnly in the custom-back-item method to keep a view horizontal. Existing left items and system-back mode are not changed.

使用 iOS 27.1 或更新的 SDK 编译时,模拟返回按钮可显示在 iPhone Duo 的竖栏中。 纯文字按钮在可使用竖栏时会暂时显示返回箭头;自定义返回视图应适合竖栏宽度, 也可以在自定义方法中设置 axisBehavior 为 UIBarButtonItemAxisBehaviorHorizontalOnly 来保持横向显示。

The Example app on the iPhone Duo simulator, before and after folding:

示例应用在 iPhone Duo 模拟器上展开和折叠时的效果:

Unfolded / 展开 Folded, outer display / 折叠(外屏)
Example app on the unfolded iPhone Duo with the back button on the right Example app on the folded iPhone Duo outer display with the back button on the right

To run the example project, clone the repo, and run pod install from the Example directory first.

Notice(Only for below v0.6)

Your ViewController hierarchy will change to:

RTRootNavigationController
    `- RTContainerViewController
    |       `- RTContainerNavigationController
    |               `- YourViewController1
    `- RTContainerViewController
            `- RTContainerNavigationController
                    `- YourViewController2

So, if you access self.navigationController it returns a container navigation controller, and its viewControllers will always be 1, i.e. self. Instead, your have to use self.rt_navigationController.rt_viewController to get all siblings, as metioned Here and Here.

Requirements

  • iOS 11 and up
  • Xcode 14 and up

Installation

Swift Package Manager

In Xcode, choose File > Add Package Dependencies... and enter:

https://github.com/rickytan/RTRootNavigationController.git

Select the master branch until a release containing Package.swift is tagged; after that, select that release or a newer version. Add the RTRootNavigationController product to your target.

在 Xcode 中添加上述包地址;在包含 Package.swift 的版本发布之前,选择 master 分支,发布后可选择该版本或更新版本。将 RTRootNavigationController 产品添加到目标。

For a package manifest, the current branch-based dependency is:

dependencies: [
    .package(url: "https://github.com/rickytan/RTRootNavigationController.git", branch: "master")
],
targets: [
    .target(
        name: "YourTarget",
        dependencies: [.product(name: "RTRootNavigationController", package: "RTRootNavigationController")]
    )
]

Import the module with import RTRootNavigationController in Swift or @import RTRootNavigationController; in Objective-C.

The Swift package provides the default Core functionality. The optional Push subspec still requires CocoaPods because its RTInteractivePush dependency does not provide a Swift package.

Swift 包提供默认的 Core 功能;可选的 Push 子组件仍需使用 CocoaPods, 因为其 RTInteractivePush 依赖尚未提供 Swift 包。

CocoaPods

RTRootNavigationController is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "RTRootNavigationController"

Author

rickytan, ricky.tan.xin@gmail.com

Alternatives

Apps Integrated

License

RTRootNavigationController is available under the MIT license. See the LICENSE file for more info.

About

Implicitly make every view controller has its own navigation bar

Topics

Resources

Stars

2.1k stars

Watchers

49 watching

Forks

Releases

Packages

Used by

Contributors

Languages