Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[AppBar] Fix bug where root view controller during initialization of …
…nav controller would not be injected with an App Bar. (#4691)

Verified that the added test fails prior to this change and succeeds after.

Closes https://github.com/material-components/material-components-ios/issues/4688
  • Loading branch information
jverkoey committed Aug 3, 2018
1 parent da322f7 commit ade5b5d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
4 changes: 4 additions & 0 deletions components/AppBar/src/MDCAppBarNavigationController.h
Expand Up @@ -61,6 +61,10 @@
To theme the injected App Bar, implement the delegate's
-appBarNavigationController:willAddAppBar:asChildOfViewController: API.
@note If you use the initWithRootViewController: API you will not have been able to provide a
delegate yet. In this case, use the -appBarForViewController: API to retrieve the injected App Bar
for your root view controller and execute your delegate logic on the returned result, if any.
*/
MDC_SUBCLASSING_RESTRICTED
@interface MDCAppBarNavigationController : UINavigationController
Expand Down
8 changes: 8 additions & 0 deletions components/AppBar/src/MDCAppBarNavigationController.m
Expand Up @@ -56,6 +56,14 @@ - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibB
return self;
}

- (instancetype)initWithRootViewController:(UIViewController *)rootViewController {
self = [super initWithRootViewController:rootViewController];
if (self) {
[self injectAppBarIntoViewController:rootViewController];
}
return self;
}

#pragma mark - UINavigationController overrides

// Intercept status bar style inquiries and reroute them to our flexible header view controller.
Expand Down
27 changes: 27 additions & 0 deletions components/AppBar/tests/unit/AppBarNavigationControllerTests.swift
Expand Up @@ -26,6 +26,33 @@ class AppBarNavigationControllerTests: XCTestCase {
navigationController = MDCAppBarNavigationController()
}

func testInitializingWithRootViewControllerInjectsAnAppBar() {
// Given
let viewController = UIViewController()

// When
let navigationController = MDCAppBarNavigationController(rootViewController: viewController)

// Then
XCTAssertEqual(viewController.childViewControllers.count, 1,
"Expected there to be exactly one child view controller added to the view"
+ " controller.")

XCTAssertEqual(navigationController.topViewController, viewController,
"The navigation controller's top view controller is supposed to be the pushed"
+ " view controller, but it is \(viewController).")

XCTAssertTrue(viewController.childViewControllers.first is MDCFlexibleHeaderViewController,
"The injected view controller is not a flexible header view controller, it is"
+ "\(String(describing: viewController.childViewControllers.first)) instead.")

if let headerViewController
= viewController.childViewControllers.first as? MDCFlexibleHeaderViewController {
XCTAssertEqual(headerViewController.headerView.frame.height,
headerViewController.headerView.maximumHeight)
}
}

func testPushingAViewControllerInjectsAnAppBar() {
// Given
let viewController = UIViewController()
Expand Down

0 comments on commit ade5b5d

Please sign in to comment.