Hiding view in stack view in iOS 10 - swift

When I hide a UIView inside UIStackview, on iOS 11 it nicely removes the gap and brings the adjacent UIViews closer inside the UIStackView. However, on iOS 10 it will not move the adjacent views and just leave the gap for the hidden view. Is there a way to get around it without having to manually move the views.

Have you added your own height constraint to the UIView? It will cause a conflict with the constraint height = 0 generated by the UIStackView when the UIView is hidden on iOS 10. Just lower the Priority of UIView height constraint (e.g. 999) and the UI will update properly.

Related

StackedView inside ScrollView in Xcode 8 / Swift 3

I'm trying to arrange stacked views so I can set out the UI of my app to look neat and tidy.
The only problem I'm having at the minute is getting it set up so I can see it!
At the minute, it looks like this on Main.storyboard and it's annoying me:
Storyboard view
My constraints are currently set up as follows:
Constraints (edited)
Where am I going wrong?
Your UIScrollView needs to know its content's width. Since it can scroll both horizontally and vertically, it does not act like a UIView on interface builder.
One thing you can do is adding a hidden UIView in UIScrollView with 0 height and |-[WidthView]-| constraints (leading and trailing to its superview - UIScrollView). And then you should add WidthView.width = UIScrollView.superView.width constraint.
This makes sure that your UIScrollView's contentView's width is always equals yo UIScrollView's superView's width.
Your view hierarchy should look like this:

Move visible images in StackView closer to leading space

I have a horizontal ScrollView of 20 (110 * 130 point) UIViews contained within a StackView. Each UIView contains an image.
When certain UIViews are hidden the UIView width is maintained at 110points. However, spaces are left were the hidden UIView should be.
I have been practicing with a StackView of 3 UIViews and hiding the center UIView. This leaves a space in the middle...
I would like the 3rd UIView to move into the 2nd UIView position (middle) and perform this for subsequent UIViews later on.
Does anyone know how I could go about doing this? Is this even possible?
I was hoping to accomplish this with a UICollectionView, however, I don't think you can drag an image from UICollectionView to UIView
Drag and Drop Image from UICollectionView to UIView?
A storyboard isn't meant for these complex user interactions, so I recommend you programmatically create your stack view and then set its constraints programmatically as well. This let's you change elements' heights in your code easily. This might help you get started:
Add views in UIStackView programmatically

StackView Horizontal Images - Maintain same images width if hidden

Let say, I have a horizontal ScrollView of 20 (110 * 130 point) UIViews contained within a StackView. Each UIView will contain an image and a label.
If I decide to hide certain UIViews from the StackView it changes/stretches the width of the UIView.
How would I be able to maintain the same width/height of the UIView?
So lets say if I decided to hide 19 UIViews contained within the StackView. Is it possible to only show 1 UIView (110 * 130) within the StackView?
I have tested this out using a StackView of 3 UIViews in Storyboard with no success.
If I get this to work then my plan is to programmatically code in the 20UIViews.
I would appreciate any help.
You need to set constraints for the views inside the stack view. So in your case, the width constraint isn't set for the UIViews and is therefore causing weird problems.
I recommend checking out this guide on working with stack views in storyboard : http://www.appcoda.com/stack-views-intro/

UIScrollView Scroll issue in Xcode 7.2 size classes

I have to design a screen for all the devices, so I have decide to use Size Classes + Auto Layouts in Xcode 7.2, but it has been 2 days to make it work, but all in vain.
I have googled and learnt a number of tutorials but even then not able to figure out what is wrong with my scroll view.
Here is what I am doing:
Dragged a UIScrollView into my UIViewController Class.
Resized scrollview to frame (0,0,600,600) viewcontroller's size.
Selected ScrollView and press on pin icon to add following constraints:
Unchecked constraints to margins checkbox.
Leading - 0, Trailing - 0, Top - 0, Bottom - 0.
Dragged a new UIView into scrollview and added following constraints:
Unchecked constraints to margins checkbox.
Leading - 0, Trailing - 0, Top - 0, Bottom - 0.
Now Dragged from my content view to main view(View controller's View) and selected "Center Horizontally in View" and "Center Vertically in view".
Now added some UI objects to my content view like UIImageView, UILabel, UItextFields and UIButtons and put constraints on them as well. My top most object is attached to content view's top and bottom object is not attached with content view's bottom as if I will do that it will shrink my objects to fit in the screen.
If I run this code everything is looking fine, except that on iPhone 4S I am unable to scroll.
I have tried a lot of things, but nothing worked for me. Could anyone help me on the same.
You have to give the height and a low priority.
Control drag the content view on to the view of the view controller as select the 'Equal Heights' option.
See around 35 secs at this video https://youtu.be/UnQsFlMGDsI?t=35s

iPhone 4 & 5 Autoresize Vertical Space

I built my views in Interface Builder for iPhone 4 dimensions. I thought that the Autoresize Constraints in iOS 6 will take care of the resizing automatically. The screenshot shows the Top Space constraint that Xcode added automatically. It says that the relationship between the top and the bottom orange view must be 332 points.
When viewed in iPhone 5, the orange button is stretched all the way to the bottom as you can see in the screenshot below.
When changing the relation to Greater Than or Equal, the view does not change and remains the same. When removing the constant to make it Auto, the view is stretched across the entire screen. How do I specify a constraint were the height is 44 points and at the bottom?
Don't have a constraint to the top of the superview. Pin it to the bottom of the superview, and fix the height to 44, then delete the constraint linking it to the top of the view.
You achieve the pinning by selecting the view in question and using the pinning menu in interface builder (the floating menu with three icons on it, or editor-->pin from the menu bar).
To move between 3.5 and 4 inch displays, you need to link content to the correct edge of the superview. Stuff at the bottom should be stuck to the bottom.
I have written about constraint editing in interface builder here if you're interested.
Try adding your own contraints (user constraint), In Auto Layout for each object there must always be enough constraints to determine both its position and size on different devices screen.
Interface Builder automatically creates constraints for you as soon as you place a view in a layout. Try placing your views along IB’s automatic guides to help it guess correctly which layout you want.
Prior to Xcode 4 it was done by setting the springs and struts from the Interface Builder and the springs and struts model is still the default, so for every programmatically created view that you want to use autolayout with, dont forget to call
setTranslatesAutoresizingMaskIntoConstraints:NO.