Just the other day I found my self working on a project that involved a UIScrollView with horizontal paging enabled (exactly similar to the iOS home screen).
As part of this project, I had to add several independent views to the scrollview at their corresponding offset and realised that I cannot add all of the UIViews I have in one go.
I would have to make a call to
addSubview: for every single view. And I would have to do this N times for N views. That’s pretty bad programming practice isn’t it :p
To tackle this, I came up with this simple category on UIScrollView that takes an NSSet of views, iterates over it, and adds the views as subviews to the UIScrollView.
It is very simple to use:
- Drag and drop the .h and .m files into your Xcode project.
- Include the header in your class
- Add all your views into an NSSet and pass this NSSet to
addSubviews:like the following :
[self.scrollView addSubviews:[NSSet setWithObjects:self.viewAboutUS, self.viewLive, self.viewDigitalPartners, nil]];
Et Voila ! You finally have a simple method to add sub views to a UIScrollView.
The source for this project is freely available at github.