I'm hardly learning to implement some iOS app screens in xCode Interface Builder with auto-layout.
I've already placed some elements with working constraints but I realize it would be more practical to group them into a vertical StackView.
At first it seems pretty easy as I did it for a previous screen. However, once I embed my elements in the StackView, all my margins constraints are gone ! Also, if I try to manually put back my constraints, they don't work at all and generate ambiguity.
I don't fully understand the way iOS UI builder works, so I'd gladly need some help.
See screenshots below.
Constraints inside stack views can be quirky, but playing around with / experimenting with the characteristics can help.
In particular, adjusting Alignment, Distribution, and Spacing can often do the trick.
Related
The main problem is that I tried command+drag to the view and selecting "align horizontally in the container", but for some reason(I was doing this on the iPhone 7) when I checked the other devices(such as the iPad Pro) they were not aligned. Then I tried adding constraints, which also did not work. My question is how in the world do you center a label without using Swift?
First thing first: "Center Horizontally in Container" is a constraint.
My initial guess is that you’ve neglected some sort of ambiguity or possibly created a constraint which conflicts with (and overrides) the centering constraint. If this is the case, you should see warnings telling you what the problem is.
As long as there's no vertical ambiguity, and no conflicting horizontal constraints (leading/trailing space), your label should be centered—no Swift code necessary.
I get the impression that you’re fairly new to Xcode, so if you don’t know already, you can check the existing constraints on an object in the Size Inspector, denoted by the ruler in the right sidebar. It’s quite possible that there’s an old constraint you forgot to remove, or one you added but don’t need.
Another thing to note: I’ve experienced some layout issues when switching devices in the new Interface Builder in Xcode 8—it’s just a bit buggy in my experience, especially with regard to Stack Views. If you have doubts about how something looks, I’d recommend running the app in Simulator for some devices you don’t have on hand to check it out.
I have 3 sections, Top Banner with Logo, Mapview in the middle and then a few labels under the MapView.
I have used Constraints for accomplishing this for 6s sizes. But when I try this out for 4s, 5, 5s the below section the Labels is not seen on the screen. In design view they are also only partially shown and seems to be somewhere hidden in 4 and 5 ios versions.
I have set height for the Top banner as 0.2 with the SuperView and MapView with 0.4 and trying to get the remaining labels to fill in the remaining 0.4 height of the total SuperView.
I have tried to contain all the labels in a Tableview also this is not even showing anything in Design time or even at runtime.
Another Approach is to Add a view in which place all the labels so that can add a contraint of 0.4 height for the view. But all in vain.
Layout Design :
http://imgur.com/aIjBJsq
I am an iOS and auto layout Noob so it maybe something silly that I might have missed. Appreciate any help or advise on the same
Ok, first it seems that you need a break down of the fundamentals of Auto Layout. Below are some resources that can help you with that. Working with Auto Layout can take quite a bit of effort so you really should get a solid understanding, otherwise you will constantly be running into problems.
Adaptive User Interfaces
Auto Layout Guide
View controllers and adaptivity and size changes
WWDC2012: Auto Layout by Example
WWDC 2015: Mysteries of Auto Layout, Part 1
WWDC 2015: Mysteries of Auto Layout, Part 2
Key to understanding your issues is the role layout constraints play in your design. You use them to make small to medium changes, for bigger changes such as different devices you need to use size classes as well as constraints. In Xcode 8's Interface Builder you have the option to set the device for your layout, see the picture below. It's in the bottom left-hand corner. The device's selection relates to size classes.
So how do we go about solving your issues? We would start by laying out the view for a selected device in interface builder, then we would select another device, say iPhone 4s and then make any adjustments to your view there. This is the basic outline and there are many steps in between. The single best step you can take at this point is to get a better understanding of Interface Builder. Learning how to use interface builder will touch on all the areas covered by the resources listed above.
Interface Builder
wwdc2015: Implementing UI Designs in Interface Builder
I understand this is a broad answer to your question, but I am trying to help you build an understanding of the concepts, so you can happily build your own layouts from here on out!
My problem is on this image:
I have this problem with Xcode 6 and I would like to know if there is a way to solve this problem programmatically... I would like to write a code which adapt my view controller to all devices... Is it possible??
(I already tried auto layout manually but I am really bad)...
Seems like you've implemented too strict constraints. As I see on screenshot, you've probably set a strong width constraint for your buttons and inputs instead of equal widths or equal spacing between each other. Make your constrains more flexible. Prefer >=,<= constraints, equal widths constraints, leading/trailing constraints. You will need more of them in comparison to strict ones, but you'll get a true adaptive layout.
To get flexible equal spacing between views, you might find helpful a conception of spacer views. In short, place a transparent UIView and set some constraints to it. To know more, read apple docs. If you like videos more (like I do :)) - watch wwdc videos, they are great. For example, Auto Layout by Example or Taking Control of Auto Layout in Xcode 5.
As an example of equally arranged buttons take a look at this screenshot:
This is done with a little bit complex, but understandable constraints set:
All the buttons has the equal widths constraints for each other. All spacer views has equal width constraints on each other. Each spacer view has leading/trailing constrains to buttons or parent view leadings/trailings.
With iOS 9 you have a few more options:
instead of spacer views you can use UILayoutGuide
UIStackView allows to achieve the same layout with no constraints at all
I suggest you to watch new WWDC 2015 videos, "Mysteries of Auto Layout",Part1 and Part2
Hi #Pedro Costa its not possible here to write solution of your above problem. I am giving one link give some movement to study it. its too simple to understand.
May this will give you the idea how to set it.
AutoLayout Constraints Programatically
http://www.thinkandbuild.it/learn-to-love-auto-layout-programmatically/
Home this will help.
I´m pretty new to Xcode and playing around with some techniques.
For testing, i am writing an app where i have a detailViewController with three views within.
As soon as i assign constraints to one of the views the layout gets really strange. No matter if i center horizontally or set border distance, the view seems somehow to vanish and the components within the view are placed somehow.
Any idea what i´m doing wrong?
Sure, you need to follow these basic rules. Checkout the auto layout guide. Each view from the top of the scene to the bottom should have constraints (also constraints should exist from side-to-side if you want expansion and contraction, or have height/width if those are important to maintain). Most times you will need to have at least four constraints per view. The auto layout errors will not go away until all the views have constraints applied and adhere to the simple rules I mentioned above.
Im starting to experiment with storyboard. As you can see I have chosen the storyboard size to be height=regular and width=compact, which says “For all iPhones in portrait”.
The simulator I am using is iPhone6 however when I run everything is slightly off to the right.
Can someone explain what is happening or what I am missing?
The problem is that (as described in the comments) you are positioning the views without AutoLayout. When you just drag and drop the views, it's actually setting the frame's positions and sizes. The main problem with this approach is that it doesn't set the position and size in a proportional and related manner, taking in consideration the container. This is why you're getting the view at the current position. If you run in another simulator, maybe you can get the correct position, or maybe not. What you can do to change this is apply auto layout constraints to those views. There's a special constraint to center views horizontally. With autoLayout you can go further, specifying relation between one or more views, and those views not necessarily need have the same container view.
One quick example:
Notes:
I'm using universal storyboard to take those screenshots. It's more flexible and with AL you don't necessarily have to concern yourself with the size, as views adjust themselves depending on the constraints applied to.
EDIT:
https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/Introduction/Introduction.html
This is a link to auto layout guide provided by apple, a great tutorial on how to get your hands dirty in this little world.