Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 奇怪的autolayout故障-将使用autolayout的自定义视图放置到superview中_Ios_Autolayout - Fatal编程技术网

Ios 奇怪的autolayout故障-将使用autolayout的自定义视图放置到superview中

Ios 奇怪的autolayout故障-将使用autolayout的自定义视图放置到superview中,ios,autolayout,Ios,Autolayout,我对自动布局有一个非常奇怪的问题。当我将一个简单、普通的视图放入superview并以编程方式将约束附加到它时,它的工作方式与我预期的完全相同。但是,当我对本身具有子视图的自定义视图执行相同操作时,约束无法正常工作 我将自定义视图的自动布局设置为: - (void) initialiseSubviews { self.backgroundColor = [UIColor whiteColor]; UILabel * headerLabel = [[UILabel alloc]

我对自动布局有一个非常奇怪的问题。当我将一个简单、普通的视图放入superview并以编程方式将约束附加到它时,它的工作方式与我预期的完全相同。但是,当我对本身具有子视图的自定义视图执行相同操作时,约束无法正常工作

我将自定义视图的自动布局设置为:

- (void) initialiseSubviews
{
    self.backgroundColor = [UIColor whiteColor];

    UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, self.frame.size.width, 20.0)];
    headerLabel.backgroundColor = [self tintColor];
    [self addSubview:headerLabel];

    NSDictionary * viewDict = NSDictionaryOfVariableBindings(headerLabel);
    NSArray * horiz = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[headerLabel]-|" options:0 metrics:nil views:viewDict];
    NSArray * vert = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[headerLabel]" options:0 metrics:nil views:viewDict];
    NSArray * height = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[headerLabel(20)]" options:0 metrics:nil views:viewDict];

    [headerLabel addConstraints:height];
    [self addConstraints:horiz];
    [self addConstraints:vert];
}

+ (BOOL) requiresConstraintBasedLayout
{
    return YES;
}

- (BOOL) translatesAutoresizingMaskIntoConstraints
{
    return NO;
}

- (CGSize) intrinsicContentSize
{
    return CGSizeMake(200.0, 120.0);
}
…并将其添加到内容视图中,如下所示:

- (SSNodeView *) addNodeAtPoint:(CGPoint)point
{

    // Prepare the node view
    SSNodeView * nodeView = [SSNodeView nodeView];
    [self.gridView addSubview:nodeView];

    NSDictionary * viewDict = NSDictionaryOfVariableBindings(nodeView);
    NSArray * width = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[nodeView(>=300)]" options:0 metrics:0 views:viewDict];
    NSArray * height = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[nodeView(>=200)]" options:0 metrics:0 views:viewDict];
    [nodeView addConstraints:width];
    [nodeView addConstraints:height];

    // Set its autolayout properties
    NSDictionary * metricDict = @{@"x":[NSNumber numberWithFloat:point.x], @"y":[NSNumber numberWithFloat:point.y]};
    NSArray * horizontal = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-x-[nodeView]" options:0 metrics:metricDict views:viewDict];
    NSArray * vertical = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-y-[nodeView]" options:0 metrics:metricDict views:viewDict];
    [self.gridView addConstraints:horizontal];
    [self.gridView addConstraints:vertical];

    // Update and layout constraints
    [self.gridView layoutIfNeeded];

    return nodeView;
}
…然而,它始终显示在其原始帧上,该帧未针对自动布局进行调整!如果我切换到一个普通的UIView,并使用完全相同的布局代码,那么在主视图中添加子视图时,无论有无约束,都会出现问题。我觉得我应该做些什么来让自定义布局正常工作,但我不知道这可能是什么

有人有什么想法吗


-Ash

好的,似乎已经发生了变化,覆盖
translates的方法自动调整大小GMaskintoConstraints
不再按预期工作;删除该方法并设置其引用的属性至少可以修复我遇到的一些问题