ios - Objective-C 在另一个 XiB 中使用自定义 XiB

标签 ios objective-c xcode uiview xib

我真的花了很多时间来寻找解决这个问题的方法。

我有;

CustomView.hUIView 扩展并且也是 IB_DESIGNABLE CustomView.m 用于使用覆盖方法 initinitWithCoderinitWithFrame 实现此 View CustomView.xib 绑定(bind)到具有所有属性的 CustomView 类 而且,我有:

  • CustomTableViewCell.h that extends from UITableViewCell
  • CustomTableViewCell.m that implementation of this cell
  • CustomTableViewCell.xib that bound to CustomTableViewCell class with all properties&outlets.

CustomTableViewCell.xib 包括 CustomView...

没有任何错误,但是CustomView的区域还是空白。

最佳答案

为了查看,您需要做一些变通。

创建一个属性并将您在 XIB 中的 View 链接到它;

@property (strong, nonatomic) IBOutlet UIView *xibView;

然后有这样的东西:

- (void)awakeFromNib{
    [super awakeFromNib];
    [self commonInit];
}

- (instancetype)init{
    self = [super init];
    if (self) {
        [self commonInit];
    }
    return self;
}

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

- (void)commonInit{
    //this will work if the view has the same name as the XIB
    [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil];
    self.xibView.frame = self.bounds;
    [self addSubview:self.xibView];
    self.xibView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

}

基本上,您必须手动加载 XIB 并将其添加到您的自定义 View 中。

关于ios - Objective-C 在另一个 XiB 中使用自定义 XiB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34022382/

相关文章:

iphone - UIView subview 不显示在 UIScrollView 中

ios - Apache cordova/phonegap 如何从 config.xml 启用功能

ios - viewControllerAfterViewController : does not get called with UIPageViewController

ios - 没有父级的 UIWebView loadRequest

Xcode 本地化 : All strings are not translated

ios - xcode 和位智集成

ios - 如何将 UIImage 切成碎片(如披萨)并将每个碎片保存在数组中?

iphone - 不应出现注册屏幕

ios - 无法通过 lldb 评估属性(property)的值(value)

ios - 从外部项目目录读取 plist 文件