UIStackview weird behaviour with size classes - ios

I'm trying to recreate the iOS stock calculator app using UIStackView in iOS 9.2 with Xcode 7.2.
I succesfully created the portrait version, now I was working with Size Classes to implement the landscape one.
The approach I used to create the layout it was to have:
a UILabel for the display, which has leading,trailing and top constraints from the superview
a UIStackview as container just below, which has leading, trailing and bottom constraints from the superview and the top from the UILabel. This Stackview stacks its components vertically with "distribution":"fill equally" and "alignment":"fill"
inside this UIStackview, one UIStackview for each row of buttons. These stackviews stack their components horizontally with "distribution":"fill equally", "alignment":"fill" and "spacing":"1" (to recreate 1pt black border)
As you can see from the screenshot here (Storyboard Screenshot), the result I got on the storyboard it's perfect: all seem to working fine and Xcode don't give any error neither warning!
However, when I compile, the result on the simulator (Simulator and Debug screenshot) it's completely different.
The buttons, in particular the ones present only in landscape, seem to go outside the stackview for some reason (see the screenshot above for the View Debug).
Anyone have experienced this problem? It's really really weird because I think the logic behind the layout it's ok.
Thanks

Related

UIStackView with UILabels in Xcode 7.3

Since I've updated to Xcode 7.3 I have a problem with UIStackView. If I create an empty project and just put two UILabels in UIStackView with default hugging and compression priorities, it shows me AutoLayout misplacement error. It even doesn't work with one(!) UILabel in UIStackView, so priorities doesn't matter (as I think, maybe there is the problem).
As I remember in Xcode prior 7.3 everything was fine. I checked on two MacBook Pro separately, it's the same issue.
Does anybody know how to fix it?
This is a bug appears in Xcode 7.3, so you can ignore it as what he said matt.
The problem
The storyboard can not calculate the intrinsic content size of subviews when you use UIStackView
Solution
If you hate seeing warnings in your storyboard you can fixe it like below :
You should do these steps for each subview of your UIStackView
Tap the warning of your subview
Tap "Update frames" option
Choose "Placeholder" of the subview intrinsic size in the fourth menu in the
inspector view
Like that you will not see warnings, and you can see if any ambiguities exist
The disadvantage is you should do this steps each time you change your subview (example: changing the size of text of an UILabel)
Hoping that Apple fixes this bug in the next release
Ignore the warnings. The storyboard has never quite understood stack views. The constraints will be correct at runtime and that's all that matters.
I have a different trick for eliminating the warnings and making 'update frames' work properly.
Advantage:
Doesn't require changing placeholder values every time text changes
Disadvantage:
Extra views that are unnecessary when Xcode 8 comes out
Steps:
Embed each UILabel inside a UIView.
Constrain the UIView to size itself based on the label (match leading, trailing, bottom, and top)
It seems like UIStackView (in Xcode 7) can't understand the intrinsic content size of a label, but CAN understand the intrinsic content size of a view - so put the label in a view and presto!

UIScrollView disappears when adding constraints

I have a UIScrollView that contains everything else in my ViewController. I use it because I have logic to scroll up the view when the keyboard shows.
I'm working to make my view compatible with variable screen sizes to accommodate newer iPhones. Currently there are no constraints on anything in the ViewController. If I open the app on a device, it appears in the top left corner.
The first thing I tried was pinning the left/right sides of the UIScrollView to the edges of the container, with left/right distances of 0. When I fired it up on a device, the view had disappeared entirely.
I then tried adding a Horizontal center in container constraint the scrollview (removing the other constraints). Again, just a blank screen - everything had disappeared.
What am I doing wrong? How do I make my scrollview fill the screen, or at least be centered on wider devices?
Is there any way to debug layout issues like this? At the moment, I have no insight into what has happened to the view.
Adding height/width constraints, even though these constraints were overridden to stretch the view to the viewport, fixed this problem.

UIView Doesn't allow swipe down to see rest of the screen

I build a view that has contents which exceed the height of the iphone 6 screen vertically.
To test that out, I've set a specific height for my image (600pts) to guarantee that the whole content wouldn't fit on the screen of an iphone 6 and that you should swipe down to see the rest (the label "BYE").
However, when I run the simulator, I cannot swipe down to see the rest of the screen.
I've used Autolayout constraint and set a margin top/bottom between each components and the superview (I don't have any autolayout constraints issues/warning).
Here's the screenshot of the storyboard of my UIViewController:
Here's when I run the simulator, I cannot see the Label "BYE"(which is normal) but I cannot swipe down to see it.
I am using Xcode 6.1 and Swift.
Any suggestion and explanations?
Thanks a lot!
I have redone your screen and here are the steps. The reason for your issue is not setting proper constraints in autolayout. First you need to choose AnyWidth AnyHeight in Xcode which is in bottom of the storyboard.
Here is the screenshots of View in StoryBoard and its constraints
Constraints
Final Result in Simulator
Setting the constraint properly will work for you. Let me know if you have any issues in doing so. Sorry for the image i used its low quality one and its just for test.
EDIT
In Storyboard if you see the bottom of the screen you will see below image. In center of that image there is wAnyhAny.
you need add constraint to thoes components!
Otherwise, some components will be placed out of the screen!
You can learn how to use Autolayout from here
![enter image description here][1]At right bottom of storyboard in xcode you will see triangel Resolve autolayout isuue select in Reset to suggested constraint.

Auto Layout views inoperable in iOS 6

I'm using Auto Layout for an iPhone app to place a button footer bar at the bottom of the screen on both 3.5" and 4" iPhones. The bar is a UIView containing two UIButtons and a UIImageView.
I have a Vertical Space Constraint pinning the bottom space to the superview.
(Editor -> Pin -> Bottom Space to Superview)
It has an Equal relationship. Constant is 60, Standard is NO, Priority is 1000, and Placeholder is NO.
In iOS 7 it works exactly as expected. In iOS 6, it does not. The bar is not a consistent distance from the bottom when I switch between that view controller and its neighbors, and the buttons frequently do not work (they don't even highlight when I tap them). This is all in the simulator, I haven't tried hardware yet. The app is a legacy app, so all of the layout work is in xib files, not storyboards.
Do I need to do something different in iOS 6 to get Auto Layout to work? From what I read, it should be fully compatible with 6 & 7.
Some more information is needed to really get to the heart of the matter. Specifically how auto layout is being used to attach the bar to the bottom.
One thing that gets me over and over though, is remembering to set the translatesAutoResizingMasksIntoConstraints property to false on view(s) that utilize constraints.
Be sure to check the log output while debugging as that will indicate if you have any ambiguous or unsatisfiable constraints. At design time, XCode 4 IB will attempt to add any constraints that you are missing for you. XCode 5 IB will only tell you what you are missing and then at runtime try to add any missing constraints if it can.

Centering UIView in middle of superview springs/struts

I have a basic question regarding springs/struts (I'm working on a legacy codebase that prefers it over auto layout), and since I started iOS using auto layout I am trying to understand why this doesnt work. I am wanting to simply center this UIView in superview.
What I have (notice my springs/struts settings):
So with that, it views just fine on an iPhone 5 screen.
Here is what iPhone 4 screen displays though:
As you can see, it is much lower than what it should be. How can I achieve this?
Thanks.
That's because you set the top and bottom margin to be fixed. Try removing them from the storyboard, and you'll see that your view will be centered vertically.

Resources