ios::ViewCore¶
Base class for iOS view cores.
This effectively wraps UIView and serves as a foundation for all other iOS view cores.
Declaration¶
namespace bdn::ios {
class ViewCore : public bdn::View::Core, public std::enable_shared_from_this<ViewCore>
}
Construction¶
- ViewCore(const std::shared_ptr<bdn::ViewCoreFactory> &viewCoreFactory, id<UIViewWithFrameNotification> uiView)
- void init() override
Parent/Child relations¶
-
bool canMoveToParentView(std::shared_ptr
newParentView) const override Always returns true on iOS as
UIViews can move to any parent view. -
virtual void addChildViewCore(const std::shared_ptr
&core) - virtual void removeFromUISuperview()
Geometry¶
-
virtual void frameChanged()
Updates the geometry Property based on the UIView's frame.
-
virtual void onGeometryChanged(Rect newGeometry)
Updates the UIView's frame to the
Rectgiven innewGeometry.
Layout¶
-
Size sizeForSpace(Size availableSpace = Size::none()) const override
Uses UIView::systemLayoutSizeFittingSize to calculate the preferred Size of the
View. -
void scheduleLayout() override
Triggers the
setNeedsLayoutmethod of the managedUIView. -
virtual bool canAdjustToAvailableWidth() const
Override to influence how
sizeForSpacebehaves. -
virtual bool canAdjustToAvailableHeight() const
Override to influence how
sizeForSpacebehaves.
Helper¶
-
template <class T> std::shared_ptr<T> shared_from_this()
Returns
shared_from_thiscasted tostd::shared_ptr<T>usingstd::dynamic_pointer_cast<T>. -
UIView *uiView() const
Returns the
UIViewthat was passed to the constructor.
Reporting Size Changes to the Core¶
The Boden Framework supports native layout updates from the target platform. This is especially useful in cases where native widget layouts should be reused to eliminate the need for custom non-native layout implementations, e.g. in list views (UITableView on iOS).
When an iOS view changes its size, the framework therefore needs to be informed so it can update the view's geometry property.
To accommodate for native layout updates, ios::ViewCore and all subclasses wrapping native UIViews must implement the UIViewWithFrameNotification protocol:
@protocol UIViewWithFrameNotification
- (void)setViewCore:(std::weak_ptr<bdn::ios::ViewCore>)viewCore;
@end
The ios::ViewCore constructor will call setViewCore on its native UIView instance. The UIView subclass then needs to call back the core's frameChanged method when its frame changes. For instance, this is the implementation used by ios::ButtonCore:
@interface BodenUIButton : UIButton <UIViewWithFrameNotification>
@property(nonatomic, assign) std::weak_ptr<bdn::ios::ButtonCore> core;
@end
@implementation BodenUIButton
- (void)setViewCore:(std::weak_ptr<bdn::ios::ViewCore>)viewCore
{
self.core = std::dynamic_pointer_cast<bdn::ios::ButtonCore>(viewCore.lock());
}
- (void)setFrame:(CGRect)frame
{
[super setFrame:frame];
if (auto core = self.core.lock()) {
core->frameChanged();
}
}
//...
@end