Custom navigation bar in the parent and child Controllers

Source: Internet
Author: User

Custom navigation bar in the parent and child Controllers

When talking about the custom navigation bar, you first think of writing a custom navigation controller and then setting the topic of your own navigation controller. Then fill in the class of the navigation controller that wraps the controller into the custom nav that you write. If the navigation bar of an individual controller is different, write a custom nav and then get a new navigation controller to package yourself.

 

However, if a parent-child controller is used in a project, the above method will be ineffective. The reason is that the navigation bar cannot be obtained.

For example, my general architecture is a loop reference of collectionView, so that each tableview is packaged in my collectionViewcell. Then, after clicking the cell in tableview, a new interface will be pushed. At this time, no matter how you set the navigation bar style on the new interface of push, it is invisible in the program, because you can only see the top layer, that is, the navigation bar of collectionViewController.

(I believe you can understand the meaning of the text because of the many results)

Even if you set the topic of the navigation bar, we all know that this code is written in the method used to call this class for the first time.

+ (Void) initialize {// set the topic UINavigationBar * navBar = [UINavigationBar appearance]; [navBar setBarTintColor: [UIColor redColor];}

After that, I want to dynamically modify the background color or background image of the navigation bar. I can only write it when loading it. I tried to change the style after pushing the new controller and pop stack, but it is invalid and cannot be changed.

 

 

 

 

 

The controller contains two scrollviews, one is the title bar scrollView, and the other is the content below is also a scrollView. (Scrollview is used for lazy loading of each page and better performance). Then tableViewController is nested in scrollView. Add the view of tableViewController to subViews of scrollView.

ScrollView is the subView of the master controller view. The view of this tableViewController is the subView of the subView of the master controller.

Note: If the view of A is added to the subViews of the view of B, the controller of A must be added to the childControllers of the controller of B.

Otherwise, the view is passed, but the controller does not. No one cares about it?

The preceding method is used to hide the navigation bar.

Of course, when setting the interface, the arrows and posts above should be self-defined. A common view is not a navigation bar and can be used as a navigation bar. (Originally edited by Dong baoran blog Park) clicking back will bring the stack top controller to the stack, and clicking the post will push another comment controller.

Then the details controller will hide the navigation bar when it is displayed, and set the navigation bar display when the details page is about to pop up the stack.

- (void)viewWillAppear:(BOOL)animated{    [self.navigationController setNavigationBarHidden:YES animated:YES];}
- (IBAction)backBtnClick:(id)sender {    [self.navigationController popViewControllerAnimated:YES];    [self.navigationController setNavigationBarHidden:NO animated:YES];}

 

Note that you need to hide the navigation bar and write it in the appropriate method. If you write it in the method prepareForSegue to be redirected, there will be a twitching bug.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{    if ([segue.destinationViewController isKindOfClass:[SXDetailController class]]) {                NSInteger x = self.tableView.indexPathForSelectedRow.row;        SXDetailController *dc = segue.destinationViewController;        dc.newsModel = self.arrayList[x];        [self.navigationController setNavigationBarHidden:YES animated:YES];    }else{        NSInteger x = self.tableView.indexPathForSelectedRow.row;        SXPhotoSetController *pc = segue.destinationViewController;        pc.newsModel = self.arrayList[x];        [self.navigationController setNavigationBarHidden:YES animated:YES];    }    }

 

This is a negative textbook. Check the navigation bar for a twitching bug.

 

 

 

 

Some may try to directly control push and pop by setting buttons without the navigation controller.

However, if the system does not allow the system to report an error, it means that you cannot add a push operation to a controller without a navigation controller.

The method I used above is to hide the navigation controller, but its function is still in progress.

 

Hiding the navigation bar is very useful, but there is a problem. After hiding the navigation bar, the interface pushed cannot be displayed on the left.

This problem is similar: a custom return button in the upper left corner of the navigation bar overwrites the original return button. As a result, sliding return cannot be implemented.

The solution is to add this line of code to the Code pushed by the Controller or in the prepareForSegue method.

 if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {            self.navigationController.interactivePopGestureRecognizer.delegate = nil;        }

Because you overwrite the return button of the system, the system will disable this slide return function through the proxy. You have kicked the proxy in advance, in this way, the sliding return function can be reported.

For security, it is best to add this line of code to the display of the master controller.

- (void)viewWillAppear:(BOOL)animated{    [self.navigationController setNavigationBarHidden:NO animated:YES];}

If this line of code is not added, you may pull it back to the navigation bar and it is hidden. Because we only set the navigation bar display when the return button is triggered, right.

The final result can be as follows:

 

What do you think is wrong or what do you think you don't understand? Welcome to communicate with me. Welcome to follow me.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.