Port subviews of UIView to UIScrollView without breaking constraints? - ios

There's a view that currently has a static view that I would like to be able to scroll, because certain elements are blocked by the keyboard in 4-inch phones. However, I find that if I simply copy all of the existing subviews to be nested under a UIScrollView, the constraints are broken. I would prefer not to have to methodically set up each subview. Is there any way to convert a plain UIView to a UIScrollView?

You can use xib to create a new UIView which nested with your existing subviews, after that,you can add constraints on it as your wish .
Then useing code to add this container view to the scrollview.
I think this method should be worked.

Related

Issue with Autolayout and StackView while using ScrollView

I a trying to create a UIScrollView that fits exactly the contents of its subviews. I do this with a StackView and the scroll view's height is determined by how much content there is in the stack view. Within the stack views, there are views that contain an UIImageView and a UITextView
The UIScrollView starts below the title view, however every time I want to add another view (which it should scroll when the content is bigger than the actual frame) there is an issue with the scroll view's Y position. This works perfectly if I only add UILabels and UITextFields, using the same procedure
How can I do it so I can programmatically add views on the stack view that includes an UIImageView and a UITextView, just as the View Controller with labels and textfields does.
You can download my app project at the following url
https://github.com/francisc112/DescriptionWithImageApp.git
Instead of a scroll view why don't you use a table view and create a custom cell with the stackview and its contents inside the cell and you can dequeue the same cell for adding multiple information.
I was not able to open your project
So I Tried opening your StoryBoard and Re-Viewing your constraints Applied , here is what I have Done Please check in following Link
Link - https://drive.google.com/file/d/1D-dzKsSqpx7dWsI5fYRdAfCMY_vENoIR/view?usp=sharing
View Hierarchy
Note - For such output I will prefer using a TableView instead of StackView as Same output can easily be achieved using TableView

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

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.

Hiding labels in iOS without breaking the view

I know we can use UIStackView in iOS9, but I'm not able to get rid of iOS8 at the moment, so I was wondering if is it possible to hide some labels inside a UIView (plain UIView, UIScrollView and UITableViewCell) and keep the rest of the visible labels "stacked".
Basically I've got some labels "stacked" vertically and pinned to each other by autolayout. If I hide any of them I get an empty space where the label was placed in interface builder.
I've managed to emulate the stackView's behavior using OAStackview, Following these steps:
Subclass ViewController using a UIScrollView and a OAStackView property.
Put the stack view inside the scroll view
Set constraints for these properties programatically
Add a bunch of custom labels to the stack view.

Advice on how to use a UIScrollView

Hello I have some app (from book) which looks like this:
You can see the image is cropped. I want to have a UIScrollView
which allows to scroll up and also see the whole picture.
This is how my app looks in interface builder.
What do you suggest where should I add ScrollView (as which subview etc.) to achieve what I need? (I guess I will need some intermediary UIView?)
This is what I usually do for fixed contents:
In IB create your view with a UIScrollView at your desired size and position connected to a scrollView outlet.
Also in IB create the view for the contents (bigger than the scroll view). Connect this to a UIView outlet called contentView.
In viewDidLoad do this:
// Put the content view into the scroll view
[self.scrollView addSubview:self.contentView];
self.scrollView.contentSize = self.contentView.bounds.size;
If you want dynamic contents then programatically lay them out in viewWillLayoutSubviews and set the contentSize of the scrollView.
Add the UIScrollView as a direct child of Control, and then put everything else inside the scroll view.
Add UIScrollView as first child of your main View and put all of your controls (UITextView, UILabel,...etc ) as sub view of UIScrollView.
And use UIScrollView property contentSize for set your visible area.
For more information read this tutorial.
You should add the UIScrollView directly under Control, then everything else, besides the toolbar should go into it.
What I usually do in these cases is set the scrollView to the size so all content fits, add the content, then resize the scrollview so it only occupies as much of the screen you want.

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