objective c - constraints cause an ambiguousLayout -
i have nswindow
instance have setupui
method.
-(void) setupui { nsview* subview = [[nsview alloc] initwithframe:nszerorect]; subview.wantslayer = yes; subview.layer.backgroundcolor = [[nscolor greencolor] cgcolor]; [self.contentview addsubview:subview]; [self.contentview settranslatesautoresizingmaskintoconstraints:no]; [self.contentview addconstraint:[nslayoutconstraint constraintwithitem:self.contentview attribute:nslayoutattributeleading relatedby:nslayoutrelationequal toitem:subview attribute:nslayoutattributeleading multiplier:1 constant:0]]; [self.contentview addconstraint:[nslayoutconstraint constraintwithitem:self.contentview attribute:nslayoutattributetop relatedby:nslayoutrelationequal toitem:subview attribute:nslayoutattributetop multiplier:1 constant:0]]; [self.contentview addconstraint:[nslayoutconstraint constraintwithitem:self.contentview attribute:nslayoutattributetrailing relatedby:nslayoutrelationequal toitem:subview attribute:nslayoutattributetrailing multiplier:1 constant:0]]; [self.contentview addconstraint:[nslayoutconstraint constraintwithitem:self.contentview attribute:nslayoutattributebottom relatedby:nslayoutrelationequal toitem:subview attribute:nslayoutattributebottom multiplier:1 constant:0]]; }
when call [self.contentview hasambiguouslayout]
, returns yes. why happened? code snippet seems same xib has created while using pin panel.
and subview green color doesn't appear in window.
thanks in advance.
the property of translatesautoresizingmaskintoconstraints of nswindow's contentview default yes. can't guess happen knowledge of frame, autoresizingmask, autolayout if set no because behavior have tried makes me terribly confused.
in word, leave top contentview's translatesautoresizingmaskintoconstraints yes, of other subviews no if want use autolayout.
when create view, constructed autoresizingmask of 0 , translatesautoresizingmaskintoconstraints of yes. if translatesautoresizingmaskintoconstraints yes, use frame, autoresizingmask , constraints(autolayout) position view.
consider situation below.
nsview* subview = [[nsview alloc] initwithframe:nsmakerect(100, 100, 200, 200)]; subview.wantslayer = yes; subview.layer.backgroundcolor = [[nscolor greencolor] cgcolor]; [subview settranslatesautoresizingmaskintoconstraints:yes]; [self.contentview addsubview:subview]; [subview addconstraint:[nslayoutconstraint constraintwithitem:subview attribute:nslayoutattributewidth relatedby:nslayoutrelationequal toitem:nil attribute:nslayoutattributenotanattribute multiplier:1 constant:50]]; [subview addconstraint:[nslayoutconstraint constraintwithitem:subview attribute:nslayoutattributeheight relatedby:nslayoutrelationequal toitem:nil attribute:nslayoutattributenotanattribute multiplier:1 constant:50]];
the translatesautoresizingmaskintoconstraints of subview yes. use point(100, 100) position it. use size(200, 200) size it. create nsautoresizingmasklayoutconstraint make unresizable because default autoresizingmask 0 means view can't change width , height. create nslayoutconstraint autolayout because have called "addconstraint". , break nslayoutconstraint because nslayoutconstraint , nsautoresizingmasklayoutconstraint conflicted. terribly terrible.
but if call [subview settranslatesautoresizingmaskintoconstraints:no], not use point, size, nsautoresizingmasklayoutconstraint, use nslayoutconstraint autolayout. because size it, not position it. it's ambiguous layout. terribly terrible.
the code below true.
nsview* subview = [[nsview alloc] initwithframe:nsmakerect(100, 100, 200, 200)]; subview.wantslayer = yes; subview.layer.backgroundcolor = [[nscolor greencolor] cgcolor]; [subview settranslatesautoresizingmaskintoconstraints:no]; [self.contentview addsubview:subview]; [subview addconstraint:[nslayoutconstraint constraintwithitem:subview attribute:nslayoutattributewidth relatedby:nslayoutrelationequal toitem:nil attribute:nslayoutattributenotanattribute multiplier:1 constant:50]]; [subview addconstraint:[nslayoutconstraint constraintwithitem:subview attribute:nslayoutattributeheight relatedby:nslayoutrelationequal toitem:nil attribute:nslayoutattributenotanattribute multiplier:1 constant:50]]; [self.conteview addconstraint:[nslayoutconstraint constraintwithitem:subview attribute:nslayoutattributeleading relatedby:nslayoutrelationequal toitem:self.contentview attribute:nslayoutattributeleading multiplier:1 constant:0]]; [self.conteview addconstraint:[nslayoutconstraint constraintwithitem:subview attribute:nslayoutattributetop relatedby:nslayoutrelationequal toitem:self.contentview attribute:nslayoutattributetop multiplier:1 constant:0]];
i think cocoa's layout such terrible thing, many reasons.
in qt, qhboxlayout can easily. not tell me there nsstackview in cocoa, feel tired debug when goes wrong without rhyme or reason.
but have work cocoa.
is there can tell me why can't set contentview's translatesautoresizingmaskintoconstraints no?
thanks in advance.
Comments
Post a Comment