search

Home  >  Q&A  >  body text

objective-c - 怎么用runtime 修改成员变量的值?

我想设置一个tableView 的 contentInsets 属性一直为 0,这是我的代码:

class_replaceMethod([UITableView class], @selector(setContentInset:), (IMP)setContentInsetZero, "@");

setContentInsetZero函数 :

void setContentInsetZero(id SELF, SEL _cmd, UIEdgeInsets insets) {
    
    Ivar tableViewInsets = class_getInstanceVariable([UITableView class], "_contentInset");

    object_setIvar(SELF, tableViewInsets, [NSValue valueWithUIEdgeInsets:UIEdgeInsetsZero]);
}

会走这个函数,但是并没有什么卵用…… 求大神解惑

黄舟黄舟2762 days ago453

reply all(1)I'll reply

  • 淡淡烟草味

    淡淡烟草味2017-04-28 09:08:09

    class_replaceMethod([UITableView class], @selector(setContentInset:), (IMP)setContentInsetZero, "@");

    Why do we need to pass "@" in the third parameter? It should be passed:

    method_getTypeEncoding(class_getInstanceMethod([UITableView class], @selector(setContentInset:)))

    reply
    0
  • Cancelreply