I have stacked a view has a stack of labels and a TableView in single stack view (as shown in the picture below), however it's giving me a constraints for the x and y position of the two views inside the main stack view. I tried adding constraints for both views but it didn't work.
Stack View Constraints error
It seems like you don't have constraints for the stack view itself.
You could click this blue icon to see your layout constraints(you should check both Horizon and Vertical).
Also, if your constraint has problems, you can see there is a red arrow, click it to see the error message.
Related
I am trying to drag and drop buttons to the storyboard. The buttons seem good in my storyboard and preview like the following;
However, when I run the simulator, the button texts seem like the following;
Why the buttons are not shown to fit in the buttons of the simulator and how can I fix this?
Note: Preview and device types are iPhone13 Pro.
The iOS 15 / Xcode 13 style buttons are highly dependent on auto-layout.
Based on your screen-shots, it doesn't look like you've given the buttons any constraints.
You do NOT need to set widths or heights, but you DO want to set at least horizontal and vertical position constraints.
So, constrain all 4 of your buttons centered horizontally, and constrain:
First button Top to view Top (safe area)
Second button Top to First button Bottom
Third button Top to Second button Bottom
Fourth button Top to Third button Bottom
Then you should see them laid-out correctly at run-time.
I think it's because of the auto layout constraints. I am not very familiar with storyboards, if you don't set width of the view component, it seems fine on the storyboard but when compiling the view it actually has default size.
Try to set some constraints for width. Maybe it would help.
The first thing you need to do is to create identical buttons with identical size and with identical font size.
As you can see in your project, the buttons have different sizes, but the text is the same size in all buttons.
To make it faster - you can create one button and make a copy with option + drag’and’drop…
Then, you can put them in a Stack View. So, it will be easier for you to work with them in the future.
Select all buttons and make a Stack View...
https://i.stack.imgur.com/QLTJP.png
https://i.stack.imgur.com/OlOia.png
After that, resize your Stack View like you want.
Then, tap on a Stack View and clear the constraints.
https://i.stack.imgur.com/1pMT8.png
Fix the dimensions like this. But, without “Constrain to mergins”.
https://i.stack.imgur.com/8HKKF
After that, make for the Stack View - horizontally and vertically position in your storyboard.
https://i.stack.imgur.com/a29wL.png
The result is…
https://i.stack.imgur.com/mvQjg.png
Hope it’s break your problem! :)
I’m trying to achieve this layout using the interface builder
I’ve already tried to embedded the bottom button inside another stackview, also tried to set it out of the stack view but nothing works as expected what have now is this
And getting this result
Can someone tell me what constraints are missing or what is wrong with my current constraints?
You usually don't need to add too many constraints to the stack view's arranged subviews. Setting the distribution and alignment correctly is the key. The constraints involving Stop Button are either unnecessary or causing the conflict that you see in the console.
To position the stack view, you just need to pin its top, bottom, leading, trailing edges to the safe area. For example:
Then, set the alignment of all the stack views to "Fill", and the distribution of all the stack views to "Fill Equally".
I’ve been working on an app with a grid of buttons. The grid consists of 4 buttons per row, and (currently) 6 rows. In the storyboard, each row of buttons is in a horizontal stack view, and all 6 stack views are in a vertical stack view.
I don’t want all of the buttons to be visible all the time, so I’m turning them on and off with .isHidden. This is causing some problems when I run the app in the simulator:
I want all of the buttons to stay the same size, but if one or more buttons in a given row / stack view are hidden, the remaining buttons in that row adjust their sizes to fill the row. I’m guessing that a combination of constraints on the buttons and settings on the stack view will solve this, but I haven’t come up with the right combination yet.
If I start with, say, the first 3 rows of buttons all unhidden, then try to unhide a button in one of the other rows, all of the buttons disappear. However, if I ‘print’ the .isHidden state of each button, the ones that should be visible have .isHidden = false.
Any ideas for solving either of these problems?
Thanks in advance for any assistance.
To answer your first question...
When you hide a view in a UIStackView, auto-layout treats it as if it is "gone" --- the stack view will re-layout its arrangedSubviews as if that view was never there to begin with.
If you want this result:
Your best bet is probably to set the .alpha property of that view / button to 0. It will be completely invisible (so, in effect, "hidden") and the stack view will retain its current layout. And, controls with .alpha = 0 do not respond to touches (so you can't tap the invisible button).
For your second question, I'd recommend you re-post that question on its own. Make sure to include the code you're using that is not having the desired result.
See: How to Ask
I recently ran the updates on xcode9 and I'm getting blank space on the right side of the screen(about 10px) for every view controllers when I run on the simulators. Is there anyone getting same errors?. I didn't set any specific constraints. I uploadad on github just in case. Does anyone know how to solve this problem?
storyboard
After looking into your project, I noticed that the table view of SingleViewController (on the main storyboard) does not has the appropriate constraints, also its size and origin are incorrect. What you should do is to edit the frame (from IB size inspector) of the table view to be:
instead of:
Also, you should setup the appropriate constraints for it:
Select the table view and tap "Add New Constraints":
Note that I added four Constraints: top, lead, trail and bottom with 0 values.
That's should fixed your issue.
Also, I would recommend to setup constrains for all of the other views in your project to avoid such an invalid layout...
Im making a custom keyboard, and wanting a label and a textField centered in the space left over the keyboard, I figured the proper way would be to incorporate them in a UIstackView, and then centering the stack. However, i'm having som issues with the stack resizing my textField, causing whatever text i enter to be clipped.
I tried adding compressionresistance and a number of different solutions, but its clear to me that im missing some information about how UIStackViews work. Usually i make them work, but the whole resizing part I dont understand.
The first two screens are without a stack, adding them as subview to the viewcontrollers view, and the second screenshot shows the stackView resizing my textfield everytime the keyboard is resigned.
How can i stop the stackview from resizing its contained views?
It seems UIStackView is inherently a auto layout component, and when it adds your arranged subviews, it automatically positions them using constraints.
Since the UIStackView is adding constraints of its own, and the best way I found to stop it, was to simply explicitly set the subviews constraits:
textField.widthAnchor.constraint(equalToConstant: 300).isActive = true
Apple doesnt like dynamic StackViews: "In general, you should not use dynamic stack views to simply implement a scratch-built table view clone." Read the
Apple Documentation on UIStackView for details.