Tricertops/KeepLayout

Removal of KeepAttributes

Closed this issue · 2 comments

I encountered an issue with removing KeepAttributes after attempting to change the view hierarchy. To be specific, I changed the superview of one view and had to update keepInsets. Using the remove methods of the library did nothing because the KeepAttributes don't update the superview after they're cached in the views during initialization.

Here's a method I wrote in UIView+KeepLayout.m that fixes my issue:

- (void)removeKeepAttributeForSelector:(SEL)selector {
    objc_setAssociatedObject(self, selector, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)removeKeepInsets {
    [self removeKeepAttributeForSelector:@selector(keepTopInset)];
    [self removeKeepAttributeForSelector:@selector(keepBottomInset)];
    [self removeKeepAttributeForSelector:@selector(keepLeftInset)];
    [self removeKeepAttributeForSelector:@selector(keepRightInset)];
}

I'm not familiar with objc/runtime.h capabilities, so maybe there's a more general way to write a method to remove each of the other attributes? Failing that, I can manually write a remove counterpart method manually for each one and submit a pull request if you like.

You are right, this needs a fix. Your solution is correct, however I will think about some automatic solution.

Maybe the best option would to override/swizzle -didMoveToSuperview: and re-apply all superview-related attributes.

This is already fixed for quite some time. 237c062