前言
在iOS开发时,关于XIB桥接,有一个孙源大神开源的库:XXNibBridge,具体原理就是运行时替换了系统的方法,拦截要桥接的视图,替换为xib加载的视图,就像这样:
由于需要搞MacOS开发,就突发奇想是不是可以把这个用在Mac开发上,在实践中稍微踩了一点坑,最终实现了Mac上与iOS通用的库:LYNibBridge,有需要的同学可以直接拿走使用,实现原理与iOS端一样,想了解的可以查看阳神的博客:xib的动态桥接
坑点
在Mac上实现时遇到了一个问题,采用跟iOS一样的代码运行起来查看不到实际的效果,同时打印台会提示错误:
Failed to set (contentViewController) user defined inspected property on (NSWindow): <LYBridgeTestView 0x604000121a40> has reached dealloc but still has a super view. Super views strongly reference their children, so this is being over-released, or has been over-released in the past
大致意思就是子视图已经释放,但是父视图还持有这个子视图对象
解决方案
方案1.
在创建完自定义的XIB试图后调用[placeholderView removeFromSuperview];
代码大致如下:
+ (UIView *)instantiateRealViewFromPlaceholder:(UIView *)placeholderView {
// Required to conform `XXNibConvension`.
UINibBridgeView *realView = [[placeholderView class] ly_instantiateFromNib];
realView.frame = placeholderView.frame;
realView.bounds = placeholderView.bounds;
realView.hidden = placeholderView.hidden;
realView.autoresizingMask = placeholderView.autoresizingMask;
realView.autoresizesSubviews = placeholderView.autoresizesSubviews;
realView.translatesAutoresizingMaskIntoConstraints = placeholderView.translatesAutoresizingMaskIntoConstraints;
#if TARGET_OS_OSX
realView.focusRingType = placeholderView.focusRingType;
realView.canDrawConcurrently = placeholderView.canDrawConcurrently;
realView.accessibilityEnabled = placeholderView.accessibilityEnabled;
realView.appearance = placeholderView.appearance;
#else
realView.tag = placeholderView.tag;
realView.clipsToBounds = placeholderView.clipsToBounds;
realView.userInteractionEnabled = placeholderView.userInteractionEnabled;
#endif
// Copy autolayout constrains.
if (placeholderView.constraints.count > 0) {
// We only need to copy "self" constraints (like width/height constraints)
// from placeholder to real view
for (NSLayoutConstraint *constraint in placeholderView.constraints) {
NSLayoutConstraint* newConstraint;
// "Height" or "Width" constraint
// "self" as its first item, no second item
if (!constraint.secondItem) {
newConstraint =
[NSLayoutConstraint constraintWithItem:realView
attribute:constraint.firstAttribute
relatedBy:constraint.relation
toItem:nil
attribute:constraint.secondAttribute
multiplier:constraint.multiplier
constant:constraint.constant];
}
// "Aspect ratio" constraint
// "self" as its first AND second item
else if ([constraint.firstItem isEqual:constraint.secondItem]) {
newConstraint =
[NSLayoutConstraint constraintWithItem:realView
attribute:constraint.firstAttribute
relatedBy:constraint.relation
toItem:realView
attribute:constraint.secondAttribute
multiplier:constraint.multiplier
constant:constraint.constant];
}
// Copy properties to new constraint
if (newConstraint) {
newConstraint.shouldBeArchived = constraint.shouldBeArchived;
newConstraint.priority = constraint.priority;
newConstraint.identifier = constraint.identifier;
[realView addConstraint:newConstraint];
}
}
}
#if TARGET_OS_OSX
// problem exists use stackView as superview,you can embed it in an empty view
if ([[placeholderView superview] isKindOfClass:[NSStackView class]]) {
NSAssert(NO, @"can't use stackView as it's superview");
} else {
[placeholderView removeFromSuperview];
}
#endif
return realView;
}
存在的问题:
在MacOS上无法使用OSStackView,使用OSStackView嵌套自定义XIB视图,在这里尝试调用[placeholderView removeFromSuperview];会提示EXC_BAD_ACCESS崩溃
方案2.
给NSView增加一个属性,使用创建完成的XIB桥接视图持有这个placeholderView,这样一来placeholderView就不会被释放掉,同时支持使用OSStackView,完美解决了痛点!
+ (UIView *)instantiateRealViewFromPlaceholder:(UIView *)placeholderView {
// Required to conform `XXNibConvension`.
UINibBridgeView *realView = [[placeholderView class] ly_instantiateFromNib];
realView.frame = placeholderView.frame;
realView.bounds = placeholderView.bounds;
realView.hidden = placeholderView.hidden;
realView.autoresizingMask = placeholderView.autoresizingMask;
realView.autoresizesSubviews = placeholderView.autoresizesSubviews;
realView.translatesAutoresizingMaskIntoConstraints = placeholderView.translatesAutoresizingMaskIntoConstraints;
#if TARGET_OS_OSX
realView.focusRingType = placeholderView.focusRingType;
realView.canDrawConcurrently = placeholderView.canDrawConcurrently;
realView.accessibilityEnabled = placeholderView.accessibilityEnabled;
realView.appearance = placeholderView.appearance;
realView.ly_placeholderView = placeholderView;
#else
realView.tag = placeholderView.tag;
realView.clipsToBounds = placeholderView.clipsToBounds;
realView.userInteractionEnabled = placeholderView.userInteractionEnabled;
#endif
// Copy autolayout constrains.
if (placeholderView.constraints.count > 0) {
// We only need to copy "self" constraints (like width/height constraints)
// from placeholder to real view
for (NSLayoutConstraint *constraint in placeholderView.constraints) {
NSLayoutConstraint* newConstraint;
// "Height" or "Width" constraint
// "self" as its first item, no second item
if (!constraint.secondItem) {
newConstraint =
[NSLayoutConstraint constraintWithItem:realView
attribute:constraint.firstAttribute
relatedBy:constraint.relation
toItem:nil
attribute:constraint.secondAttribute
multiplier:constraint.multiplier
constant:constraint.constant];
}
// "Aspect ratio" constraint
// "self" as its first AND second item
else if ([constraint.firstItem isEqual:constraint.secondItem]) {
newConstraint =
[NSLayoutConstraint constraintWithItem:realView
attribute:constraint.firstAttribute
relatedBy:constraint.relation
toItem:realView
attribute:constraint.secondAttribute
multiplier:constraint.multiplier
constant:constraint.constant];
}
// Copy properties to new constraint
if (newConstraint) {
newConstraint.shouldBeArchived = constraint.shouldBeArchived;
newConstraint.priority = constraint.priority;
newConstraint.identifier = constraint.identifier;
[realView addConstraint:newConstraint];
}
}
}
return realView;
}