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 8263545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:08:34+00:00 2026-06-08T04:08:34+00:00

In my app I am trying bring a subview to front, then put it

  • 0

In my app I am trying bring a subview to front, then put it back to its original layer position later. The code should be pretty simple:

To bring the subview to front (inside my custom UIView class):

[self.superview bringSubviewToFront:self];

Easy. I store the original z position in an instance variable called, you guessed it, zPosition. So, the line before -bringSubviewToFront: is:

zPosition = [self.superview.subviews indexOfObject:self];

So, all of the code I use to bring my subview to front is:

zPosition = [self.superview.subviews indexOfObject:self];
[self.superview bringSubviewToFront:self];

This works as it should. The problem is when I try to put the subview back where it was. I’m simply doing this:

[self.superview exchangeSubviewAtIndex:zPosition withSubviewAtIndex:
    [self.superview.subviews indexOfObject:self]];

Using this code, if I have two subviews, this is what happens:

Let’s say I have view A and view B. View A is above view B. I tap view B, it comes to the front. I tap view B again (it should go back to where it was), and nothing happens, so it’s now on view A. If I now tap view A, it comes to the front, but when I tap it again (so it should go back to its original z position: below view B), all of its sibling views disappear!

Does anyone see what could be causing this problem?

  • 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-06-08T04:08:37+00:00Added an answer on June 8, 2026 at 4:08 am

    exchangeSubviewAtIndex may well put the view back in the right place, but it will also swap another view on top, which wont be what you started with. You might need to do something like this instead of exchangeSubviewAtIndex :

    [self retain];
    UIView *superview = self.superview;
    [self removeFromSuperview];
    [superview insertSubview:self atIndex:zPosition];
    [self release];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to bring data into my app from an external file. everything's working
I'm trying to save simple text data in my app using this code from
I am trying to write some c# code to start a browser using Process.Start(app,args);
I'm working on the front end for a web app, but I'm trying to
I'm trying to implement a tabs into my app, but id tabhost is being
So I've been toiling over this app trying to figure out this strange behaviour
I have a Rails app and trying to make a remote form, so far
I'm making a 'post text' app and trying to update my database with name
I have Rails 3.1.3 app and trying to plug gem redis to it. I've
In my app I'm trying to render text along a path; this is fine

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.