I have a UIViewController with an UIScrollView in it.
Then I created a new UIView subclass with some properties that I want to programmatically add in this UIScrollView until my data ends.
Something that should look like this picture:
I have read other topics that does that by creating a view and setting its class to the custom view subclass, but I want to do it programmatically and its not working.
how would I do that?
From your image it looks like you’re looking to load views from a nib and add them as subviews of your UIScrollView. To do this have a look at the UINib documentation.
You want to create your nib and set it’s main view to be an instance of your UIView subclass then load the nib in
viewDidLoadof your viewController, and add the nib’s views as subivews of your scrollview (which I’m assuming is a subview of your viewController’s view).You can instantiate a nib with
instantiateWithOwner:options:.To get the array of views from a nib you do something similar to:
I’ll assume we’re inside a UIViewController and we’re somewhere in (or after)
viewDidLoad. You would then use the array from above and add the views as subviews of your scrollview. You may need to set the frames of these views to place them properly, but that should be trivial.I hope that sets you in the right direction.
Edit:
If you want more information you may need to read deeper into how nibs work to manage your expectation. Linked with the UINib documentation is the ‘Resource Programming Guide’ in particular the nib section