前言
在iOS開發時,關于XIB橋接,有一個孫源大神開源的庫:XXNibBridge,具體原理就是運行時替換了系統的方法,攔截要橋接的視圖,替換為xib加載的視圖,就像這樣:
效果圖.png
自定義視圖.png
storyboard中我們看到的.png
由于需要搞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;
}