Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Observing UIView changes crash

i've used the following code below to determine when views get added to a superview:

//Makes views announce their change of superviews
Method method = class_getInstanceMethod([UIView class], @selector(willMoveToSuperview:));
IMP originalImp = method_getImplementation(method);

void (^ block)(id, UIView *) = ^(id _self, UIView *superview) {
    [_self willChangeValueForKey:@"superview"];
    originalImp(_self, @selector(willMoveToSuperview:), superview);
    [_self didChangeValueForKey:@"superview"];
};

IMP newImp = imp_implementationWithBlock((__bridge id)((__bridge void*)block));
method_setImplementation(method, newImp);

i haven't had any issues with this, but when i try to run it in 64-bit, i get

EXC_BAD_ACCESS (code=EXC_I386_GPFLT) on originalImp(_self, @selector(willMoveToSuperview:), superview);

anyone have any insight?

thanks

like image 476
Martin Avatar asked Jun 12 '26 11:06

Martin


1 Answers

It seems like using an imp_implementationWithBlock crashes on arm64.

You may try classic swizzling instead (put the code in your block in a dedicated swizzledWillMoveToSuperview: method, exchanging implementations of the two methods / selectors, and call [self swizzledWillModeToSuperview:superview] in swizzledWillModeToSuperview: to actually call the original implementation).

Avoiding using imp_implementationWithBlock and using an implementation of an existing method instead seems to work for both 32 and 64 bit architectures.

Be aware that this won't cover all the cases anyway, especially if someone subclasses an UIView and override willMoveToSuperview without calling super, then those implementations won't be swizzled as you expect and the observation on these won't work.

like image 166
AliSoftware Avatar answered Jun 14 '26 04:06

AliSoftware



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!