iOS 11 使用方法替换(Method Swizzling),去掉导航栏返回按钮的文字
方法一:设置BarButtonItem的文本样式为透明颜色,代码如下:
[[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]} forState:UIControlStateNormal];
[[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]} forState:UIControlStateHighlighted];
此外这种方法会导致title不能居中,被偏移很多,如下所示(虽然不被显示,也占了导航栏左边很大一部分位置)

方法二:给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);
}
}
注意事项:
- 要给整个backButtonItem赋值才可以,
iOS 11 使用方法替换(Method Swizzling),去掉导航栏返回按钮的文字的更多相关文章
- iOS 隐藏/去掉 导航栏返回按钮中的文字
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(, -) forBarMetrics:U ...
- IOS 改变导航栏返回按钮的标题
IOS 改变导航栏返回按钮的标题 下午又找到了一个新的方法 这个方法不错 暂时没有发现异常的地方. 新写的App中需要使用UINavigationController对各个页面进行导航,但由于第一 ...
- 转载 iOS拦截导航栏返回按钮事件的正确方式
原文链接:http://www.jianshu.com/p/25fd027916fa 当我们使用了系统的导航栏时,默认点击返回按钮是 pop 回上一个界面.但是在有时候,我们需要在点击导航栏的返回按钮 ...
- ios 修改导航栏返回按钮的图片
修改导航栏返回按钮的图片 方法1: [UINavigationBar appearance].backIndicatorTransitionMaskImage = [UIImage imageName ...
- iOS拦截导航栏返回按钮事件的正确方式
当我们使用了系统的导航栏时,默认点击返回按钮是 pop 回上一个界面.但是在有时候,我们需要在点击导航栏的返回按钮时不一定要 pop 回上一界面,比如一个视频播放界面,进入横屏后,默认点击返回按钮仍然 ...
- iOS拦截导航栏返回按钮事件的正确方式(二)
当我们使用了系统的导航栏时,默认点击返回按钮是 pop 回上一个界面.但是在有时候,我们需要在点击导航栏的返回按钮时不一定要 pop 回上一界面,比如一个视频播放界面,进入横屏后,默认点击返回按钮仍然 ...
- iOS不得姐项目--appearance的妙用,再一次设置导航栏返回按钮,导航栏左右按钮的封装(巧用分类)
一.UI_APPEARANCE_SELECTOR 彩票项目中appearance的用法一直没有搞明白,这次通过第二个项目中老师的讲解,更深一层次的了解到了很多关于appearance的作用以及使用方法 ...
- OBJC运行时方法替换(Method swizzling)
在上周associated objects一文中,我们开始探索Objective-C运行时的一些黑魔法.本周我们继续前行,来讨论可能是最受争议的运行时技术:method swizzling. Me ...
- 拦截iOS系统导航栏返回按钮事件-三种方法
方法一:在dealloc里面书写监听事件,因为只有pop才会调用dealloc,push不会掉用 - (void)dealloc {YLLog(@"123"); } 方法二:在- ...
随机推荐
- LK光流算法的三个假设
在实际过程中采用 Lucas-Kanade 光流算法跟踪运动物体特征点的时候,一个很明显的特点是LK算法(包括其他光流算法)不能计算"大运动",加上金子塔的方法稍微好点. 这是什么 ...
- Retrofit+RxJava(2)-基本使用
首先是抽象的基类 public abstract class BaseApi { public static final String API_SERVER = "服务器地址" p ...
- linux 用户管理修改用户信息、密码状态、删除用户、退出登陆、切换用户
修改用户信息usermoduseradd支持的选项usermod都支持passwd有两个选项-l(在密码串前面加了两个叹号),-u,usermod有两个选项:-L 临时锁定用户(Lock)(在密码串前 ...
- linux 权限管理命令chmod、文件和目录的权限的意义
chmod /bin/chmod chmod [{ugoa}{+-=}{rwx}] [文件或目录]chmod [mode=421] [文件或目录]-R 递归修改 只有 root 和 所有者 可以修改一 ...
- java 内存分析之构造方法执行过程
package Demo; public class BirthDate { private int day; private int month; private int year; public ...
- PyQt4(使用ui)
1.使用qt designer设计界面,保存为test1.ui: 2.使用pyuic4 test1.ui -o ui.py生成ui代码. 3.程序载入. import sys import ui fr ...
- Replication--复制Token
--创建tokenDECLARE @tokenID AS INT;EXEC sys.sp_posttracertoken @publication = @publication,@tracer_tok ...
- oracle动态添加一条记录
/// <summary> /// 添加一个实体 /// </summary> /// <typeparam name="T">实体名称< ...
- 转:tomcat安全设置
小程序部署上去后,用户反馈说存在注入入侵等风险.反省之,记录下来 最省事的办法,直接删除%tomcatRoot%/webapps下的所有文件夹,仅仅保留自己部署的工程 前提是你不需要监控程序的一些 ...
- 使用xtrabackup不停服务做从库
一.安装xtrabackup 1.YUM安装,下载percona源: yum install http://www.percona.com/downloads/percona-release/redh ...
- iOS 隐藏/去掉 导航栏返回按钮中的文字