Xcode: Stack View Move to next line if screen is too small - ios

Using Stack View, I want to get 3 items next to each other, which I achieved:
However, what I am looking to do is if the screen is too small to fit everything nicely in, then to move the block to the next line such as:
Should I not be using Stack View for this?
Github version of sample project:
https://github.com/jzhang172/StackTest

UIStackViews cannot do that kind of automatic layout where it sends overflow to a new row AFAIK. For what you are trying to do there are several other options depending on your purpose.
Embed several stackviews in a large UIScrollView and size it to only show your first row. Later could "grow" that scrollview to expose additional stackviews as needed.
You could dynamically create additional UIStackViews located in the lower position and fill them as needed.
You could consider a UICollectionView instead.
BTW: There is a github project for OS X embedding stack views in a scrollview dynamically. It might help you out: StackViewTest

Related

UIImageView streches over entire row height of UITableViewCell

Intro: Hi, I'm working on a "News" app, that uses a news API online. I want to display the content of the latest news in a TableView (Currently I want to display the title of the news item, the name of the organization that posted it (e.g: the BBC), and an image that I load from a given link). I dragged a table view in the Main storyboard, created a custom cell for a singular news item, and get all of the required information from the internet.
The Problem:
When all the data gets loaded into the TableView, the image streches across the whole rows height.
I'm trying to find a work-around for hours now. At the moment, In the prototype cell, I have a Stack View, that contains 2 labels and an ImageView, with a Vertical Axis, and a Distribution of Fill Equally.
There are no constraints on the Views inside the Stack View (the 2 labels and ImageView), and I'm not editing any constraints within the code, to keep it as simple as possible.
I also tried the constraints approach prior of using the stack view, but it gave the same result.
Image Of The Stack View
The Result in the Simulator (Rows without an image, don't have an image url provided by the api, so I just skip them).
I tried being as expressive as I can about the problem, if you are still missing some information please let me know
The problem you are encountering is due to constraint issue. The problem should be solved by unstacking the view and put constraints instead.
You can still achieve scalable application using them.
If you are confused with how to do it, refer https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithConstraintsinInterfaceBuidler.html
https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/AutolayoutPG/AnatomyofaConstraint.html
These should help.
I think you are in right Direction.
if you want static(fixed) Layout Follow below step.
change StackView Distribution from Fill equally to Fill
Apply StackView(leading,trailing,top,bottm) =
cell(leading,trailing,top,bottm)
set tableView rowHeight to 300 or whatever you want
if you want dynamic layout follow below step
https://www.raywenderlich.com/1067-self-sizing-table-view-cells

iOS / Xcode: Group overlapping elements in Stack View

For my game, I want to have this element for movement:
It's actually one image with 4 buttons on it:
Now how do I go about making them adaptive as in a stack view?
Since stack view doesn't really allow me to make elements overlap, I don't see an easy way here. Or I could make different stack views and make these overlap, but would that really be a good approach?
Is there another way to group elements in iOS, which allows more than horizontal and vertical stacking?
Apple suggests to use UIStackView rather than NSLayoutConstraints for designing the view but, it is not necessary to use UIStackView always.
According to mentioned situation in question, it is better if you use constraints rather than UIStackView here.
But you wish to stack here, then take the 2 images in stack at a time and then align them in center of container view.

Swift stack view in storyBoard

Im having an issue with my layout in my story board. When i make a stack view how can i adjust the size of the UIlabel box to fit more text?
Image Example:
Also how can i get rid of the stack view to bring everything back to its original state?
To add more text- simply type more text. The label adjusts itself.
To get rid of the stack view- select the stack view and go to Editor->Unembed
I have given you a brief overview of auto layout. The examples that we have worked on were pretty easy. However, as your app UI becomes more complex, you will find it more difficult to define the layout constraints for all UI objects. Starting from iOS 9, Apple introduced a powerful feature called Stack Views that would make our developers' life a little bit simpler. You no longer need to define auto layout constraints for every UI objects. Stack views will take care of most of that.
In this chapter, we will continue to focus on discussing UI design with Interface Builder. I will teach you how to build a more comprehensive UI, which you may come across in a real-world application. You will learn how to:
Use stack views to lay out user interfaces.
Use image views to display images.
Manage images using the built-in asset catalog.
Adapt stack views using Size Classes.
On top of the above, we will explore more about auto layout. You'll be amazed how much you can get done without writing a line of code.
What is a Stack View
First things first, what is a stack view? The stack view provides a streamlined interface for laying out a collection of views in either a column or a row. In Keynote or Microsoft Powerpoint, you can group multiple objects together so that they can be moved or resized as a single object. Stack views offer a very similar feature. You can embed multiple UI objects into one by using stack views. In most cases, for views embedded in a stack view, you no longer need to define auto layout constraints.
Quick note: For views embedded in a stack view, they are usually known as arranged views.
The stack view manages the layout of its subviews and automatically applies layout constrants for you. That means, the subviews are ready to adapt to different screen sizes. Furthermore, you can embed a stack view in another stack view to build more complex user interfaces. Sounds cool, right?
Don’t get me wrong. It doesn’t mean you do not need to deal with auto layout. You still need to define the layout constrants for the stack views. It just saves you time from creating constraints for every UI element and makes it super easy to add/remove views from the layout.

Change constrains depending on screen size

I tried to solve this at my own, but I just really can not understand auto layout. I also searched here for a solution, but I could not find anyone with this similar problem (although I see a lot of people struggling with auto-layout). I want to make an app in landscape mode what is supported by every iPhone 5 and higher. I set up some constrains, with this as result:
How it should be
Above is the correct image on how it should be on every device. Can I set it relative to a screen size? I tried changing values with the multiplier, but that did not worked out well.
How it is on bigger screens
Here you can see how it is now. Because the values are absolute and not relative, I think it not changes... By the way, I am using " compact height" for building my landscape apps for iPhone, is that correct? Or is this the cause of this big trouble?
Here's a quick run through that might be more applicable to your needs than the general tutorials...
Create your elements on the storyboard and lay them out in roughly the right place:
Select items 1-4 and hit the stack view button, then select items 5-8 and do the same, Xcode is very good at guessing what you want so you'll get two vertical stack views, like this:
Select them both, hit the stack view button again and you'll get a horizontal stack view that contains your two vertical stack views:
Then select this stack view and the header and press the stack view button one last time to put them in another vertical stack view:
How you just need to adda few constraints to the outer stack view and then tweak the settings of each stack view. In this case, I've pinned the outer stack view 20 from the view edges, set the outer stack view to fill proportionally and everything else to fill equally, plus I've set the vertical stack views to centre alignment. You can play with all of these settings to get a result that you find pleasing:
This will now scale perfectly for any size screen:
I've just used labels for speed, but the same rules apply for images or whatever.
Hope that helps, stick with the tutorials and keep practicing until you figure it out, it will save you lots of time in the long run.

storyboard constraints (Xcode)

I'm new to programming and I'm trying to work with Xcode. I have no experience. I'm trying to make an app with Xcode. I'm done with the code, the only issue i have is with the storyboard constraints. I need some one who can help me to set them up.
So, i have 28 buttons as you can see in the picture, I want to keep the size of 65x65 pixels. that means that the distance between them should vary in different device sizes(4inch, 4.7inch and 5.5inch). i also should mention that i want it to be only in portrait view and only for iPhone.i hope someone can help because I'm to noob for this, I've searched a lot on the internet but I'm not able to do it.
UIStackView (iOS 9+)
UIStackView was introduced in iOS 9, which is very useful in your case.
Edit: Before getting started, make sure to have size classes to be w Any, h Any, this will make thing easier.
To use this, search UIStackView in your utility panel.
Drag an drop a Vertical Stack View on the view of your view controller and set the constraints so it spans the whole screen.
Select the stack view we just placed and set the distribution to Fill Equally.
Drag and drop a Horizontal Stack View onto the vertical one.
Select the horizontal one and set the distribution to Fill Equally.
Place your buttons in the Horizontal Stack View.
Copy and paste the horizontal stack view multiple times based on number of rows you want.
That's it.
You can control the buttons' background images to make it smaller (65px), or you can place a view as its' parent then align horizontally/vertically to place it to the center, like this:
Orientation
If you'd like to lock the orientation throughout the app, go to the settings of your project.
Sample project
I made a sample project for this question, you can check it out here: https://github.com/cyhsutw/UIStackView-example

Resources