0

I have two custom views in my project which needs to be zoomed in the exact same proportion. So I decided to use UIScrollView for that and it fits perfectly.

I decided to develop a very simple class inherited from UIScrollView and inside of it I initialized all views structure. That way I'm avoiding any construction steps in the NIB file and my class can be used just by adding one view. But I faced an issue already on the stage of adding contentView.

Here's my class:

final class PlayerContentView: UIScrollView {
    fileprivate var contentView: UIView!

    override func awakeFromNib() {
        super.awakeFromNib()

        self.backgroundColor = .clear
        setupScrollProperties()

        setupContentView()
    }

    private func setupScrollProperties()
    {
        self.translatesAutoresizingMaskIntoConstraints = false
        self.minimumZoomScale = 1.0
        self.maximumZoomScale = 2.0
        self.contentSize = frame.size
        self.delegate = self
    }

    private func setupContentView()
    {
        contentView = UIView(frame: self.frame)
        contentView.backgroundColor = .red
        self.addSubview(contentView)

        CommonSwiftUtility.setSideConstraints(superview: self, view: contentView)
        CommonSwiftUtility.setSizeConstraints(superview: self, view: contentView)
    }


    func requireToFail(_ gestureRecognizer: UIPanGestureRecognizer) {
        self.panGestureRecognizer.require(toFail: gestureRecognizer)
    } }

And here're methods for adding constraints:

static func setSideConstraints(superview: UIView, view: UIView) {
        let topConstraint: NSLayoutConstraint = NSLayoutConstraint(item: view,
                                                                   attribute: .top,
                                                                   relatedBy: .equal,
                                                                   toItem: superview,
                                                                   attribute: .top,
                                                                   multiplier: 1.0, constant: 0.0)

        let bottomConstraint: NSLayoutConstraint = NSLayoutConstraint(item: view,
                                                                      attribute: .bottom,
                                                                      relatedBy: .equal,
                                                                      toItem: superview,
                                                                      attribute: .bottom,
                                                                      multiplier: 1.0, constant: 0.0)

        let leadingConstraint: NSLayoutConstraint = NSLayoutConstraint(item: view,
                                                                       attribute: .leading,
                                                                       relatedBy: .equal,
                                                                       toItem: superview,
                                                                       attribute: .leading,
                                                                       multiplier: 1.0, constant: 0.0)

        let trailingConstraint: NSLayoutConstraint = NSLayoutConstraint(item: view,
                                                                        attribute: .trailing,
                                                                        relatedBy: .equal,
                                                                        toItem: superview,
                                                                        attribute: .trailing,
                                                                        multiplier: 1.0, constant: 0.0)

        superview.addConstraint(topConstraint)
        superview.addConstraint(bottomConstraint)
        superview.addConstraint(leadingConstraint)
        superview.addConstraint(trailingConstraint)
    }

    static func setSizeConstraints(superview: UIView, view: UIView)
    {
        let wConstraint: NSLayoutConstraint = NSLayoutConstraint(item: view,
                                                                 attribute: .width,
                                                                 relatedBy: .equal,
                                                                 toItem: superview,
                                                                 attribute: .width,
                                                                 multiplier: 1.0, constant: 0.0)

        let hConstraint: NSLayoutConstraint = NSLayoutConstraint(item: view,
                                                                 attribute: .height,
                                                                 relatedBy: .equal,
                                                                 toItem: superview,
                                                                 attribute: .height,
                                                                 multiplier: 1.0, constant: 0.0)

        superview.addConstraint(wConstraint)
        superview.addConstraint(hConstraint)
    }

As you can see, I painted my contentView in red color in order to define it on the screen. After showing my PlayerContentView I get this:

enter image description here PlayerContentView is stretched on the full screen so I'm expecting contentView to be full-size, but clearly it's not. Could someone please refer me to the solution of that issue? Thanks in advance!

1 Answer 1

1

Can you set

contentView.translatesAutoresizingMaskIntoConstraints = false
Sign up to request clarification or add additional context in comments.

4 Comments

Hello and thank you for your response! As you can see in setupScrollProperties() method, I did it, but it didn't change anything :(
you set it for self not contentView
You are 100% correct, sir! Your solution did help, thank you very much! But one note: I need to set every single subview translatesAutoresizingMaskIntoConstraints to false in order to apply constraints.
Yes, you've to update this property to false in order to set constraints

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.