方法一:設置barbuttonitem的文本樣式為透明顏色,代碼如下:
1
2
|
[[uibarbuttonitem appearance] settitletextattributes:@{nsforegroundcolorattributename: [uicolor clearcolor]} forstate:uicontrolstatenormal]; [[uibarbuttonitem appearance] settitletextattributes:@{nsforegroundcolorattributename: [uicolor clearcolor]} forstate:uicontrolstatehighlighted]; |
此外這種方法會導致title不能居中,被偏移很多,如下所示(雖然不被顯示,也占了導航欄左邊很大一部分位置)
方法二:給uiviewcontroller添加類別,然后在load方法里面用method swzilling方法替換 交換viewdidappear,部分代碼如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
+( 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 will fail if original method already exists bool didaddmethod = class_addmethod( class , originalselector, method_getimplementation(swizzledmethod), method_gettypeencoding(swizzledmethod)); // the method doesn't exist and we just added one if (didaddmethod) { class_replacemethod( class , swizzledselector, method_getimplementation(originalmethod), method_gettypeencoding(originalmethod)); } else { method_exchangeimplementations(originalmethod, swizzledmethod); } } |
注意事項:
要給整個backbuttonitem賦值才可以,這種方法不行,因為backbarbuttonitem默認為空,給nil方法消息,默認聲明都不執行(參考官網)
self.navigationitem.backbarbuttonitem.title = @" ";
leftbarbuttonitem 與backbarbuttonitem 的顯示關系:
有leftbarbuttonitem則優先顯示當前vc的leftbarbuttonitem,無則顯示上個vc的backbarbuttonitem,再無則顯示上個vc的title(參考官網 還是官網解釋的清楚)
總結
以上所述是小編給大家介紹的ios 11 使用兩種方法替換(method swizzling)去掉導航欄返回按鈕的文字,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:https://www.cnblogs.com/Apple2U/archive/2018/05/04/8991662.html