UITableViewIndex修改frame、sectionIndexTitlesForTableView位置

本文介绍了一个自定义方法来调整UITableView中快速定位索引的位置。通过使用Objective-C运行时功能,我们能够交换UITableView的方法实现,从而使得索引的位置可以根据需求进行微调。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 效果展示 :

 

系统默认的快速定位功能位置太过靠右位置,特定情况下,我们需要调整其位置,如何做呢?请看代码:

@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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhaocarbon

你的鼓励是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值