Re: Adding Constraints in Code
Re: Adding Constraints in Code
- Subject: Re: Adding Constraints in Code
- From: Iain Holmes <email@hidden>
- Date: Mon, 14 Sep 2015 12:20:24 +0100
> On 14 Sep 2015, at 11:58 am, Iain Holmes <email@hidden> wrote:
>
>>
>> On 14 Sep 2015, at 10:35 am, Dave <email@hidden> wrote:
>>
>> Hi All,
>>
>> I’m trying to add Constraints to a View in order to have it stretch to the left and right edges of the superview. This is slight complicated by it being in a ScrollView/Stack View Combo as so:
>>
>>
>> NSScrollView Setup in NIB
>> NSFlippedClipView Setup in NIB
>> NSStackView Setup in NIB
>> LTWDetailXView (added dynamically in code).
>>
>>
>> The Detail View is smaller in Width than the NSScrollView/NSStackView and I want it to stretch to fit it.
>>
>> I tried adding the following constraints in code as follows:
>>
>> myDetailView = [myDetailViewController getPrimaryView];
>> [self.pValidationListStackView addView:myDetailView inGravity:NSStackViewGravityTop];
>>
>> myConstraintsViewDictionary = [[NSMutableDictionary alloc] init];
>> [myConstraintsViewDictionary setObject:self.pValidationListStackView forKey:@"StackView"];
>> [myConstraintsViewDictionary setObject:myDetailView forKey:@"DetailView"];
>>
>> myConstraintsArray = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[StackView]-(<=1)-[DetailView]" options:NSLayoutFormatAlignAllLeft metrics:nil views:myConstraintsViewDictionary];
>> [myDetailView addConstraints:myConstraintsArray];
>>
>> myConstraintsArray = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[StackView]-(<=1)-[DetailView]" options:NSLayoutFormatAlignAllRight metrics:nil views:myConstraintsViewDictionary];
>> [myDetailView addConstraints:myConstraintsArray];
>>
>> But I get this Error Message:
>>
>> Unable to parse constraint format:
>> Options mask required views to be aligned on a horizontal edge, which is not allowed for layout that is also horizontal.
>> H:[StackView]-(<=1)-[DetailView]
>> ^
>> I’m not sure what this means or if the above code is correct?
>
> The NSLayoutFormatAlignAllLeft says that all the views mentioned in the format string should be aligned along their left edges. Which isn’t possible if you want them to be laid out horizontally.
> You should just use 0 for options.
>
> However: StackView is the parent of DetailView so you should be using | as the superview, so simply doing something like this should work.
>
> myConstraintsArray = [NSLayoutConstraint constraintsWithVisualFormat:@“H:|[DetailView]|" options:0 metrics:nil views:myConstraintsViewDictionary];
> [myDetailView addConstraints:myConstraintsArray];
>
In fact, with NSStackView you should just be able to set the content hugging priority and it’ll just work
myDetailView = [myDetailViewController getPrimaryView];
[self.pValidationListStackView addView:myDetailView inGravity:NSStackViewGravityTop]; // NOTE: Should this not be GravityLeading as you’re using a horizontal stack view?
[myDetailView setContentHuggingPriority:NSLayoutPriorityDefaultLow forOrientation:NSLayoutConstraintOrientationHorizontal];
and that’s all you should need.
iain
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden