Simple UIView Question

Discussion in 'iOS Programming' started by krayziekray, Jun 18, 2012.

  1. krayziekray macrumors 6502

    Joined:
    Sep 24, 2008
    Location:
    127.0.0.1
    #1
    If I am coding an extension to UIView programatically, which function do I add all subviews in the View.m file in order to build the view hierarchy? The drawRect is definitely the wrong function to do this as this can be called hundreds of times and I don't believe the init functions would do this.

    Thanks.
     
  2. Ides macrumors member

    Joined:
    Mar 27, 2012
    #2
    Not Understanding...

    Could you please refrain your question? I can't really understand what you're trying to say. Something about the view hierarchy?

    If you're asking about having the view properly display all of it's subviews, then the answer is: don't worry about it. The UIView class already has methods that will take care of the drawing of subviews, you don't need to write anything extra.
     
  3. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #3
    Does your view itself have any special behavior or is it just a container for subviews? If it is just a container, you should not actually have to subclass UIView. Typically, subviews are added to a view by a view controller. The view takes care of the rest, and the subviews take care of themselves.
     
  4. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #4
    there is a function called layoutSubviews.. if you put your code in there, it should be okay :)
     
  5. krayziekray thread starter macrumors 6502

    Joined:
    Sep 24, 2008
    Location:
    127.0.0.1
    #5
    Hi, so basically I want my view to add and position subviews when it is loaded rather than having to manually call a method to do so. Let's say my view is a container. When it is created I want it to add and position views depending on the values I added in the init function. Would layoutSubviews do this?

    Thanks.
     
  6. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #6
    sir yes sir, this is a class that will get called if you have your own UIView :)
     

Share This Page