1

I'm getting very confused with traits and constraints.

I have a view that needs to be in a 1:1 ratio, and it needs to have it's size dependent on the larger of either the screen width or screen height, at runtime, and with all device orientations.

On top of that, I need to move it down from the centre of the screen, by an amount that is calculated at runtime depending on orientation and iPhone vs iPad.

Is this possible or is it better to do all this in code and remove all AutoLayout constraints from this view?

I've read this: Aspect ratio constraint relative to the screen width

And also this: https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/AutolayoutPG/Size-ClassSpecificLayout.html

But I still can't seem to get it to do what I want.

Can someone please clarify what I need to do?

Thanks

Elphaba
  • 89
  • 1
  • 9
  • 2
    Set the height to be equal to width and change the height constraint's constant to the min(height, width) in `layoutSubviews()`. – Rakesha Shastri Aug 27 '19 at 05:04
  • That's a great idea, but do you mean max(height, width) as it needs to be made to size up to the bigger of the two? – Elphaba Aug 27 '19 at 05:21
  • Wouldn't the view be outside the screen then? – Rakesha Shastri Aug 27 '19 at 05:22
  • parts of it yes, but that's what I want. – Elphaba Aug 27 '19 at 05:27
  • @RakeshaShastri If you want to post your suggestion as an answer I can upvote it (if it works - coding now) – Elphaba Aug 27 '19 at 05:28
  • @RakeshaShastri How do I alter the Y value? In IB the centre of this larger view is centred to the superview. But at runtime in layout subviews, I need to move the centre.y down by an amount... and this changes on orientation too. I tried this: ```swift self.sceneKitViewWidthConstraint = NSLayoutConstraint(item: self.sceneKitView!, attribute: .centerY, relatedBy: .equal, toItem: nil, attribute: .centerY, multiplier: 0.0, constant: viewCentre.y)``` where 'viewCentre' is the newly calculated point... but it crashes the app – Elphaba Aug 27 '19 at 05:43
  • 1
    You should create the constraint once and then change the constant of that constraint when needed. – Rakesha Shastri Aug 27 '19 at 06:01
  • Do you think it's possible to find a way to do what you want by only using autolayout constraints at the storyboard? For example you can set the aspect ratio to 1:1, along center Y with an offset, and Leading space to one side of the screen with the desired margin. The size will change automatically when you turn the device. – L33MUR Aug 27 '19 at 07:52
  • One more thing, it would be helpful if you show your idea with a picture or something ;) – L33MUR Aug 27 '19 at 07:53
  • Check out this question and the accepted answer https://stackoverflow.com/q/30439214/1630618 – vacawama Aug 27 '19 at 09:11

1 Answers1

1

You need to add at least these 2 constraints to your UIView in storyboard. 1st will be for the aspect ration and the 2nd will be for the width of the UIView with a constant of a value. You will drug an outlet of the 2nd contraint to your UIViewController class named widthConstraint On the delegate function ViewWillAppear() you will add this code that computes the max side and it adds that value to the contraint constant.

UIView.animate(withDuration: 0.0) {
        let largestOfScreenSide = max(UIScreen.main.bounds.width, UIScreen.main.bounds.height)
        self.widthConstraint.constant = largestOfScreenSide
        self.view.layoutIfNeeded()
    }
Vasilis D.
  • 1,416
  • 13
  • 21