[IOS category usage] --- modify the leftBarButtonItem icon in the navigation bar

Source: Internet
Author: User

[IOS category usage] --- modify the leftBarButtonItem icon in the navigation bar

Category: Modify the Item icon in the navigation bar of a specific ViewController.

A problem occurs in the project:
When a newly added VIewController1 wants to push to the original VIewController2
The leftBarButtonItem icon on the left of the original VIewController2 navigation bar is changed to the style that comes with the system,
In addition, because the project uses both code and StoryBoard development, it makes it more difficult to modify leftBarButtonItem in a unified manner!

In general, we will redefine a UIBarButtomItem in VIewController2, insert the following code for partial modification.
The Code is as follows:

-(void)changeleftItem{    UIImageView *imagview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 32, 32)];    imagview.image = [UIImage imageNamed:@"jiantou"];    UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"jiantou"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(back)];    self.navigationItem.leftBarButtonItem = left;}-(void)back{    [self.navigationController popViewControllerAnimated:YES];}

After the code is modified:

But the problem arises.

If I modify only one part, the amount of code is acceptable,
What I want is to push VIewController2 to the next page.
Or, if VIewController1 is pushed to the VIewController3 page, the arrow icon must be kept. Do I have to write it many times?

I don't want to write a subclass of UIViewController to inherit the class as its parent class. What should I do ??

At this time,
You can extend the methods and functions of a Category,

(Category) is an existing class (including the class cluster: NSString ..., Classes that cannot be obtained from the source code) do not need to inherit the subclass from the existing class to add new methods. The new method added by category can be inherited by the quilt class .)

Well, write a category for UIViewcontroller.

//.h#import 
  
   @interface UIViewController (changeLeftItem)-(void)changeleftItem;@end//.m-(void)changeleftItem{    UIImageView *imagview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 32, 32)];    imagview.image = [UIImage imageNamed:@"jiantou"];    UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"jiantou"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(back)];    self.navigationItem.leftBarButtonItem = left;}-(void)back{    [self.navigationController popViewControllerAnimated:YES];}
  

// Then, go to the Controller and add the header file name (or add the pch file)
Next, go to the time classification method in. h, and the effect will be displayed !!

** This is only half the amount of code. What if I want to change all the existing Arrow styles? Hey, what? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> flock/WPC9wPg0KPHByZSBjbGFzcz0 =" brush: java; "> [super viewDidLoad];[self changeleftItem];

This method ??

I haven't thought of this question for the moment. Please write down what you know first.

Okay, I solved the problem roughly.

1. Put the classification header file in the pch file of the project.

Then, because it is the Classification written by UIViewController,
So we only need to perform global search

[Super viewDidLoad];

Then replace it:

    [super viewDidLoad];    [self changeleftItem];

We can see that a total of nearly 74 items have been replaced. If you want to manually add them, it is estimated that you want to get dizzy.

** Of course, this simple and crude method also needs to be noted that there are three rootviewcontrollers in our project, and the arrows cannot be added in these three aspects. Therefore, we only need to find these three places and delete the method for calling the category,
It seems that this is a quick way.

PS: I personally feel that the classification + replacement method is better than simply writing a parent class and then let the inheritance of many sub-classes,

Modify their inheritance on the VIewController that has been written, and the workload is still relatively large.~

-- End
**

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.