Reputation: 2257
I am adding a subview to a UICollectionViewCell. I am programmatically adding constraints to make it fill the cell, however the width does not fill the cell.
When I view it in the view debugger, it says the position is ambiguous. How can this be since I am specifying all 4 sides are pinned to the superview?
This is what the views look like in the debugger. The inner, white view should fit the parents width (blue border):
Inspecting the constraints on the parent view shows this with the "position ambiguous" warning:
The code I am using is as follows:
[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) ]];
Upvotes: 0
Views: 355
Reputation: 20804
The problem seems to be related to the use of contentView, you must use the cell itself to achieve what you want, this is the code and works, check the picture below
#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
I hope this helps you, best regards
Upvotes: 1