UIView displaying outside superview, but is apparently within bounds

Discussion in 'iOS Programming' started by moonman239, Sep 24, 2015.

  1. moonman239 macrumors 68000

    Joined:
    Mar 27, 2009
    #1
    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.
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    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.
     
  3. Hagrid macrumors member

    Hagrid

    Joined:
    Jul 13, 2005
    Location:
    New Jersey
    #3
    I agree, much easier to start in the storyboard and then tweak if necessary.
     
  4. moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #4
    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.
     

Share This Page