Is there a way to keep subviews of a UIStackView participating in autolayout when they're hidden? - ios

I introduced some UIStackView's in my latest project, because it made the spacing of the views and adding other autolayout constraints a lot easier.
Only to discover that hidden views inside a UIStackView no longer 'participate' when autolayout does its thing.
I suppose this is often a great feature, but for this particular view I don't want that, is there a way to have the subviews of a UIStackView behave as if they were embedded in a plain UIView?
Or do I have no option but to resort to removing the UIStackViews? (and adding a whole lot of annoying 'spacer' views and constraints)

It is by design that hidden arranged subviews are not only hidden but no longer contribute to the layout either. It's a major feature that cannot be achieved easily with auto layout.
If you want to prevent this, then you can wrap your view within another view. Instead of hiding the direct subview of the UIStackView (the wrapper view in the new setup), hide the inner view (the same way as in the old setup except it is now nested). As the direct subview is visible, UIStackView won't reclaim the space. But the user can't see any content as the view content is hidden.

Instead of hiding the subview, you can just turn its alpha to 0. This way the subview won't be visible but it will participate in the layout.

Related

Horizontal scroll view not scrolling

I've just vertical scrolling a few times in the past, but now I'm trying to implement horizontal scrolling but when I run it it doesn't scroll.
What is the problem with it?
P.S. is it possible to design and layout views using a storyboard for scrolling? In this example the blue view is still visible within the screen representation, so its ok to deal with, but suppose I wanted to add another view which is further to the right and thus not visible within the screen representation? Is there anyway of visually designing a scroll view where you can see all the entirity within the storyboard what it will look like?
You don't have a trailing constraint on the scroll view's direct subview, so the scroll view cannot compute the correct contentSize.
Also, it's easier to understand the constraints in the document outline if you give each view a unique label. You have two views labelled “View” so it's difficult to be sure which constraints connect to which “View”.

How to setup a UIScrollView to arrange content dynamically?

I am trying to create a user profile screen. The screen will have lots of information about a user. It will look similar to this AirB
From the looks of it, it seems to be some sort of scrollView? or is it a tableView or a collectionView? (because it seems that there are some rows in there i think)
Does any one here know how this type of view can be accomplished or can be setup?
EDIT two answers below says to use a UITableview, and 2 other answers says to avoid it. Are there any benefits/disadvantages to using either?
Use only one child UIView as i will call it ContainerView inside UIScrollView and then place your child views inside that ContainerView. Use constraint to trailing and leading and top and bottom of that ContainerView to UIScrollView. and use constraint for placing child views to viewController.view not to ContainerView to then iOS will find where child views have to be.
Good Tutorial to find out how to use constraint on scrollView
Apple Technical Note
Do Not use tableview in this case, this content is difficult to maintain in a table view, really, use a scrollview, as for how, a simple googling for "swift uiscrollview tutorial"
Tableview is the best solution for this kind of scrolling views but in the screen you have mapview if it updates every moment i.e. updates with current location then it is not recommended to use tableview.
In that case you can use scrollview with custom views add subview changing with parameters.
In one of my app i did same thing using Tableview. And for that tableview i created a custom cell which have such a big height and i added all the images,map,details of user in that. And it work similar to what you want.
If you want help reply to my answer

Matching widths for Autolayout views in separate hierarchies

I am trying to build a spreadsheet like app which has a scroll view and "floating" headers on the top and left which do not scroll with the rest of the content. I have this implemented in a ScrollView using layoutSubViews to override the frames of the header UIViews, with all the scrolling in a single UIView:
ScrollView
Top Header UIVew
Left Header UIVew
Content UIView
Content
This is all working great, except where the items in the header need to line up with the main content (i.e., "W:cell1==topHeader1", "H:cell1==leftHeader1", etc.). Autolayout won't allow constraints since the items are not in the same hierarchy. How do I keep the widths/heights the same?
I think it's better implementing this scenario using UICollectionView, if the default UICollectionViewFlowLayout won't satisfy your requirement subclass it or even create a whole new custom layout.
Such scenario is definitely for CollectionView, it will add reusability for the visible cells. So that if you have hundreds of rows/columns, you don't use extra memory.

Using vertical UIScrollView with horizontal paging

So, what I need: 3 pages (with swipe support): two of them just fit the screen, third one must have vertical scrolling.
1) Can you provide what bunch of controls I need to use: ScrollView with vertical scrolling inside parent ScrollView with paging enabled? Or ScrollView with PageControl?
2) How to adjust vertical ScrollView. I've read bunch of question here, and tried to do that by myself.
I tried to add 4 constraints for ScrollView (pin it to parent edges), then I add child UIView, also pin it to Scroll edges. Then I add some rectangles to child view, and using different settings I got: just horizontal view, horizontal+vertical scroll, only vertical scroll but with top space from UINavigationBar and with clipped some height from child rectangles (I can't scroll to them).
Can someone provide some example of how to do that? I prefer IB for constraints, but if it's necessary to calculate some math in code - it's ok.
Thanks
When paging between view's like that, I would suggest using a UIPageController (You can easily get a sample application by creating a new project and selecting "Page Based Application"). Change the transition style from Page Curl to Scrolling and there is the functionality you need for paging between view controllers. Now you just can place a scroll view in your third view controller and make sure the attributes are as follows. DirectionLockEnabled is the key to what you are trying to do, as it determines if scrolling is disabled in a particular direction (this case horizontal scrolling). Using this type of solution, it is then really easy to set constraints because you're setting constraints for 3 separate view controllers rather than 3 views within a scroll view
edit: disable "Shows Horizontal Indicator" Checkbox also

Rearrange UIView Subviews if a Subview is removed in Auto Layout

I am in a need of implementing a drag and drop controller in Full-Auto Layout environment.
I have achieved the dragging and dropping successfully, but the issue is if I drag a subview from another view the other related subviews behave weirdly,
as an example the subviews are laid out horizontally in scrollview are related to each other, so if I remove a subview the other views should rearrange them self automatically.
Regards,
Dhanesh.
when you doing the design part of xib uncheck the autolayout.
After completing the designing then go towards to autolayout and arrange the view using constraint adding.

Resources