Search code examples
iosobjective-cuicollectionviewcellmethod-swizzling

UICollectionViewCell method swizzling


i have an unexpected behavior while swizzling a method on a UITableViewCell category

my .m file :

#import "UICollectionViewCell+CostraintFix.h"
#import <objc/runtime.h>

@implementation UICollectionViewCell (CostraintFix)

+(void)load {
    Method original_setBounds, swizzled_setBounds;
    original_setBounds = class_getInstanceMethod(self, @selector(setBounds:));
    swizzled_setBounds = class_getInstanceMethod(self, @selector(swizzled_setBounds:));

    method_exchangeImplementations(original_setBounds, swizzled_setBounds);

}

- (void) swizzled_setBounds:(CGRect) bounds{
    [self swizzled_setBounds:bounds];
    self.contentView.frame = bounds;
}
@end

Well, when i start my project i have this:

-[UINavigationButton swizzled_setBounds:]: unrecognized selector sent to instance 0x7fe2dbb301c0
  1. This is 100% a UICollectionViewCell category.
  2. At this point, in +(void) load, only UITableViewCell class are passing (as expected)

Solution

  • Found the problem. If you swizzle a method of a subclass which does not overrides the method, then you will override its super class method. its superclass could not respond to some selectors; of course this is the case.

    Thank you for the help by the way