Skip to content

Class Overriding

The Face SDK provides a way to register a custom subclass in place of our internal view class with the registerClass(Overriding.Type, forBaseClass: Overriding.Type) API.

This allows you to take more control of what is being displayed to the user. For example, with a custom subclass in our UI module hierarchy, you can react to various life-cycle methods and make changes you want!

Info

The views that support overriding are marked with the Overriding protocol.

LivenessContentView

Here is how you can use this method to customize the layout appearance of the Liveness process.

Suppose you want to move the CameraToolBarView to the top of the screen. For this, you simply need to create your own subclass of the LivenessContentView and provide the setupConstraints method with your own constraints.

final class MyLivenessContent: LivenessContentView {
    override func setupConstraints() {
        // Pin ToolbarView to the top of the content.
        toolbarView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            toolbarView.leadingAnchor.constraint(equalTo: leadingAnchor),
            toolbarView.trailingAnchor.constraint(equalTo: trailingAnchor),
            toolbarView.topAnchor.constraint(equalTo: topAnchor),
        ])

        // Leave HintView default constraints
        hintView.translatesAutoresizingMaskIntoConstraints = false
        let centerConstraint = NSLayoutConstraint(
            item: hintView, attribute: .centerY,
            relatedBy: .equal,
            toItem: self, attribute: .centerY,
            multiplier: 0.68, constant: 0
        );
        NSLayoutConstraint.activate([
            hintView.leadingAnchor.constraint(greaterThanOrEqualTo: self.leadingAnchor, constant: 8),
            hintView.trailingAnchor.constraint(lessThanOrEqualTo: self.trailingAnchor, constant: -8),

            hintView.centerXAnchor.constraint(equalTo: self.centerXAnchor),
            centerConstraint,
        ])
    }
}
// MyLivenessContent.h

#import <UIKit/UIKit.h>

@class RFSLivenessContentView;

@interface MyLivenessContent : RFSLivenessContentView
@end

// MyLivenessContent.m

#import <FaceSDK/FaceSDK.h>

@implementation MyLivenessContent

- (void)setupConstraints {
    self.toolbarView.translatesAutoresizingMaskIntoConstraints = NO;
    [NSLayoutConstraint activateConstraints:@[
        [self.toolbarView.leadingAnchor constraintEqualToAnchor:self.leadingAnchor],
        [self.toolbarView.trailingAnchor constraintEqualToAnchor:self.trailingAnchor],
        [self.toolbarView.topAnchor constraintEqualToAnchor:self.topAnchor],
    ]];

    self.hintView.translatesAutoresizingMaskIntoConstraints = NO;
    NSLayoutConstraint *centerConstraint =
    [NSLayoutConstraint constraintWithItem:self.hintView attribute:NSLayoutAttributeCenterY
                                relatedBy:NSLayoutRelationEqual
                                    toItem:self attribute:NSLayoutAttributeCenterY
                                multiplier:0.68 constant:0];
    [NSLayoutConstraint activateConstraints:@[
        [self.hintView.leadingAnchor constraintGreaterThanOrEqualToAnchor:self.leadingAnchor constant:8],
        [self.hintView.trailingAnchor constraintLessThanOrEqualToAnchor:self.trailingAnchor constant:-8],

        [self.hintView.centerXAnchor constraintEqualToAnchor:self.centerXAnchor],
        centerConstraint,
    ]];
}

@end

Warning

Please note that we do not call super in this example. We do not want the constraints to conflict with the default ones.

After declaring this custom subclass, all we have to do is register it in the LivenessConfiguration and that's it!

let configuration = LivenessConfiguration {
    $0.registerClass(MyLivenessContent.self, forBaseClass: LivenessContentView.self)
}

FaceSDK.service.startLiveness(
    from: viewController,
    animated: true, 
    configuration: configuration,
    onLiveness: { response in
        // ... check response.liveness for detection result.
    },
    completion: nil
)
RFSLivenessConfiguration *configuration = [RFSLivenessConfiguration configurationWithBuilder:^(RFSLivenessConfigurationBuilder  * _Nonnull builder) {
[builder registerClass:MyLivenessContent.class forBaseClass:RFSLivenessContentView.class];
}];

[RFSFaceSDK.service startLivenessFrom:viewController
                            animated:YES
                        configuration:configuration
                        onLiveness:^(RFSLivenessResponse * _Nonnull response) {
// ... check response.liveness for detection result.
} completion:nil];

Info

For more Class Overriding examples, please take a look at the Catalog App. If you need more information about the supported Overriding classes and documentation, see SDK Reference.