效果展示 :
系统默认的快速定位功能位置太过靠右位置,特定情况下,我们需要调整其位置,如何做呢?请看代码:
@interface UITableView (IndexViewFrame)
@end
#import <objc/runtime.h>
@implementation UITableView (IndexViewFrame)
+ (void)load
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[self swizzledSelector:@selector(dd_layoutSubviews) originalSelector:@selector(layoutSubviews)];
});
}
// 交换方法
+ (void)swizzledSelector:(SEL)swizzledSelector originalSelector:(SEL)originalSelector
{
Class class = [self class];
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
BOOL didAddMethod =
class_addMethod(class,
originalSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod));
if (didAddMethod) {
class_replaceMethod(class,
swizzledSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
- (void)dd_layoutSubviews{
NSInteger indexTag = 90089;
static UIView *needView = nil;
if (!needView) {
for (UIView *subview in self.subviews) {
if ([subview isKindOfClass:NSClassFromString(@"UITableViewIndex")]) {
needView = subview;
break;
}
}
}
CGRect rt = needView.frame;
//调整其位置
rt.origin.x -= 10;
}
@end