国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > iOS > 正文

iOS 11 使用兩種方法替換(Method Swizzling)去掉導(dǎo)航欄返回按鈕的文字

2020-07-26 02:28:16
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

  方法一:設(shè)置BarButtonItem的文本樣式為透明顏色,代碼如下:

[[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]} forState:UIControlStateNormal]; [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]} forState:UIControlStateHighlighted]; 

   此外這種方法會(huì)導(dǎo)致title不能居中,被偏移很多,如下所示(雖然不被顯示,也占了導(dǎo)航欄左邊很大一部分位置)

     方法二:給UIViewController添加類別,然后在load方法里面用Method Swzilling方法替換 交換ViewDidAppear,部分代碼如下

+(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);  }}

注意事項(xiàng):

要給整個(gè)backButtonItem賦值才可以,👇這種方法不行,因?yàn)閎ackBarButtonItem默認(rèn)為空,給nil方法消息,默認(rèn)聲明都不執(zhí)行(參考官網(wǎng))

self.navigationItem.backBarButtonItem.title = @" ";

leftBarButtonItem 與backBarButtonItem 的顯示關(guān)系:

有l(wèi)eftBarButtonItem則優(yōu)先顯示當(dāng)前VC的leftBarButtonItem,無(wú)則顯示上個(gè)VC的backBarButtonItem,再無(wú)則顯示上個(gè)VC的title(參考官網(wǎng)   還是官網(wǎng)解釋的清楚)

總結(jié)

以上所述是小編給大家介紹的iOS 11 使用兩種方法替換(Method Swizzling)去掉導(dǎo)航欄返回按鈕的文字,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)武林網(wǎng)網(wǎng)站的支持!

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 公安县| 明溪县| 甘南县| 龙里县| 怀集县| 华亭县| 滨州市| 旌德县| 平湖市| 静海县| 洛隆县| 保德县| 城口县| 台中县| 宜州市| 涞源县| 宜都市| 桃源县| 寿阳县| 上林县| 泸定县| 贵德县| 城市| 北辰区| 玉龙| 喀什市| 定边县| 衡南县| 句容市| 宣武区| 阿克苏市| 浮山县| 民丰县| 雷州市| 卓尼县| 抚州市| 龙岩市| 阿鲁科尔沁旗| 龙井市| 连州市| 广东省|