如何在情节提要场景中嵌入自定义视图Xib?

特拉维斯·格里格斯

我在XCode / iOS领域相对较新;我已经完成了一些不错的基于情节提要的应用程序,但是我从来没有在整个笔尖/ xib事情上咬牙切齿。我想对场景使用相同的工具来设计/布局可重复使用的视图/控件。因此,我为视图子类创建了第一个xib并对其进行了绘制:

在此处输入图片说明

正如我在情节提要中所做的那样,我已连接好插座并进行了约束设置。我将自己的类设置为File Owner自定义UIView子类的类。因此,我假设我可以使用一些API实例化此视图子类,并且它将如图所示进行配置/连接。

现在回到我的情节提要中,我想嵌入/重复使用此内容。我这样做是在表格视图原型单元格中:

在此处输入图片说明

我有一个观点。我已经将其类设置为我的子类。我已经为其创建了一个出口,以便可以对其进行操作。

$ 64的问题是,在哪里/如何指示仅将我的视图子类的空/未配置实例放在那里,还使用我创建的.xib对其进行配置/实例化还不够?如果在XCode6中可以输入用于给定UIView的XIB文件,那真是太酷了,但是我没有看到执行此操作的字段,因此我假设我必须在某个地方的代码中做些事情。

(我在SO上确实看到了类似的其他问题,但是还没有发现任何问题只是这个难题的一部分,或者是XCode6 / 2015的最新问题)

更新资料

I am able to get this to kind of work by implementing my table cell's awakeFromNib as follows:

- (void)awakeFromNib
{
    // gather all of the constraints pointing to the uncofigured instance
    NSArray* progressConstraints = [self.contentView.constraints filteredArrayUsingPredicate: [NSPredicate predicateWithBlock:^BOOL(id each, NSDictionary *_) {
        return (((NSLayoutConstraint*)each).firstItem == self.progressControl) || (((NSLayoutConstraint*)each).secondItem == self.progressControl);
    }]];
    // fetch the fleshed out variant
    ProgramProgressControl *fromXIB = [[[NSBundle mainBundle] loadNibNamed:@"ProgramProgressControl" owner:self options:nil] objectAtIndex:0];
    // ape the current placeholder's frame
    fromXIB.frame = self.progressControl.frame;
    // now swap them
    [UIView transitionFromView: self.progressControl toView: fromXIB duration: 0 options: 0 completion: nil];
    // recreate all of the constraints, but for the new guy
    for (NSLayoutConstraint *each in progressConstraints) {
        id firstItem = each.firstItem == self.progressControl ? fromXIB : each.firstItem;
        id secondItem = each.secondItem == self.progressControl ? fromXIB : each.secondItem;
        NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem: firstItem attribute: each.firstAttribute relatedBy: each.relation toItem: secondItem attribute: each.secondAttribute multiplier: each.multiplier constant: each.constant];
        [self.contentView addConstraint: constraint];
    }
    // update our outlet
    self.progressControl = fromXIB;
}

Is this as easy as it gets then? Or am I working too hard for this?

Segev

You're almost there. You need to override initWithCoder in your custom class you assigned the view to.

- (id)initWithCoder:(NSCoder *)aDecoder {
    if ((self = [super initWithCoder:aDecoder])) {
        [self addSubview:[[[NSBundle mainBundle] loadNibNamed:@"ViewYouCreated" owner:self options:nil] objectAtIndex:0]];
    }
    return self; }

Once that's done the StoryBoard will know to load the xib inside that UIView.

Here's a more detailed explanation:

This is how your UIViewController looks like on your story board: 在此处输入图片说明

The blue space is basically a UIView that will "hold" your xib.

This is your xib:

在此处输入图片说明

There's an Action connected to a button on it that will print some text.

and this is the final result:

在此处输入图片说明

The difference between the first clickMe and the second is that the first was added to the UIViewController using the StoryBoard. The second was added using code.

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

自定义视图(xib)在情节提要板上不可见

如何在情节提要中添加自定义UIControl类?

情节提要中的自定义字体?

UINavigationBar自定义颜色,在情节提要板中嵌入了UInavigatonController

如何将ibOutlet从子视图链接到情节提要Xcode中的自定义UIView类

如何使用情节提要板实现自定义表格视图节的页眉和页脚

在情节提要中自定义表格视图单元时出现NSInternalInconsistencyException异常

如何从情节提要或XIB加载iCarousel视图?

如何在情节提要中将自定义字体设置为UILabel

如何在情节提要中使用ViewController的自定义init

如何在两个情节提要板上执行自定义搜索?

无法在情节提要中更改自定义UICollectionViewCell

如何替换/自定义情节提要导航控件中的后退按钮图像

从代码和情节提要中实例化自定义按钮-如何创建init方法

如何在情节中自定义悬停窗口?

情节提要和自定义init

如何在情节提要的触摸栏中添加滚动视图?

如何在一个项目中的两个情节提要之间自定义对象?

如何在QWebEngineView或页面中嵌入自定义QWidget?

如何在rails中自定义状态嵌入json?

使用情节提要板实现自定义视图布局

自定义视图过渡,无需使用情节提要脚本(快速)

为什么在情节提要中看不到我的自定义视图的预览?

在情节提要中,如何制作自定义单元以与多个控制器一起使用?

从情节提要中的外部Xib文件加载视图

从情节提要中的Xib实例化视图进行无限循环

如何在自定义视图类中动态替换自定义子视图?

如何在自定义表格视图单元格中创建自定义视图?

如何在navigationItem titleView中将xib的自定义视图居中