Making a view fill its superview

Discussion in 'Mac Programming' started by ATG, Jul 26, 2006.

  1. ATG
    macrumors regular

    Joined:
    Aug 7, 2005
    #1
    I've got a NSView that I'm inserting into another NSView that is in a nib file using:
    Code:
    - (void)addSubview:(NSView *)aView
    That all goes fine and it appears in the IB NSView perfectly. However the subview is stuck to point (0, 0) in the superview when I want it to fill the whole of the superview. I've tried setting the 'springs and struts' of just about everything in IB but it still won't fill the whole view (the subview does resize, it just doesn't fill the whole superview).

    Does anybody know how to solve this? I'll post my app/code if wanted.

    Thanks :)
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    If we assume your sub view and superview then this will more or less work (typed straight into Safari):

    Code:
    NSView *subview = ...
    NSView *superview = ...
    
    [superview addSubview:subview];
    NSRect frame = [subview frame];
    NSRect superFrame = [superview frame];
    frame.size.width = superFrame.size.width;
    frame.size.height = superFrame.size.height;
    [subview setFrame:frame];
    
     
  3. ATG
    thread starter macrumors regular

    Joined:
    Aug 7, 2005
    #3
    Thanks! :)

    I can't test it right now because I'm too tired but tomorrow I will put it in my code.

    Would that live resize the subview as the window is resized so that it is always filling the superview whatever size the window?
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    No. It simply sets the size when you add it. You should still use the springs and struts to control the auto-sizing behaviour.
     
  5. ATG
    thread starter macrumors regular

    Joined:
    Aug 7, 2005
    #5
    Just put it in and it works fine :) There is one interface element that isn't apprearing but I think that's just a spring or strut that I havn't set right.

    Thanks :D
     

Share This Page