1

I am having a lot of trouble understanding the Autolayout and how it is working phenomenally for everyone else but me. The problem is, the constraints when they end up working for me for one resolution e.g. for iPad, fail to comply with another like iPhone4s or something, and consequently either this leads to conflicting constraints or not work at all the way I want them to (e.g the button will appear way near the text field on iPad and not very near on iPhone). I have read Raywenderlich's Adaptive Layout tutorial as well as Steven Lipton's book on Autolayout (Practical AutoLayout) and still facing a lot of trouble. Please help me out. Its so demotivating that I want give up coding altogether and end up becoming a monk or something... x(

EDIT

So to help you out further, I'll explain how somethings are not working for me with the help of some snapshots. My original idea was to show 4 views each containing a text field which would transition in through CoreAnimation on the press of a specific button. The view of ViewController has an image in the background, a back button, 4 views, a progress bar and a button to show each view. The problems are as follows; though the whole view seems appropriate on the simulator, I can see conflicts in the terminal of xcode.

image with all elements:

image with all elements

image with conflicts:

image with conflicts

But somehow if I resolve the conflicts, the autolayout doesn't function as required on all devices. e.g.

no conflicts but next button is hiding on tap of textfield in iPhone4s:

no conflicts but next button is hiding on tap of textfield in iPhone4s

where as in the case of iPad no conflicts and Next button is very much accessible:

no conflicts and Next button is very much accessible

How can I treat this to work on all devices the same and coherently. Please help out thanks.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Mohsin Khubaib Ahmed
  • 1,008
  • 16
  • 32
  • 2
    Don't feel too bad, we all have those moments. Can you add a screenshot of your storyboard (with the constraints values if possible)? – Islam Dec 09 '15 at 12:07
  • The problem is as a whole I can't get my head around it. I need to get the understanding as to when the alignment thing needs to be configured, when the pinning menu should be used, when to set the constraints automatically and so many ambiguities like these. However I will attach the snapshots to help you with what I am trying to achieve. Thank you for reaching back to me though.. – Mohsin Khubaib Ahmed Dec 09 '15 at 12:28
  • Please create a **minimal** example outlining your issue. Then list exactly all the views and constraints you have, copy and paste (rather than insert a screenshot) any error logs, show us what exactly you're expecting and what exactly you're getting (on several devices if needed). – jcaron Dec 09 '15 at 13:57
  • If there are important differences between the layouts you want on various screen sizes, then you can use size classes to either enable/disable specific constraints in various cases, or change values associated with them. However, in many cases, it's perfectly possible to get a perfect layout on all devices without the use of size classes. – jcaron Dec 09 '15 at 13:59

3 Answers3

1

One thing to understand is that AutoLayout won't solve all your problems for screen sizes that differ a lot (and I still have the impression that the size classes are cumbersome to use). I normally stick with two different xibs for phone and tablet, connected to the same view controller. And if you can, drop iOS 7 support, that removes a lot of headaches concerning AutoLayout.

EDIT

By all means , DO USE auto layout. What I said is just that often it's better to have two different xibs for greatly different resolutions, but of course use auto layout in both of them.

TheEye
  • 9,280
  • 2
  • 42
  • 58
  • The separate xibs is certainly one solution, I have had the pleasure of implementing those for sure, but we're progressing through a whole new level of development nowadays and I want to grow my spectrum in the AutoLayout now, and actually understand what Apple claims, as AutoLayout is a perfect solution for multiple devices. Thank you for your sincere and helpful words but I guess I really really want to learn AutoLayout heh :) – Mohsin Khubaib Ahmed Dec 09 '15 at 12:32
  • 2
    Don't get me wrong, you SHOULD use auto layout - it's working really well nowadays. It just doesn't solve everything ... – TheEye Dec 09 '15 at 12:42
  • Auto layout is best thing in Xcode. Do not mislead if you do not know/ have idea about things. – Saqib Omer Dec 09 '15 at 13:25
  • ?!?!?! I' m not misleading at all, I love auto layout (except for some performance issues, that is ...) – TheEye Dec 09 '15 at 13:26
1

Few things which I want to share from my experience about Auto-layout.

  1. If you want an element to appear at a particular position (20 points from right side and 40 points from top) you need to adjust button in storyboard and add trailing and top constraints. Xcode will give you suggestion to fix height/width of element. Resize element or add height and width constraints if you want exactly same height width as in storyboard.
  2. If you want en element to expand/ contract according to height width of different screen size add -leading, -trailing, -top, and bottom constraints.
  3. Important: When we "pin" elements (e.g button) (top, bottom, leading, trailing) these constraints are added relative to other elements (e.g some other button). In this case make sure other elements have constraints. Otherwise Xcode will give error of missing constraints.
  4. Do not add missing constraints as Xcode is suggesting because Xcode will add some constraints which will result in unexpected sizes.

Now Regarding your problem: When keyboard appears either you can move your view up (Relative to height of keyboard. This answer will help you if you want to achieve this without changing constraints programmatically. Otherwise if you have correctly added constraints than create and IBOutlet of top constraint of top most element and subtract a constant of keyboard height (150) from that constraints's constant. And also you need to add keyboard height (150) in that constraint's constant when keyboard disappears.

Community
  • 1
  • 1
Saqib Omer
  • 5,387
  • 7
  • 50
  • 71
0

The issue you have with the next button being hidden by the keyboard is not directly related to auto-layout, though auto-layout can help managing it. The issue is simply that you are using more space on screen than is available.

There are several ways to solve this issue:

  • you can embed your content in a scrollview, so users can scroll the screen up and down if the contents of the scroll view exceed the available height. Auto-layout will help here: you'll set a constraint from the bottom of the scroll view to the bottom layout guide, add an outlet to that constraint, and in your code you can observe keyboard hide/show/change notifications and adjust the constant of that constraint accordingly

  • you adjust your layout so that everything is always visible, even with the keyboard up, without changing the position of anything whether the keyboard is up or not. That means you don't use any of the space where the keyboard will show up

  • you adjust your layout dynamically based on the presence of the keyboard. This will combine a constraint like the one in the first option with other constraints to move things around when that one moves.

Before you do anything, think about how you want things to look like in the different scenarios. Then build the constraints that will give this result. Auto-layout cannot guess for you what you want to do, you need to have a precise idea of the layout you want.

jcaron
  • 17,302
  • 6
  • 32
  • 46