gpt4 book ai didi

ios - subview 宽度不适合自动布局的 super View

转载 作者:行者123 更新时间:2023-11-29 00:26:37 25 4
gpt4 key购买 nike

我正在向 UICollectionViewCell 添加一个 subview 。我以编程方式添加约束以使其填充单元格,但宽度未填充单元格。

当我在 View 调试器中查看它时,它说位置不明确。这怎么可能,因为我指定所有 4 个边都固定到 super View ?

这是调试器中的 View 。内部的白色 View 应适合父级宽度(蓝色边框):

enter image description here

检查父 View 上的约束会显示“位置不明确”警告:

enter image description here

我使用的代码如下:

[self.contentView addSubview:calloutView];
calloutView.translatesAutoresizingMaskIntoConstraints = NO;
self.contentView.translatesAutoresizingMaskIntoConstraints = NO;

[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[calloutView]-0-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:NSDictionaryOfVariableBindings(calloutView) ]];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[calloutView]-0-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:NSDictionaryOfVariableBindings(calloutView) ]];

最佳答案

问题好像和contentView的使用有关,你必须使用cell本身来实现你想要的,这是代码和工作,查看下图

#import "CollectionViewCell.h"

@interface CollectionViewCell()
@property UIView * testView;
@end


@implementation CollectionViewCell

@synthesize testView;

-(void)awakeFromNib
{
[super awakeFromNib];

testView = [[UIView alloc]initWithFrame:CGRectZero];
testView.translatesAutoresizingMaskIntoConstraints = NO;

[self addSubview:testView];

[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-2-[testView]-2-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:NSDictionaryOfVariableBindings(testView) ]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-2-[testView]-2-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:NSDictionaryOfVariableBindings(testView) ]];

self.testView.layer.borderWidth = 2;
self.testView.layer.borderColor = [UIColor blueColor].CGColor;
}

@end

enter image description here

希望对你有帮助,祝好

关于ios - subview 宽度不适合自动布局的 super View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42849040/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com