Whenever I have a scenario that involves repeating UIViews, like, views in a UIScrollView, I have resorted to creating a .xib
file, then Bundle.instantiateViewController(...)
(or whatever) from the UIViewController
that owns the UIScrollView
.
I found another way (or thought I did!) that involves putting the UIView
above the UIViewController
, ie above the ribbon, inside the Storyboard. So I have something like the following:
Next, I assigned a custom class to this UIView
and thought I could just instantiate it in code. It didn't work as it seems to be a plain UIView
albeit subclassed. I guess I'd need to go via UIStoryboard.instantiateViewController
but then I instantiate the whole thing again, which I don't want. I just want my custom UIView.
My question is: Is the.xib approach the way to go, or what am I missing with the Storyboard approach?
You just need to create an IBOutlet of that view in the ViewController and you can then use it.
But if you need to reuse your view then you'll need to create a xib for it. There is no other way to reuse view
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.