Stack view issue in iOS - ios

I have two labels and one image view (the "sound" image) in a stack view. Actually, the top label and image view are in a horizontal stack view which is inside another vertical stack view.
I want the "test" label to be as close to the image view as possible, however, as you can see in the image the "test" label stretches out as much as it can so that the "test" + image view width matches the width of the label below. How can I fix this?
I know I could use some fake transparent view in the top horizontal stack view to push the image view as close as possible to the test label but I'd rather avid hacks.

That should be pretty easy.
Interface builder:
Result:
Settings:
Give your test UILabel a height if you want to make it good, or equal to the button or imageView, but as I can see in your screenshot, you already did what specifically what you wanted to do, except the settings of your vertical (main) UIStackView.
Play with your stackView settings. In my vertical (main) stackView, my Alignment is set to Leading. Setting it to the default Fill will achieve the unwanted result like in your screenshot. Distribution is up to you.
I hope this helps! :)

Related

How do I ensure UILabel only takes the space it requires, without extra padding?

I have a peculiar issue when dealing with my label in Vertical, then horizontal stack view.
The horizontal stack view is supposed to be of a dynamic height based on the label's contents. I've tried other solutions mentioned before, such as embedding the label (and image which is also in the horizontal stack view) in separate UIViews and pinning the labels to the edges.
The issue is, when the label is set to 0 - the label grows beyond what it requires, adding padding above and below the label.
I did verify this wasn't an issue with constraints from where the Stack View begins below the imageView, as the warning label correctly floats upwards when aligning the stackview to "top", but the label still remains centered.
Everything has been done in the storyboards without code.
Take a look at the images, maybe someone can help me out and recognise how I can resolve this issue.
Desired Effect
What occurs upon running the application
Verifying that the stack view is correctly pinned to the image view by aligning top
Constraints involved
The second stackview in the hierarchy at the bottom is just to fill the empty space during the designing phase. I don't think that would be causing the issue.
I would guess that actually your second stackView at the bottom is also the part of your problem. If you pinned the vertical stack view to the bottom of the image (TOP constraint) but you also pinned it to the bottom of the screen (BOTTOM constraint), your vertical stack view tries to strech it's content. That's the reason why the label has the extra space.
What you can do: get rid of that bottom constraint (don't give vertical stackView bottom constraint) or maybe better set vertical content hugging priority of warning label to greater value.
Here, the warning UILabel is occupying the remaining available space, try adding an empty UIView in the second stack view which you kept for the later stage of the development. This will act like a spacer view.
Also, don't forget to call warningLabel.sizeToFit() inside viewDidLoad()
You can achieve this by calling yourLabel.sizeToFit()
Above will dynamically adjust the height of the label based on the amount of space your text occupies.

How to have content view in cell take up entire cell?

I am attempting to create a horizontal stack view and have it take up the entire cell width. I have two vertical stack views inside this horizontal stack view. I have pinned the horizontal stack view to the leading and trailing edges of the superview as well as aligned it vertically to the containing view. It seems to look as expected when I see it in the storyboard link to picture here:
but when I run it has this spacing on the right side .
I've tried to see if I could see the width of the content view of the cell by having the background this maroon color. To me, it seems like it is the entire width of the cell so I am not sure what I am doing wrong here.
UPDATE: After adjusting widths to the stack view. I am not quite sure if this is a stackView issue exactly. Setting a fixed width for the stackview fixes my issue for one screen but this does not work for all screens hence the attempt to constrain it to the edges. I ended up stripping out everything out of the cell and just left the title label.
Stripped Down Cell
I centered this to the parent view but it still ignores its constraints.
Run Time View
I am guessing auto layout does this. Not quite sure what takes priority and is forcing it to ignore my constraints.
I looked into the debug view hierarchy and it has a purple label on the UILabel indicating that the position is ambiguous.
Debug View Hierarchy
I am guessing autolayout then tries its best to fix its position. Not sure what is ambiguous here.
UPDATE 2: I dont know what was wrong with the storyboard constraints I set. I ended up removing everything from the controller and starting from scratch. After doing this it worked as expected. Sometimes starting from scratch is the best thing to do.
You need to define the width of your horizontal stack view children (Both vertical stack view). You can set the width of both vertical stack view to equal this way they will take equal space and fill the parent.

how to prevent buttons from shrinking in a stackview

I have been struggling with getting buttons (circular) to keep their size in a stack view to no avail.I have two rows of buttons where I place each row in its own horizontal stack view and then embed both stack views in an outer stack view to make it easier to set constraints.
The issue is that the buttons do not keep their original size in portrait or landscape. What settings am I missing to achieve this? I set the size of each button to w=50 and h=50 by resizing each button (no constraints)
Before embedding stack view:
Top row in a stack view:
Each button is of Type CircularButton
I set the size of each button to w=50 and h=50 by resizing each button (no constraints)
That is certainly one thing you are doing wrong: "no constraints". The stack view is a constraint maker. That is all it is. But it is not a mind-reader. It needs its arranged views to have certain constraints, under certain configurations, in order to know what you want. You have to tell it. Constraints are how you do that.
Let's stipulate that a lot of your question is a red herring — the roundedness of the buttons, the double set of stack views. All you want to know is how to make three buttons be spaced horizontally at equal distribution by a stack view without losing their size. Here's how.
First, configure your stack view like this:
Second, give your buttons height and width constraints, and wrap them in the stack view. Now use more constraints to position and size the stack view where you want the buttons distributed:
As you can see, that works as desired in the running app:
The rest of your interface merely builds upon that.
In the Attributes Inspector, you can set the alignment and distribution to center. This should work:
Also provide the same height and width for the buttons or put aspect ratio of 1:1 for the buttons.

making an auto layout of UIimageView and textview inside scrollview for iOS 10.2 in xcode8

I am making a note taking app in xcode8 for iOS 10.2. The note is consist of a textfield, textview and imageview. I am adding textview and imageview in separate scrollviews (to provide the facility of zooming and adjusting image). Then I have added both scroll views in a stackview to ensure that imageview and textview stands side by side in a landscape mode. To do that, I have added variation to axis property of stackview( width=any, height= compact and Gamut=any, and set that to the horizontal, as shown in right corner of screenshot to see view hierarchy without any constraints).
I have tried many constraints but, every time it fails to adopt. Let's take an example, I am adjusting size of the scrollviews on the size of content(by setting top,bottom,leading and trailing constraint), the stack view is adjusted to the main view. Now, the only thing that needs to be adjusted is text view and imageview with respect of stack view. But, here is the tricky part If I include a fixed trailing constraint of text view to the stack view; it will work in portrait mode.As soon I turn into landscape mode that constraint will not work(the trailing constraint which have 0 distance from stackview) because the edge will be side to the imageview not to the stackview(because I am putting image and text side by side as explained above) which will make textview larger than required, same goes for imageview's leading constraint to the stack view and vertical distance constraint betweem image view and textview.I think the root of the problem is I have made a variation to the axis of a stack view (in order to get text and image side by side in landscape mode) but, that was necessary part.Can anyone have solution for this? Do you think there can be other problem? Should I choose Intrinsic Size of scrollviews to placeholder option or let it be system default?
Thank you.
So I hope I understand the question and I will try to walk you through what I understand you hope to accomplish. I preface this by saying I just started using UIStackView because of backwards compatibility.
Note. To get the magic of ScrollView with AutoLayout I almost always embed them in another UIView. There are reasons to not but in this case you will see how valuable this is to AutoLayout.
Step 1) Drag your UIStackView and add Top,Bottom,Leading, and Trailing. Now add 2 UIViews and set the UIStackView to Fill Equally. It will now look like the image(Background colors to check your work).
Step 2) Now add a UIScrollView to each of these UIViews. Add Leading,Trailing,Top,and Bottom on each of these.
Step 3) Add a UITextView and UIImageView to the ScrollViews respectively. Now Autolayout is mad at us :( but we will fix that.
Step 4) Drag from the UIImageView to the View that is holding the UIScrollView(First Set of Views we added). See image
-Choose the option to add Equal Widths. See Image
Repeat the same step but add Equal Heights. Now add Top, Bottom, Leading, and Trailing to the ScrollView.
Step 5) Repeat exact Step 4 with UITextView
Your final view hierarchy should look like this.
Now you can rotate your UIStackView and do what you want I think.
And Horizontal
Side by Side Preview
I did add a <= 0.1 equal heights multiplier on the textfield at the top but I don't know if that was necessary.
Enjoy.

How to display label on top of UIImage?

I have been trying to add a label on top of an image but the label is under the image and I've tried turning AutoLayout off, adding constraints and moving tried moving the label on top of and on bottom of the UIImageView in the View controller scene but its still under neither. Anybody know a quick fix?
Your view hierarchy looks OK (i.e. the label is further down in the view hierarchy than the image view, meaning that it will appear "over" the image view). If you're not seeing the label, it either doesn't have text in it or the constraints are not defined properly.
I'd suggest running the app through Xcode and then clicking on the "view debugger":
Then you can examine where precisely the label is (confirming that it is above the image view) and what its frame is (i.e. perhaps your other constraints are not defined properly) and what text it contains (perhaps it's in the right place but just doesn't have text to show).
You can either navigate through the view hierarchy in the left panel or rotate and expand the wireframe rendition in the main panel.
I guess that is due to your arrangement of elements and constraints in IB. Try removing all the constraints and see if you have the label on top of the image. If so - add constraints gradually and observe not to launch your label off the screen.

Resources