一个兼容MacOS和iOS的XIB桥接库(LYNibBridge)

前言

在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;
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AGI阅读 16,018评论 3 119
  • 大王wxd阅读 616评论 4 7
  • 春节的脚步还未走远之际,节日的喜庆还洋溢在每个人的脸上之时,前旗四中人迎来了校本培训的如期举行。 2018年2...
    王小唐阅读 142评论 0 0
  • “千年的文字会说话”,听过这句话吗?今天无意间看到,前不久撕得热闹的郭师傅说的。 学会用文字记录自己的所思所想,正...
    happy小晶阅读 287评论 0 1
  • 对恋爱充满了美好的期待 同时也害怕恋爱所带来的期骗 以及与感情洁癖相违背的事情 和最后感情逐渐变淡走向衰落的过程 ...
    寒潭白鱼阅读 132评论 0 0