Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7488659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:00:30+00:00 2026-05-29T15:00:30+00:00

I have a UIView subclass that programmatically creates and adds a number of subviews.

  • 0

I have a UIView subclass that programmatically creates and adds a number of subviews. However, I want to give users of the class the ability to place new subviews over it (and its subviews) using addSubview and ideally from within Interface Builder as well.

What is the best way to do this?

Now, I suppose it would be acceptable to only add the views programmatically using addSubview, but I’m not sure how I would go about this either.

NOTE: I’ve tried adding additional subviews using interface builder, but they’re hidden once the custom UIView subclass I refered to above creates its other views programmatically, presumably since they’re added last.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T15:00:31+00:00Added an answer on May 29, 2026 at 3:00 pm

    Just drag a generic UIView into a nib in Interface Builder and set its class to your custom subclass. You can still do everything you could with a plain old UIView, including drag-dropping subviews.

    You won’t be able to see what the custom subview actually looks like in Interface Builder; it will just be a gray rectangle. You could have (I think) done this in Xcode 3 with IB plugins, but, at least for the time being, Xcode 4 does not support such plugins.

    EDIT:

    To address the OP’s follow-up, you need to manage z-ordering programmatically. Probably the easiest thing to do is to leave your code largely as-is, but instead of doing something like:

    UIView *mySubview1 = ...;
    UIView *mySubview2 = ...;
    UIView *mysubview3 = ...;
    [self addSubview:mySubview1];
    [self addSubview:mySubview2];
    [self addsubview:mySubview3];
    

    (which, as you noted, may well layer the subviews on top of the user’s subviews)

    Do something like:

    UIView *container = [[UIView alloc] initWithFrame:[self bounds]];
    UIView *mySubview1 = ...;
    UIView *mySubview2 = ...;
    UIView *mysubview3 = ...;
    [container addSubview:mySubview1];
    [container addSubview:mySubview2];
    [container addsubview:mySubview3];
    [self addSubview:container];
    [self sendSubviewToBack:container];
    [container release];
    

    You could alternatively use [self insertSubview:container atIndex:0] but I think that adding and moving to the back is a little clearer during a rapid skim of the code.

    The end result is that you don’t have to mess with the existing z-ordering of your subviews; by putting them in a single, easily-managed container, you can move the whole collection to wherever you would like in the subview hierarchy.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to have a UIView subclass that has a border image, but I
(iPhone SDK 3.x:) I have a UIControl subclass that creates a different number of
I have a UIScrollView subclass that I am programmatically scrolling using UIView animations. I'd
I have a UIView subclass that I want to be 50x50; When I load
I have a UIView subclass that draws itself when -drawRect: is called. It only
If I have a UIView (or UIView subclass) that is visible, how can I
Currently, I have a UIView subclass that stamps a single 2px by 2px CGLayerRef
I have a UIView subclass ( CustomView for purposes of this question) that has
I have a UIView subclass and I want the keyboard to appear when it
I have a subclass s of UIView. I want to put some buttons and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.