Simultanous scrolling of UIScrollView - ios

Inside my app I have to implement a scenario where i need to make two scroll view follow each other scroll.I searched in stackoverflow ,and i see many answers for that.But here in my case those answers are not applicable because my scrollview is being created in a different way.That means,I have a class named MyScrollView inside which I have a single line of code for creating ScrollView.I am not creating two scrollview like scrollViewA and scrollViewB,Instead have another view inside which i will make call for craeting scrollviews using different object.In tht case how will i compare the scrollviews.

Put tag values to your scroll views. so that you can handle at the time of delegate methods.

Related

CollectionView nested inside Collectionview

I have seen solutions where a collection view is nested inside a table view but for my app I need to have 2 collection views as it makes it easier to do some other things.
So lets call the root collection view VerticalCollectionView which only scrolls vertically and the nested collection view HorizontalCollectionView which only scrolls horizontally. I created them using the Storyboard. Below you'll see the orange is the Vertical with the green Horizontal with a label inside it.
And I have set the delegate & datasource of both collections to the same CollectionViewController.
I distinguish between the 2 different cells by checking which tableview the delegate method is referencing as such
My problem is that HorizontalCollectionView isn't getting instantiated. I have everything working for VerticalCollectionView, the background color, the number of items, etc. In the VerticalCollectionViewCell, I have an IBOutlet referencing HorizontalCollectionView
I have used nested collectionViews in my recent app a lot. at first it didn't work for me but when I learned its away it became pretty simple.
instead of setting the HorizontalCollectionView delegate and dataSource to the same CollectionViewController make a UICollectionViewCell and set the delegates and dataSource in awakeFromNib() function of the cell and write the HorizontalCollectionView functions in that cell. it works like a charm ;)
if you needed more detailed answer let me know.
In case someone is looking here since WWDC 19, Apple has introduced CompositionalLayout for UICollectionViews which make these tasks much much easier.
One should read here:
https://developer.apple.com/videos/play/wwdc2019/215/

How do you handle the case when there is no image for a post?

Let's say one's want to build programatically a feed similar to Facebook's feed, without using storyboards at all, using a Collection View in a View Controller.
Users can write a new post, which would consist in a body, and if they wish, they can add an image as well. So the collection view will be a mix of text posts and image+text posts.
I was wondering what is the best way to handle the case when there is no image to display, how do you handle the height of the collection view cell ?
I created a subclass of collection view cell and inside I added some constraints. Within that subclass I added an "if statement" to check whether the postImageView.image is nil or not and update the constraints/anchors accordingly.
The problem is that it never get called and continue behaving as if there are no images even when there actually are.
It seems like the Collection View doesn't detect the empty images and I'm suspecting it has something to do with the fact I'm using reusable cells.
Do you have any idea of how to deal with the constraints in such a case ?
Thank you very much for your help,
J,

Dynamic uiview layout with auto layout

I'm working with autolayout and its been going very well so far but right now I am at lost as to the way to proceed to achieve the design I want.
I got a a small questionnaire with 2 sections. Each section is embedded in its own UIView with each section's view containing an uiview per question.
My problem is, I want to add the question dynamically, say only show question 2 of section 1 when the user enter a certain input in question 1.
I first attempted to show each section by using using addsubview for the question view and reseting the frame of the section dynamically using setFrame: before realizing this method was not gonna work properly with autolayout (for example, since my questionaire is contained in a scrollview, scrolling will call the loadsubviews method on the scrollview and reset the section's view to their original size...)
What would be the proper way to achieve this? Using a tableview with a variable size perhaps? Or programatically creating layoutconstraint?
What I ended up doing is Dan F suggestion of using a tableview. I used the answer in this great post https://stackoverflow.com/a/12574544/865632 to create static tableview in view container allowing me to have static cell uitableview without having to cluster my main view controller with dummy tablecontroller

How to add multiple instances of custom subviews in UIViewController

Often, when I'm making my apps, I'm in this situation : I have a UINavigationController, handling the view stack, some UIViewControllers, controlling their respective views...
But when I want to add several custom UIViews in my mainView, I don't know how to manage my code.
Each UIViewController needs to handle one and only one view (wich normally occupy all the screen size), and a view should not control their content (update it a the extrême limit).
You can't neither do this :
[myViewController1.view addSubview:childViewController.view];
So if I want to achieve something like this, what should I do ?
The orange parts have to be 3 instances of the same UIView(Controller?), but with a content depending of a NSObject (User, obviously).
I think this very important to segment your content, this should be an easy problem, but I found a lot of contradictory answers so, what's the best practice to handle this common issue?
Theses orange views should be instances of UIViewControllers in order for it to handle their UITableViewDatasource? Is addChildViewController relevant in this case?
I already found a lot of things which work, but I don't know what should I do...
Also, I'm using xibs.
Thanks in advance if you can help me (and other people I think).
You can do it either way (view or view controller) depending on how you want to handle things. Certainly, you can have one object be the data source for multiple tables, so in that case, you would just add multiple views. If, however, you want to keep your code more compartmentalized, then add view controllers, and have each control its own view -- to do this, you do need to use addChildViewController, and use the methods that Apple describes for creating custom container controllers. Alternatively, you can use container views in a storyboard which makes the process of creating custom container controllers simpler.
You're on the right path... Create separate instances of your subviews, and add them to your view. If you will have more than 3 (for instance, imagine coverview for your music, and you could scroll indefinitely left and right), I'd take a look at UICollectionViewController ... That will help manage cell re-use.
But, if it's just 3, just create three instances with different frames and add them to your view.
Here's how I'd do it:
each orange box will be a custom view (inherits from UIView)
the view will have the label, image and the tableview.
since you are not sure of the number of instances of these views you'd be using, its better to use some kind of tagging, so that you can have one place for the datasource and delegate methods of the tables in these orange views.
in the datasource and the delegate methods, you can make use of the tableView.tag (same as the orangeView.tag property).
I personally dislike having more than one viewController in a view (except the splitVC), probably because I haven't had a such requirement.
I dont see how a uiviewcontroller for orange box would help, over a uiview.
as #James Boutcher mentioned in his answer, UICollectionViews will simplify this issue further.
Why not creating a UIView class and overriding the drawRect method and then adding subView for this class in your myViewController1.view

Embedding a UIScrollView with a UITableView

So Path uses this type of page where there is a view above their customized looking table that is a background photo, which contains some user info among other things. I'm trying to recreate something very similar to this.
So lets say that I hypothetically wanted to make a view that shows exactly the way the Path app does, but instead of that weird customized version of a table view that they have, there is an actual table. How would I do something like this? The reason why I would need there to be another UIScrollView embedded into the view is because the entire thing needs the capability to scroll. I'm trying to be as detailed as possible, but its a little difficult to explain.
What I'm imagining is going to happen if I just tried it right now, is that I'd embed a UIView above a UITableView within a UIScrollView that's the size of the frame, and when I'd go to scroll, the user would only scroll the UITableView, and not the entire thing at once. Hopefully that helps convey my doubts.
Another possibility is that I'm totally over thinking this, and I can simply just subclass a view in the header of a UITableView and it would stretch the width and height that I'd like. Hopefully this is the way as this would be easy!
Anyways, can anybody weigh in on this?
Path just uses a normal UITableView with UITableViewStyleGrouped.
The custom view at the top is the header of the first section of the table.
They also access the UIScrollViewDelegate method of the UITableView to change the look of the view (I think the image is moved) when the scroll view scrolls.
If you'd like a tableview that only scrolls within a part of the view and other stuff above it then you need to use a UIViewController. Then you can make it conform to UITableViewDelegate and UITableViewDatasource and add a UITableView and make the view controller the datasource and delegate.
Then you can also add a UIScrollView to it as well.

Resources