remove gap when a subview is deleted in scrollview - ios

In Objective-C, I have a UIScrollView in which multiple subviews (created as xib) are placed. When I click the delete button of subview I want to delete this particular clicked subview alone and rearrange the scroll view without any gap of the deleted subview..
I have successfully removed the subview using the corresponding tag of subview, but I want to arrange the views in scrollview without the gap
How can I achieve this?

If you're using auto layout, you'll want to create variable references to your constraints of these subviews. Then when the delete action is performed, update the constraint values to move the other subviews into the correct placement.

You have 2 options to use achieve your goal.
1. Add UIView in UIScrollView Programmatically.
Code structure :-
Add a view1 on UIScrollView than increase content size of ScrollView according to view1 hight.
Remove a view1 from UIScrollView than decrease content size of ScrollView according to view1 hight.
2. Auto Layout.
According to me "2" option better approach. You can use prefer tutorial link for how to add a view on scrollview with Auto Layout. Using UIScrollView with Auto Layout in iOS

Related

Dynamically grow UIView containing a non-scrollable vertical UICollectionView

I am looking for a way to build a UICollectionView with a vertical flow layout that is embedded in a UIView. the UIView acts as the datasource/delegate for the containing collectionview. The behavior I am looking for is that the collectionview itself has its scroll disabled and the containing UIView grows as you add more rows to the collectionview. How can this be achieved?
I think you can achieve this by following steps,
Embed the UIView which you currently have into a UIScrollView(Make its constraint respective to its super view).
Then add height constraint to your UIView and create an outlet for that height constraint in our view controller, so as of now when the height of UIView gets increased the scrollView content size will also get increased.
Then place the UICollectionView inside the UIView and make its top, bottom, left & right constraints to UIView.
Now increase the height constraint constant value based on the number of collection view cells in the collection view.
I hope this will work for your scenario.
But if in case you are free to explain why you need to achieve this kind of behaviour it would be nice, because I cannot able to figure out the need for it.
This kind of scenario could be achieved. Here is rule of the thumb:
You should not specify height of you view, that will be contains unscrollable collection, pinned to it, and it should be embedded in UIScrollView using equals width, and pinned to all sides with margin. Auto layout makes things work.
Otherwise, is there any important reason to disable scrolling and make UIView delegate / data source for collection?

Embedding a horizontal scrollview in storyboard

how would I go to embed a horizontal scrollview as shown on the image below. I've put a collectionview as my buttons that should be marked when the user is on either "page" of the scrollview. What I'm stuck on is the scrollview part. I put the scrollview where it should be and then my plan is to put a new view on it thats width is scrollview.bounds.width * 2. Then I'd put whatever I need on each "page" of that view. Is there a way to do it from storyboard ? Thanks.
ps here is the imgur link if image isn't shown http://imgur.com/a/Yu7r9
Yes, you can do this. First add a UIView inside your UIScrollView and make sure it sticks to all sides using Auto Layout. Then explicitly set the width of the UIView (your 'content view') to any value you like. You can also keep a reference to this constraint so you can change it later in code. Then you can add your subviews to this 'content view' and you should be good to go.

Do I mandatory add views in UIScrollView in order to scroll those views?

I have used auto layout and after designing screen I need to scroll all the contents. If I add all the screen views inside a new scrollview, all my constraints will not work. So whats the easiest way doing this?
The Easy way is ,
Take One scrollview in Mainview then add another view(Contentview) in scrollview and then add all the content in content view. so, you can able to scroll contentview. and when your contentview will be scroll then all control will goes up and down. Due to contnent view.
Your hierarchy will be something like this ,
Mainview
Scrollview
ContentView
Textfield
button
Label
On below link , You will find best tutorial regarding
Set Constrain to Scrollview with Autolayout with Example
and last but not the lease ,
Don't forget to untick Adjust Scrollview insets in viewcontroller property.

UIScrollView Getting stuck

I am working on application in which I have UIScrollView which contains 3 subViews(UIView) and each subView has different controls & has different height. This all works fine but when I added constraint to UIScrollView and subView then UIScrollView isn't working.
I also added constraint to all the controls in subviews.
So please suggest me how to solve this problem?
It happened because UIScrollView content height and width dynamic at run time.
So whenever you apply constraint on UIScrollView then add
1.X position
2.y Position
3.width
4.height
Take a View as SubView on UIScrollView now apply Constraint
1.Leading on UIScrollView
2.Trailing on ScrollView
3.Top
4.Bottom
5.Height
6.Width
Then it work fine
For constraints to work properly,
You need to add a child view to UIScrollView and add your 3 views to that child view.
The main UIView and child view should have equal width constraint set.
For reference see this

Using Auto layout with hidden views

I have a view controller with a UIScrollView. Inside that scrollview I have two UIViews. viewA sits onto of viewB. I am using Xcode 5 and auto layout in this project. What I would like to do is when viewA is hidden, move viewB to the top where viewA was sitting (technically, still sitting, just hidden).
I looked at this SO answer: How to use auto-layout to move other views when a view is hidden?
However it hasn't really helped me. I connected a IBOutlet which was a constant to the vertical spacing to the top of the scroll view and set it to 0.0f when viewA was hidden. However it makes no changes to my UI at all.
First get the Top Space to SuperView NSlayoutConstraints Outlets for both subViews and then do the following:-
[self.aView setHidden:YES];
if([self.aView isHidden])
{
self.bViewTopConstraint.constant = self.aViewTopConstraint.constant;
}
using this the second UiView will go to the place of first UIView.
For Scrollview you have to set the constraints value properly. No need to set any contentsize. Once you set the constriants scrollview will work automatically.
Check the attached screenshot.

Resources