美文网首页
oc storyboard中使用xib定义的view

oc storyboard中使用xib定义的view

作者: 喵喵粉 | 来源:发表于2020-03-04 10:43 被阅读0次

1. xib的view

  • .h
#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface AView : UIView

@property (strong, nonatomic) IBOutlet UIView *vContent;
@property (strong, nonatomic) IBOutlet UILabel *lbName;

@end

NS_ASSUME_NONNULL_END
  • .m
#import "AView.h"

@implementation AView

/** 要点:此view的xib中 custom class不设置  File's Owner设置为 AView */
- (id)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
         [self addSubView];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
         [self addSubView];
    }
    return self;
}

/*
 (lldb) po self.vContent
 <UIView: 0x104f1b060; frame = (0 0; 253 169); autoresize = RM+BM; layer = <CALayer: 0x28241d380>>

 (lldb) po containerView
 <UIView: 0x104f1b060; frame = (0 0; 253 169); autoresize = RM+BM; layer = <CALayer: 0x28241d380>>
 */
- (void)addSubView {
    UIView *containerView = [[[UINib nibWithNibName:@"AView" bundle:nil] instantiateWithOwner:self options:nil] objectAtIndex:0];
    CGRect newFrame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    containerView.frame = newFrame;
    containerView.backgroundColor = [UIColor cyanColor];
    [self addSubview:containerView];
}

@end

2. xib

要点:
xib中 custom class 不设置
File's Owner 的 class 设置为 AView ,之后控件可以拖线

image.png image.png

3. storyboard使用自定义的view

image.png

相关文章

网友评论

      本文标题:oc storyboard中使用xib定义的view

      本文链接:https://www.haomeiwen.com/subject/kczplhtx.html