Brendan McLaughlin
Brendan McLaughlin

Reputation: 33

Strategy for creating and utilizing reusable components in a purely programmatic swift ios application? (UIKit not SwiftUI)

I've been playing around with a few different ways to create really reusable/extensible iOS apps via "components" using the programmatic approach (swift only, no storyboards).

Coming from a React background I find component-based strategies really intuitive and scalable.

After trying to replicate this in iOS I've found that creating custom UIViews/UIStackViews (with their own customizable UI components within) and initializing those views within the different view controllers has worked well, but have a hunch there must be battle-tested strategies for doing this that I might be unaware of.

I'm wondering how others have approached "component-based" strategies in programmatic swift / iOS app, and what architectural patterns they might be using to accomplish this.

Upvotes: 1

Views: 1247

Answers (1)

InkGolem
InkGolem

Reputation: 2762

What you described is the best way. If you look at Apple's strategy for UIKit you see UIView which as a component isn't terribly useful, but its subclasses are. UIScrollView, for example, is the foundation of all scrollable content, including UITableView which is arguably the backbone of iOS.

This pattern of breaking components down into logical behaviors and then extending those behaviors as needed is what UIKit is based on.

You might think that SwiftUI does this kind of component based architecture better, especially with how similar to React it is, but it's mostly the same. The way its' different is SwiftUI has a different layout system that allows a view to size itself which makes it very easy to fill with arbitrary data, which in turn makes it easy to reuse. That said, if you inspect the view hierarchy at run time SwiftUI is still 99% UIKit with a new layout algorithm, cool syntax, and heavy use of Combine.framework.

In short, subclass UIKit components to make your own and reuse them. If you're new to iOS, look around before writing your own. Between Apple and GitHub there's an existing component for almost everything. And when you do write your own model it after some of Apple's really generic components like UIPickerView or UIButton. Hope that helps.

Upvotes: 1

Related Questions