Andrew
Andrew

Reputation: 16051

Accessing a uinavigationbar from subclass?

I have a subclass based on UIView and i want to change the tint of the uinavigationbar but various methods I've tried haven't worked. How do i do this?

Upvotes: 0

Views: 681

Answers (2)

Sid
Sid

Reputation: 9536

As long as the view is in a viewController that's part of the navigation stack you can do:

viewController.navigationController.navigationBar.tintColor = [UIColor blackColor];

To access the viewController, you can declare an id as an ivar in your View class:

id myParentViewController;

Declare this id as a property so it can be accessed from outside the View.

When creating the view from the viewController, you could pass a reference to the viewController into the view by doing:

view.myParentViewController = self;

Where self would be the reference to your viewController on the navigation stack.

Now that you're in the view, you can access the navigation bar by doing this:

if([myParentViewController isKindOfClass:[UIViewController class]])
    {
        UIViewController *theParentViewController = (UIViewController*)myParentViewController;
        theParentViewController.navigationController.navigationBar.tintColor = [UIColor blackColor];
    }

Upvotes: 2

AechoLiu
AechoLiu

Reputation: 18368

You can access UINavigationBar from UINavigationController. One method is pass the UINavigationController or UINavigationBar as a parameter to the UIView. Or the UIView can use delegate to notify the outer UIViewController to do works.

Upvotes: 1

Related Questions