2

I have the following view with the following constraints:

@IBOutlet weak var square1ViewOutlet: UIView!

enter image description here

Which is inserted inside the following view using the storyboard:

@IBOutlet weak var holderView: UIView!

enter image description here

My problem is that I am not being able to override the positioning of square1ViewOutlet established by the storyboard. The following code does not have any effect. I've tried some variations as bellow, but nothing works and the view is really stuck to previous storyboard constraints. I am calling this with my viewDidLoad method.

    square1ViewOutlet.frame.origin.y = self.holderView.frame.origin.y + 20
    square1ViewOutlet.frame.origin.x = self.holderView.frame.origin.x + 20
    square1ViewOutlet.frame = CGRectOffset(CGRect(origin: square1ViewOutlet.frame.origin, size: square1ViewOutlet.frame.size), 20, 20)

    square1ViewOutlet.center = CGPointMake(150, 150)
    square1ViewOutlet.frame = CGRectMake( 100, 200, square1ViewOutlet.frame.size.width, square1ViewOutlet.frame.size.height )

Any idea what am I doing wrong here?

GuiSoySauce
  • 1,763
  • 3
  • 24
  • 37
  • 1
    Update your constraints not your view's frame. See here: http://stackoverflow.com/questions/23655096/change-frame-programmatically-with-auto-layout – beyowulf May 20 '16 at 00:44

2 Answers2

1

When you use autolayout and you try to change frame dimensions or positions like your code, instead to correct the correct constraints involved, you can disable you constraints effect causing warnings and unexpected view dimensions and movements.

The correct way to do it is to link the constraints as IBOutlets in your code:

@IBOutlet weak var customViewTopConstraint: NSLayoutConstraint
@IBOutlet weak var customViewHeightConstraint: NSLayoutConstraint
@IBOutlet weak var customViewLeadingConstraint: NSLayoutConstraint
@IBOutlet weak var customViewWidthConstraint: NSLayoutConstraint

And work with these properties changing it's constant value (.constant). These rules are valid for all the code where you have to change frame dimensions, so check all your code and change it to a constraints work.

Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133
0

Can you please try that code inside your viewDidAppear() method. please go to the link below iOS UIViewController Lifecycle.

Since you have placed your code inside viewDidLoad method, that may be the reason your frames are not applied.

If this not worked please try layoutIfNeeded method for your view after the frames are set. layoutIfNeeded forces the receiver to layout its subviews immediately if required.

By this way you can set your frames for your views. But doing this way will alter your constraints that you had set in your xib file. If you really want to change your view frames, then best way will be create outlet for your constraints and change the values for that constraints. Hope this will help :)

Aneesh
  • 107
  • 7
  • Thanks for the answer, it works now on viewDidAppear and on other methods. You were right about the life cycle and I was doing wrong trying to get it to work on viewdidload or viewwillappear. – GuiSoySauce May 20 '16 at 23:51