Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

moonman239

Cancelled
Original poster
Mar 27, 2009
1,541
32
I have a view in my storyboard that seems to be acting like it doesn't want to go where I want it to go. In the debugger, on a breakpoint after adding constraints, I ran
Code:
po (BOOL)CGRectContainsPoint((CGRect)[self bounds],(CGPoint)[textField center])
and got a YES response.

Here's the implementation code for the superview class:
Code:
-(instancetype)initWithCoder:(NSCoder *)aDecoder

{

    self = [superinitWithCoder:aDecoder];

    if (self) {

        label = [[UILabelalloc] init];

        [labelsetText:@"blahblah"];

        textField = [[UITextFieldalloc] init];

        [textFieldsetText:@"Mr. M"];

        [labelsetTranslatesAutoresizingMaskIntoConstraints:false];

        [textFieldsetTranslatesAutoresizingMaskIntoConstraints:false];

        [labelsetFrame:CGRectMake(150, 50, 100, 100)];

        [textFieldsetFrame:CGRectMake(50, 50, 100, 100)];

        [textFieldsetClipsToBounds:YES];

        [selfaddSubview:label];

        [selfaddSubview:textField];

    }

    returnself;

}



-(void)updateConstraints

{

    [superupdateConstraints];

    NSDictionary *views = NSDictionaryOfVariableBindings(label,textField);

    NSArray *horizConstraints = [NSLayoutConstraintconstraintsWithVisualFormat:@"H:|-0-[label(textField)]-0-|"options:0metrics:nilviews:views];

    NSArray *lblHeight = @[[NSLayoutConstraintconstraintWithItem:labelattribute:NSLayoutAttributeToprelatedBy:NSLayoutRelationEqualtoItem:selfattribute:NSLayoutAttributeTopmultiplier:1constant:0],[NSLayoutConstraintconstraintWithItem:labelattribute:NSLayoutAttributeBottomrelatedBy:NSLayoutRelationEqualtoItem:selfattribute:NSLayoutAttributeTopmultiplier:12constant:100]];

    NSArray *txtHeight = @[[NSLayoutConstraintconstraintWithItem:textFieldattribute:NSLayoutAttributeBottomrelatedBy:NSLayoutRelationEqualtoItem:selfattribute:NSLayoutAttributeBottommultiplier:1constant:0],[NSLayoutConstraintconstraintWithItem:textFieldattribute:NSLayoutAttributeToprelatedBy:NSLayoutRelationEqualtoItem:selfattribute:NSLayoutAttributeBottommultiplier:1constant:-50]];

    [selfaddConstraints:horizConstraints];

    [selfaddConstraints:txtHeight];

    [selfaddConstraints:lblHeight];

  

}

Will someone please help? Thanks.
 
Why are you doing this layout in code? I would recommend at a minimum, set up the layout in the storyboard until it works the way you want. You can then translate it to code if you really want.
 
So, I stayed up late last night - found a way to get the view looking the way I wanted it to. Along the way, I learned that AutoLayout is kind of pesky, shall we say. You can either translate autoresizing masks into constraints or define constraints to describe exactly where a subview should be relative to the subview's parent.

If Bob is a subview of Alice, and Alice is the view we're designing in code, and you set a width constraint for Bob, you can pretty safely expect that - in the absence of a center constraint on Bob - Bob will maintain a different position than you wanted him to.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.