Problem with initWithFrame when subclassing UIView in Swift

I have an Objective-C class UIView:

ChoosePersonView.h

@class Person;

@interface ChoosePersonView : MDCSwipeToChooseView

@property (nonatomic, strong, readonly) Person *person;

- (instancetype)initWithFrame:(CGRect)frame
                       person:(Person *)person
                      options:(MDCSwipeToChooseViewOptions *)options;

@end

MDCSwipeToChooseView.m

@class MDCSwipeToChooseViewOptions;

@interface MDCSwipeToChooseView : UIView

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIView *likedView;
@property (nonatomic, strong) UIView *nopeView;

- (instancetype)initWithFrame:(CGRect)frame
                      options:(MDCSwipeToChooseViewOptions *)options;

@end

Normally I would subclass and use it as shown below:

ChoosePersonView.m

@implementation ChoosePersonView

#pragma mark - Object Lifecycle

- (instancetype)initWithFrame:(CGRect)frame
                       person:(Person *)person
                      options:(MDCSwipeToChooseViewOptions *)options {
    self = [super initWithFrame:frame options:options];
    if (self) {
        _person = person;
        self.imageView.image = _person.image;

        self.autoresizingMask = UIViewAutoresizingFlexibleHeight |
                                UIViewAutoresizingFlexibleWidth |
                                UIViewAutoresizingFlexibleBottomMargin;
        self.imageView.autoresizingMask = self.autoresizingMask;

        [self constructInformationView];
    }
    return self;
}

...
@end

The problem I am facing is when I try to do this in Swift, the property is self.imageViewalways nil.

I'm not sure how to reimplement initWithFramehow it is implemented in Objective-C in Swift. I thought the use init(frame: CGRect, person: Person, options: MDCSwipeToChooseViewOptions)was correct, but I think I could be wrong.

ChoosePersonView.swift

class ChoosePersonView: MDCSwipeToChooseView {
    var informationView: UIView!
    var nameLabel: UILabel!
    var cameraImageLabelView: ImageLabelView!
    var interestsImageLabelView: ImageLabelView!
    var friendsImageLabelView: ImageLabelView!
    var person: Person!
    let ChoosePersonViewImageLabelWidth = CGFloat(42.0)

    // #pragma mark - Object Lifecycle

    init(frame: CGRect, person: Person, options: MDCSwipeToChooseViewOptions) {
        super.init(frame: frame)

        self.person = person
        self.imageView.image = self.person.image // self.imageView is nil.
        self.autoresizingMask = .FlexibleHeight | .FlexibleWidth | .FlexibleBottomMargin
        self.imageView.autoresizingMask = self.autoresizingMask

        self.constructInformationView()
    }
    ....
}
+4
source share
1 answer

self.imageView - , , . , , , , .

, MDCSwipeToChooseView, super.init(frame:options:), imageView. , . , , , ChoosePersonView Objective-C. , , Swift? - - super.init(frame:). . , , .

, - , imageView nil, nil, Objective-C.

+1

Source: https://habr.com/ru/post/1547684/


All Articles