1

I'm developing an app targetting iOS6/7, and I've lost two hours staring at a storyboard, trying to understand why autolayout doesn't do what I want it to do.

Basically, I have a scene containing a scroll view and in it, I want to have a UIIMage anchored at the bottom right. Therefore, I set four constraints for the image:

  • Width equals
  • Height equals
  • Bottom space to superview and
  • Trailing space to superview

XCode does not complain about the positioning, so I run my app with confidence, only to find that it is not shown in any orientation. It's just nowhere to be found!

I know that to find how autolayout implemented the constraints and did its magic, I have to inspect the view.bounds rect. I checked at the viewDidAppear event, to find its value to be as expected though:

Image pos is: 0.000000,0.000000 106.000000x103.000000

The frame is of course at the actual position in the storyboard which I guess is to be expected.

Here is a screen cap of my Storyboard: Twilight zone or plain dizzy developer error?

Any ideas?

Update: Some more info: If I remove all constraints and run the app, the image view is shown at the bottom right of my view in portrait, but when rotating, as expected, it is not shown.

Update 2: This all should fall in the dreaded UIScrollView and AutoLayout threads. In the end I reverted to using a UIView, inside of which is a UIScrollView containing all the content I wanted to scroll (so that no text fields are hidden by the keyboard in landscape mode). The image I wanted anchored at the bottom was left at the container UIView and it all worked as intended.

Spyros P.
  • 296
  • 3
  • 14
  • Please log `view.frame` (rather than `view.bounds`) in `ViewDidAppear` as this is in the superview's coordinate system (so it will tell you where the view is). Also, have you set the `contentSize` of the scroll view? – Robotic Cat Mar 06 '14 at 19:00
  • When logging view.frame, it shows the initial size and position I defined in my storyboard, but, as far as I know, this is to be expected. You can't get the current position of a view when using AutoLayout by checking the view.frane. I haven't set the `contentSize` of the scroll view (I do set it when the keyboard is shown to move content upwards). However, since, as I added in my question, if I remove the constraints the image is shown at the bottom right in portrait mode, I don't think that this applies. – Spyros P. Mar 06 '14 at 19:28
  • Can you clarify for me if this view is meant to be within the scroll view or floating above it? – Robotic Cat Mar 06 '14 at 20:28
  • This answer (http://stackoverflow.com/questions/15773815/storyboard-uiscrollview-contentsize) suggests that overriding `viewDidLayoutSubviews` and setting `contentSize` there may work. – Robotic Cat Mar 06 '14 at 20:39
  • OK - I can easily get the view to float above the scroll view but if you want the view to within the scroll view then there is a catch when using a `UIScrollView` and Autolayout. See Apple's Technote on the topic (apologies - no time to write some example code): https://developer.apple.com/library/ios/technotes/tn2154/_index.html – Robotic Cat Mar 06 '14 at 21:43
  • Thanks for this info. I was this close to return to using AutoResizing masks and abandon AutoLayout! In the end, I just reverted to the idea of having the image view floating above the scroll view. Thanks for your time and showing me the correct approach. – Spyros P. Mar 07 '14 at 07:09

1 Answers1

0

If you want to anchor to bottom right, Programmatic autoresizingMask has been far more consistent for me.

It's slightly opposite of IB so if you want to anchor bottom right, you want the left margin flexible and the top margin flexible

yourView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin;

This means that bottom margin, right margin, height and width will all remain the same, keeping it in the lower right of your view.

Logan
  • 52,262
  • 20
  • 99
  • 128
  • I tried this, but still nothing got displayed. Are you sure that I can still use an autoresizing mask in an AutoLayout - based storyboard? – Spyros P. Mar 06 '14 at 17:37
  • Wait, so your imageView isn't appearing at all? – Logan Mar 06 '14 at 17:39
  • Nope. I can see it (it's the red rectangle on the screenshot) in my storyboard, it is not hidden, but I cannot see it! – Spyros P. Mar 06 '14 at 17:44
  • Ok, just to rule it out, is your storyboard 4 in (568 px) and your simulator or device 3.5 in (480px) – Logan Mar 06 '14 at 17:53
  • No, it's an iPad 768x1024 storyboard. – Spyros P. Mar 06 '14 at 18:04
  • If you launch this storyboard viewController without any other code does it appear? Have you NSLog'd the frame? Can I see more of the code? – Logan Mar 06 '14 at 18:07
  • Please check the added info in my question. Logging the frame shows the position and size defined in the Storyboard. What code would you like to see? I wouldn't know what to post. – Spyros P. Mar 06 '14 at 19:31
  • Is the parent view autoresizing properly? What is the parentV frame after rotation? – Logan Mar 06 '14 at 19:50
  • The parent view is correctly 768x1024 in portrait and 1024x768 in landscape. The problem, as I found, was related to the UIScrollView behavior when using AutoLayout. Changing my container view to a simple UIView made all my constraints work. Thanks for your time. – Spyros P. Mar 07 '14 at 07:07