LinkedInAttic / LayoutKit

LayoutKit is a fast view layout library for iOS, macOS, and tvOS.
http://layoutkit.org
Apache License 2.0
3.16k stars 267 forks source link

UIVisualEffectView not supported #172

Open jiachenmu opened 7 years ago

jiachenmu commented 7 years ago

like the kind of View.contenView eg:

class BlurLayout: SizeLayout< UIVisualEffectView > {
   ..........
}

it will crash:

extension View {

    /**
     Similar to `addSubview()` except if `maintainCoordinates` is true, then the view's frame
     will be adjusted so that its absolute position on the screen does not change.
     */
    fileprivate func addSubview(_ view: View, maintainCoordinates: Bool) {
        if maintainCoordinates {
            let frame = view.convertToAbsoluteCoordinates(view.frame)
            addSubview(view)
            view.frame = view.convertFromAbsoluteCoordinates(frame)
        } else {
            addSubview(view)
        }
    }
}

Is there some solutions? thanks.. 😬

jiachenmu commented 7 years ago

In LayoutKitDemo, i found this:

open class CircleImagePileView: UIView {

    open override func addSubview(_ view: UIView) {
        // Make sure views are inserted below existing views so that the first image in the face pile is on top.
        if let lastSubview = subviews.last {
            insertSubview(view, belowSubview: lastSubview)
        } else {
            super.addSubview(view)
        }
    }
}

override UIVisualEffectView.addSubview, is a solution?

jiachenmu commented 7 years ago

What I guess above is a solution, here's an example:

class RootCardLayout: SizeLayout<VisualEffectView> {
    init(_ title: String, subtitle: String, info: String, size: CGSize, action: @escaping (() -> ())) {

        let titleLayout = LabelLayout.init(text: title, font: UIFont.cic.systemFont, numberOfLines: 1, viewReuseId: "title") { (label) in

        }

        let subtitleLayout = LabelLayout.init(text: subtitle, font: UIFont.boldSystemFont(ofSize: 20.0), numberOfLines: 0, viewReuseId: "subtitle") { (label) in

        }

        let infoHeight = info.cicHeight(size.width, font: UIFont.cic.preferred(.body))
        let infoLayout = SizeLayout<CICLabel>.init(height: infoHeight) { (label) in
            label.line(0)
                .text(info)
                .font(UIFont.preferredFont(forTextStyle: .body))
                .sizeTo(layout: .maxWidth(.screenWidth))
                .textColor(CIComponentKitThemeCurrentConfig.textColor)
                .longPressAction(.copy)
                .copyRange(NSMakeRange(0, 5))
            label.copySuccessClousure = {
                let tips = """
                            嫉妒使我高斯模糊

                            嫉妒使我氧化分解

                            嫉妒使我增减反同

                            嫉妒使我奇变偶不变符号看象限

                            嫉妒使我基因突变

                            嫉妒使我质壁分离

                            嫉妒使我泰拳警告

                            嫉妒使我弥散性血管内凝血
                           """
                CICHUD.showAlert("羡慕使我嫉妒", content: tips)
                CICHUD.showNotifier(title: "爱酱今天要元气满满哦~")
            }
        }

        let stackLayout = StackLayout<UIControl>.init(axis: .vertical,
                                                      spacing: 30,
                                                      sublayouts: [titleLayout, subtitleLayout, infoLayout]) { (control) in
                                                        control.addHandler(for: .touchUpInside, handler: { (_) in
                                                            action()
                                                        })
        }

        super.init(minWidth: size.width,
                   maxWidth: size.width,
                   maxHeight: size.height,
                   alignment: .center,
                   sublayout: InsetLayout.init(insets: UIEdgeInsetsMake(5, 5, 5, 5), sublayout: stackLayout)) { (view) in
            view.layer.cornerRadius = 6.0
            view.layer.masksToBounds = true
            view.layer.shadowColor = UIColor.white.cgColor
            view.effect = UIBlurEffect.init(style: .extraLight)
        }
    }
}

class VisualEffectView: UIVisualEffectView {
    override func addSubview(_ view: UIView) {
        print(view.classForCoder)
        if view is UIControl {
            self.contentView.addSubview(view)
        }else {
            super.addSubview(view)
        }
    }
}

override this method ,addSubview, but may not be the best solution (-_-#!)