UIStackView with UILabels in Xcode 7.3 - ios

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!

Related

Xcode 9 setting label with 0 lines to height of 14,600.5 in stack view

In Xcode 9 I am following along to the AppCoda tutorial to get started in iOS development. I noticed some odd behavior and am wondering if this is a bug or something I am not understanding with stack views in the Interface Builder (since the app does compile and displays correctly on my phone).
When I place the Label and set it as the third element in the stack view, a sibling of two other nested stack views, it appears fine if the default attributes are used. However, when I change the lines property from 1 to 0, the label and the adjacent stack view disappear from the interface builder.
In the size inspector, the coordinates for the label changed from 0,208.5 to 0,-90. If I change these properties, the height of the label changes from 0,0 to 375,14600.5, and Xcode throws an error: Interface Builder does not support UILabel sizes larger than 10,000 by 10,000.
Is this a bug with Xcode, or is there some changed behavior with the lines attribute for this version of Xcode (the book currently covers Xcode 8).
Here's a solution. Select the label view and go to the size inspector. Under label preferences check the explicit size box. This prevents the stack view from collapsing when you put a multiple line label into it.
Set Size Inspector > Intrinsic Size > Placeholder, set width to none and height to say 30
From description: Setting a design time intrinsic content size only affects a view while editing in Interface Builder. The view will not have this intrinsic content size at runtime.
From Xcode Help: Use a placeholder intrinsic size to indicate a view’s width and height and avoid design time constraint ambiguity.
This is a bug in Xcode 9 - see https://github.com/lionheart/openradar-mirror/issues/18779. It only seems to affect how the canvas is drawn in Xcode: at least for me, my app still builds correctly. I first saw it after I upgraded to 9.0.1 from 9.0, and Xcode 9.1 still has the bug. I downgraded to 9.0, which doesn't appear to have this bug.
This has been reported as bug 30484038 on Apple's Bug Reporter (bugreport.apple.com) - I reported a similar error and they replied that it was a duplicate of 30484038. That bug is still marked "open". I've asked if there is a workaround or an expected time until it's fixed, but they haven't responded with an answer as yet.
I will try setting an explicit size for the affect UILabels to see if that serves as a workaround.
UPDATE: This bug appears to be fixed in Xcode 9.3. In my case, Xcode now draws labels properly when the lines property is set to 0. Also, in the release notes for 9.3 I found this under Interface Builder, Resolved Issues:
Toolbars or multiline labels in stack views no longer result in an Auto Layout misalignment on the canvas. This issue resulted in problems such as zero width multi-line labels, or in extra tall toolbars when an iPhone X was selected in the device bar. (35377301)
In xib, just set a temporary height constraint for label(like 100), and delete it. It will display the height you just set.(just the display height, not actual height).
I think it's because your text label is too long.
numberOfLines docs
This property controls the maximum number of lines to use in order to
fit the label’s text into its bounding rectangle. The default value
for this property is 1. To remove any maximum limit, and use as many
lines as needed, set the value of this property to 0.
Basic on your constraints, your label can't fit into stack view. I suggest you divide your big UILabel into smaller pieces.

Xcode 9 - "Fixed Width Constraints May Cause Clipping" and Other Localization Warnings

I downloaded the new Xcode and in Interface Builder I'm having a ton of problems with warnings that say things like:
Fixed Width Constraints May Cause Clipping
It looks like this:
I do have localization for several languages and I understand the warning that in another language a label's size may change, but my app doesn't have this problem. I ran and tested it in Xcode 8 yesterday, it was fine. I don't want to spend hours and hours adding pointless new constraints.
Any suggested solutions?
I was getting the same warnings even without multiple languages in my app, which led me to find out what was really going on. . .
There are a few different things going on here. I was able to silence the fixed-width warnings in my own app by changing the width of the object spacings from fixed width to greater than or equal or less than or equal.
This can be done by selecting the object in interface builder, going to the size inspector and changing it there:
Or, select the constraint from the document outline, go to size inspector, and change it there:
As far as the warning at the top of your screenshot:
Fixed leading and trailing constraints with a center constraint may
cause clipping
Here is a screenshot from my own app in which I was getting the exact same warning:
I had the label with the # sign set to leading and trailing to the buttons but also to align the center with the rating label. Once I removed the center alignment constraint, the warning disappeared, but I was left with an improperly laid out set of objects.
It is then that I resigned myself to embrace the Stack View. As annoying as it is to use, when you get all of the constraints and settings right, it lays out beautifully and with no warnings.
Edit
As Repose writes in the comments, sometimes simply adding >= 0 will be what you need, as you are making sure two elements do not overlap.
You can try Disabling "Respect Language Direction" on per Constraint basis to silence the warning and see if it helps. Select your constraint and open Attributes/Size Inspector. Please see image attached.
If you are not planning on localizing your app to other languages, then this solution should not have any drawbacks. For localized apps you have to be more conscious of your label and font sizes.
p.s. This solution works for iOS. For macOS try >= or <= to silence the warning.
p.p.s. Labels in the picture below are much easier to create using AutoLayout and attributedString property on a single UILabel or UITextView using NSMutableAttributedString. The image is for demonstration purposes only.
For labels and buttons which are localized this warning makes sense and you should provide the necessary constraints so your labels don't overlap. If they don't overlap now they might in the future, so it won't hurt to provide the constraints.
Xcode helps you add these constraints automatically:
In the document outline of your storyboard click on the yellow arrow and either choose "fixed leading" or "fixed trailing", depending on where the text is on your screen (left or right). This will fix it for most issues.
If you have this issue with a Button without any text (only image), try to remove the "default title" which might still be set for the button:
With Labels, you can set Lines is 0 and Autoshrink properties is Minimum Font Size to remove Fixed Width Constraints May Cause Clipping warnings, like this:
Another quick solution !
For a UIButton by changing the title from plain to Attributed text also resolved my issue:-
I know this question has already been answered but what I did to fix this error in my case was to add the "Aspect ratio" property and then eliminate the width or height constraint this worked pretty well and was less effort, and I managed to keep the same output and adapt my view for the different devices.
Swift 4 , Xcode 9.1 :
About this issue, I think your object don't know what it's the correct center position in the context of it's superview, and using remove, greater than or other leading/trealing settings most of times don't work correctly. First, you must check the correct constraints of your superview.
If your superview/s are correctly setted, you can try to "explain" to your object what is the correct position in the view by setting the "horizontally in Container" constraint:
If you need fixed width constraint for button just set width constraint priority to 700.
I had the same problem, but when I changing to >= it automatically set the constant to 0, if I choose 60 for instance, the warning appears again. So I was in a loop with the problem.
I could fix embedding my Label in a View
Editor > Embed In > View
In Label I set Top, Bottom, Leading and Trailing with constant = 0
In View I set the constraints that I was expecting before.
I had the same problem when moving to Xcode 9 and found an approach that's useful for certain kinds of layouts. In my case, I wanted a table header in which two columns (UILabels) were of fixed width and another was of variable width. Regardless of how I specified column widths (including using constraints greater than or equal instead of equal, etc.), I kept getting the warning about possible clipping. In my case, I wanted the variable width column (UILabel) to clip if necessary. I could have just ignored the warning, but don't like doing that.
The approach that worked here was to create a UIView with appropriate size constraints and embed the UILabel as a subview in the UIView. Then truncation happens if necessary and I get no warning. This works whether the UIView/embedded UILabel is in a StackView or not.
This is essentially the same approach as that of Haroldo Gondim but here you can see it also works with or without StackView.
The following image shows the approach, with and without StackView. "SpacerName" is a variable width UIView containing a label and "SpacerPD" is one with a fixed width of 80. [Colors are not significant; just there to show where the views are.]
As you can see in the image below, I was having the error "Fixed Width Constraints May Cause Clipping" because although I had set my textbox to be vertically centered and my label to have a left margin constraint, I hadn't defined a constraint for the text box in relation to the label, so XCode was alerting me that the textbox could clip (be rendered above) the label.
After adding the left constraint to the text box to always stay some distance apart from the label the error was considered solved by XCode and it didn't bothered me with the constraint warning anymore.
I had a similar issue when trying have the button with the same paddings from the edges of the super view.
I've ended up using horizontal center constraint and equal widths constraint to the super view.
To Fix The Error: Fixed Width Constraints May Cause Clipping” and Other Localization
You need to select the view/object, go to the "Show Size Inspector", find the Width Constraint and set the Constant to Greater or Equal to:
To Fix The Error: Leading/Trailing constraint is missing which may cause overlapping with other views
This means that the view/object Xcode is complaining about, is missing a Leading or Trailing Constraint to a neighboring view.
While holding control, drag to a near by view/object
Add a Leading or Trailing Constraint

Horizontal StackView Spacing not working at runtime

I'm having problems when using StackViews for laying out my views when device rotation changes.
I get good results in storyboard.
However it isn't working as expected at runtime; after doing rotation on simulator, search control size increments and space is gone! I even tested on a physical device.
I had width constraint for the filter button; that's all I need I think.
Problem is gone now! Fixed by myself by deleting and re-adding controls to the UIStackView, looks like stackviews are not very stable; sometimes it might fail, so must delete and add them again. Have in mind that!
Width constraint for filter button

UIStackview weird behaviour with size classes

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

What is the reason for the remaining vertical spacing between an "inner"-view and the contentview of an UITableViewCell using Storyboard?

I am using storyboard in an iOS/xcode project with the following simplified "hierarchy" (from top to bottom):
ViewController (not a TableViewController for some customizing reasons)
View
TableView (and some other content that does not matter I think)
Cell
ContentView
"AnyView" (specific type seems to be irrelevant, as the following happens for every item I tried)
When I set the constraints between AnyView and ContentView to Zero Spacing for Top/Bottom/Leading/Trailing there is still quite some "space" between the ContentView and AnyView. (I have never encountered this when doing similar things just programmatically.)
In order to get rid of that spacing I looked for everything in storyboard options that sounded even remotely like spacing / intending etc., but did not really succeed.
By accident I just realized that when I drag & drop a view into the ContentView in a certain way it appears that storyboard sets a negative spacing of -8 that seems to "remove" that spacing. I could just use that number in my constraints, but it seems somewhat random. Does anyone know what the reason for my unwanted spacing might be or if that -8 has a deeper meaning (e.g. maybe Apple is forcing some design guidelines onto storyboard users that way?)?
Thank you very much for any insights!
In Xcode 6 constraints can be "relative to margin". The margin is set as the normal default spacing to the edge of the screen.
You can turn this off in the "add constraints" button...
Or in the constraint property inspector...
I know this may sound weird, but the only solution for me to remove top padding in UITableViewCell is to remove the existing "Top Space" constraint and add it back and setting it to 0.

Resources