Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UIView with programmatic layout #139

Closed
mhillebrand opened this issue Apr 27, 2019 · 4 comments
Closed

UIView with programmatic layout #139

mhillebrand opened this issue Apr 27, 2019 · 4 comments

Comments

@mhillebrand
Copy link

I'd like to use Injection with my UIView subclasses that contain programmatic layout. That is, I've created Auto Layout constraints using Swift code within UIViews...not within UIViewControllers. Injection doesn't seem to help in this scenario, or is there something I'm missing?

Thanks!

@mhillebrand
Copy link
Author

mhillebrand commented Apr 27, 2019

I created a workaround, but I'm not sure I like it very much. It requires each UIView subclass to override the addConstraints() method, and what I've done with subview constraints feels awkward:

@objc extension UIView {
    #if DEBUG
    @objc func injected() {
        removeSubviewConstraints()
        addConstraints()
    }
    #endif

    func addConstraints() {}

    func removeSubviewConstraints() {
        subviews.forEach { $0.removeAllConstraints() }
    }

    func removeAllConstraints() {
        var superview = self.superview

        while superview != nil {
            for constraint in superview!.constraints {
                if constraint.firstItem as? UIView == self || constraint.secondItem as? UIView == self {
                    constraint.isActive = false
                }
            }

            superview = superview?.superview
        }

        constraints.forEach { $0.isActive = false }
    }
}

@mhillebrand
Copy link
Author

mhillebrand commented Apr 27, 2019

Bah. This is no good. I still can't see programmatic updates to UIViews that are NOT related to layout constraints. I guess I could just stop using anonymous closures to configure my subview properties and do all of that within a method that gets called both on init and on injection...

@Polisas
Copy link

Polisas commented May 9, 2019

Did you fix it ? Or got a workaround ?

@mhillebrand1
Copy link

mhillebrand1 commented May 9, 2019

Yeah, I mentioned my workaround above:

I guess I could just stop using anonymous closures to configure my subview properties and do all of that within a method that gets called both on init and on injection.

Here's an example:

import TinyConstraints

class InjectableView: UIView {
    private let subview1 = UIView()
    private let subview2 = UIView()

    init() {
        super.init(frame: CGRect.zero)
        configure()
        addSubviews()
        addConstraints()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func configure() {
        subview1.backgroundColor = .green
        subview2.backgroundColor = .yellow
    }

    private func addSubviews() {
        addSubview(subview1)
        addSubview(subview2)
    }

    override func addConstraints() {
        subview1.edgesToSuperview(excluding: .bottom)
        subview1.height(300)

        subview2.edgesToSuperview(excluding: .top)
        subview2.topToBottom(of: subview1)
    }
}

...and then I added a call to configure() in my UIView+Extension.swift file:

    @objc func injected() {
        configure()
        removeSubviewConstraints()
        addConstraints()
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants