问题与自动版式与自定义视图中的NIB文件中定义,并在脚本中使用 [英] Issues with AutoLayout with Custom Views defined in NIB file and used in StoryBoard

查看:123
本文介绍了问题与自动版式与自定义视图中的NIB文件中定义,并在脚本中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个NIB文件中定义的自定义视图,并想在故事板实例化一个副本,但我在使用自动布局的问题。

I've defined a custom view in a NIB file and would like to instantiate a copy in a StoryBoard but I'm having issues with autolayout.

在一个简单的例子,自定义视图有一个固定大小的单个标签和中心垂直和水平,全部采用自动布局。

In a simple example, the custom view has single label with a fixed size and centered both vertically and horizontally, all using autolayout.

文件所有者设置为我的类,它有一个出口的俯视图。在自定义视图实现我做的:

The file owner is set to my class, it has an outlet to the top view. In the custom view implementation I do:

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

现在,在我的故事板,我添加了一个UIView,设置它的类来我的自定义类和布局它的大小,居中我的网页上,再次使用自动布局。

Now, in my storyboard, I add a UIView, set it's class to my custom class and layout it out sized and centered on my page, again using autolayout.

和我的小部件的位置和大小正常,但它是如下图所示的内容不调整:

And my widget is positioned and size properly but it's content is not resized as illustrated below:

我试图从笔尖,沿东西线加载后加入更多的约束:

I tried adding more constraints after loading from the NIB, something along the lines of:

UIView* subV = self.topView;
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(subV);
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[subV]|" options:NSLayoutFormatAlignAllBaseline metrics:nil views:viewsDictionary]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[subV]|" options:NSLayoutFormatAlignAllBaseline metrics:nil views:viewsDictionary]];

但是,这会导致无效的布局约束。

But this causes invalid layout constraints.

如何得到任何想法这个工作?

Any ideas on how to get this to work?

干杯!

推荐答案

从加载NIB创建的视图需要被告知不自动调整大小蒙版转换的约束,即使NIB的内容实际上是与自动布局创建启用。

The view created from loading the NIB needs to be told not to convert auto resizing mask to constraints, EVEN if the content of the NIB is actually created with autolayout enabled.

所以加载NIB后,加入它的顶视图到自定义视图之前,我需要调用:

So after loading the NIB and before adding it's top view to the custom view, I need to call:

[self.topView setTranslatesAutoresizingMaskIntoConstraints:NO];

这篇关于问题与自动版式与自定义视图中的NIB文件中定义,并在脚本中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆