0

I have a mainViewController and inside its nib file I added an info button, in which action is to flip between two subviews, subview A and subview B.

From mainViewController, under viewDidLoad, I'm inserting subview A. Here I notice that the info button is in front of the subview A, which is fine.

The problem comes that when pressing any buttons that are located within subview A's nib file, in which they add new subviews, the info button remains on front.

So, how can I add these later subviews on front of all parent view stacks, so the info button does not appear? or how can I hide the info button?

1 Answer 1

1

If I understand correctly, your mainViewController's view has two superviews: subView A and infoButton. You'd like to add subviews to subviewA that appear over the infoButton view?

The simple answer is that you can't make subviews of subviewA appear over the infoButton view. If you think of the view hierarchy as a tree, the renderer draws the views in a depth-first way. This means it draws subviewA, and all of subviewA's subviews (and so on) before it considers the infoButton view. If you want a view to appear over the infoButton view you need to add it as a sibling of infoButton after infoButton in the main view's subview list.

If you'd like to hide the infoButton, you can simply set the hidden property to YES, and then set it to NO when you want the infoButton view to reappear, of course.

Depending on what you're doing with subviewA, you might consider using a modal view controller or a navigation controller to manage the views that you add when the user interacts with subviewA.

6
  • Hi Adam, thanks for your response. The infoButton is a button within mainViewController's view. Is within subview A that I'm trying to load a view that when did load, the infoButton still on front of the view that I just load. Subview A is already a child of mainViewController. How can I hide the infoButton?
    – Ole Media
    Nov 7, 2010 at 15:21
  • I think the easiest thing to do is to simply set the hidden property of the infoButton. When you add the new view to subviewA: infoButton.hidden = YES; When you want the infoButton to appear again: infoButton.hidden = NO; Nov 7, 2010 at 20:51
  • Alright! We are getting close. I do want the infoButton to be in the subview A, but not in the subview that is being loaded from subview A. How can I hide the infoButton from the later subview?
    – Ole Media
    Nov 7, 2010 at 22:50
  • You don't necessarily need to hide infoButton from the later subview. At some point you have some code that adds the later subview to subviewA; you can add a line of code to set the hidden parameter of the infoButton view to YES in the same method. Similarly, add the line to set the hidden property of infoButton to NO in the method that removes the later subviews. Nov 8, 2010 at 4:32
  • That is exactly what I'm trying to do. So from mainViewController loads subview A, how can I reach the infoButton within subview A? Would it be like... self.view.superview.infoButton.hidden = YES ?
    – Ole Media
    Nov 8, 2010 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.