0

I have a UIView subclass (to deal with drag and drop) and with Interface Build I added two subviews: an UIImageView and an UILabel.

Both have constraints:

UIImageView border match the superview borders in order to fill all the available space. UILabel is centered vertical and horizontally.

Here you can see the three (superview background is green).

enter image description here

My problem comes when I animate a frame change in the superview. The superview seems fine, but its subviews go crazy.

Here you can see it (ignore the brown square, is from another view)

enter image description here

As you can see, subviews "fall" from the superview, despite I call updateConstraints on every subview.

I know it is a bad practice to mix AutoLayout with setFrame, but is the only way I know to do it.

Thanks

Mihir Oza
  • 2,768
  • 3
  • 35
  • 61
WedgeSparda
  • 1,161
  • 1
  • 15
  • 40
  • Why would you not remove the superview and set the background of the UIImageView to green? Then use the correct contentmode to size the image correctly, see this question: http://stackoverflow.com/questions/4895272/difference-between-uiviewcontentmodescaleaspectfit-and-uiviewcontentmodescaletof – Black Magic Oct 07 '15 at 12:42
  • I tried, but then I couldn't set the UILabel as a subview os the UIImageView. Also, the green is just temporary. – WedgeSparda Oct 07 '15 at 12:47
  • 1
    Is it possible for you to include your source code? The behavior you are describing is what's expected to happen. A few things to consider: Are you getting any warnings at runtime in the console? If constraints can not be resolved, some will be destroyed and you'll be alerted in the log. `-updateConstraints` should not need to be called on the subviews unless you're actually modifying them. That does not sound necessary in your case. How are you handling positioning the view being dragged? If the view is positioned with constraints but then you modify the frame, unexpected behavior could occur – Anthony Mattox Oct 07 '15 at 13:01
  • It's possible to use auto layout to animate a frame change: http://stackoverflow.com/questions/13296232/ Is there any reason you can't do that for the superview? – jscs Oct 07 '15 at 19:09

1 Answers1

0

Thanks to the comments I made some changes.

1) Change the superview (the green one) with the UIImageView.

2) Remove the UILabel and use a CATextLayer to place the text, using this answer to center the text vertically in front of the UIImageView.

And now it works fine. Thanks for the help.

Community
  • 1
  • 1
WedgeSparda
  • 1,161
  • 1
  • 15
  • 40