IOS 11 Using method swizzling, remove the text from the navigation bar back button

Source: Internet
Author: User

Method One: Set the text style of Barbuttonitem to Transparent color, the code is as follows:

[[Uibarbuttonitem appearance] Settitletextattributes:@{nsforegroundcolorattributename: [UIColor ClearColor]} Forstate:uicontrolstatenormal];   [[Uibarbuttonitem appearance] Settitletextattributes:@{nsforegroundcolorattributename: [UIColor ClearColor]} Forstate:uicontrolstatehighlighted];

In addition, this method causes the title to not be centered and is shifted a lot, as shown below (although not displayed, it also occupies a large portion of the left side of the navigation bar)

Method Two: Add a category to Uiviewcontroller, and then replace the interchange Viewdidappear with the method swzilling in the Load method, some of the code is as follows

+ (void) load {Swizzlemethod ([self class], @selector (viewdidappear:), @selector (Ac_viewdidappear));}                                              -(void) ac_viewdidappear{self.navigationItem.backBarButtonItem = [[Uibarbuttonitem alloc]                                              initwithtitle:@ "" Style:uibarbuttonitemstyleplain    Target:self Action:nil]; [Self ac_viewdidappear];} void Swizzlemethod (class class, Sel originalselector, sel swizzledselector) {//The method might not exist in the class    , but in its superclass Method Originalmethod = Class_getinstancemethod (class, Originalselector);        Method Swizzledmethod = Class_getinstancemethod (class, Swizzledselector); Class_addmethod would fail if original method already exists BOOL Didaddmethod = Class_addmethod (class, Originalselec     Tor, Method_getimplementation (Swizzledmethod), method_gettypeencoding (Swizzledmethod));   The method doesn ' t exist and we just added one if (didaddmethod) {Class_replacemethod (class, Swizzledsele    ctor, Method_getimplementation (Originalmethod), method_gettypeencoding (Originalmethod));    } else {method_exchangeimplementations (Originalmethod, Swizzledmethod); }}

Precautions:

    • To assign a value to the entire Backbuttonitem can,?? This method does not work, because backbarbuttonitem default is empty, to nil method message, the default declaration is not executed (refer to the official website)
@" ";

    • Leftbarbuttonitem and Backbarbuttonitem display relationship: Leftbarbuttonitem is the first to display the current VC Leftbarbuttonitem, No show the last VC Backbarbuttonitem, no show the title of the last VC (refer to the official website or the official website explained clearly)

IOS 11 Using method swizzling, remove the text from the navigation bar back button

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.