Bring previously added view to front - ios

My problem is the following:
I have a view and to that view I add 3 subViews(view1,view2,view3) in that order. The views slightly overlap. I want to bring the first view to the top but I just can't get it to work.
I tried adding the views with insertSubview:atIndex: and giving view1 a larger index and I tried using addSubview: and then bringSubviewToTop but nothing seems to work.
Any ideas?
P.S. I can't add the views in a different order. They have to be added in this order.

bringSubviewToFront should work, just make sure you're using it correctly. The usage is.
[parentView bringSubviewToFront:view1];

You can try the method bringSubviewToFront:
Moves the specified subview so that it appears on top of its siblings.
- (void)bringSubviewToFront:(UIView *)view
Parameters
view: The subview to move to the front.

Related

How to bring subview to front in interface builder without changing its position

I want to ask a simple question how can I temporary bring a subview to front to view its element without changing its position by dragging and dropping. The problem i face all the time is i forgot to put the views back to there position and that causes lot of trouble specially if you are working on the view that have large number of subviews.
Question : Is there any shortcut or any functionality that can show the view temporary without dragging, changing its frame or making any changes in its hierarchy.
For a view controller like this :-
FYI, for those who are wondering, the view closest to the bottom of the list (on the left) will show in the front. So in the case below, view 'a' will be in front of 'b' and 'b' will be in front of 'c'
XIB:
If you need your views to be readily accessible for viewing/editing without having to rearrange them, I would actually recommend breaking them out into their own view and then stitching them together in the correct order in your code. This will ensure that all elements will be put in the proper order and will always be easily editable. Something like this:
And then in your code, in somewhere like viewDidLoad:
[self.view addSubview:view2];
[self.view addSubview:view3];

How can I bring a view in front of another view, in Swift?

Below are how my views are organized in IB, top->bottom when the app is started.
The user can do something to make "Category Table View Header" temporarily expand over "Name View" - however once doing so, the .TouchDown action assigned to "Category Table View Header" no longer works wherever it overlaps with "Name View" (i.e., the user can tap the header anywhere it doesn't overlap with name view and it still works).
I know that may be confusing, so I drew out some boxes. On the left is the original, right is after user action - problem is on the right the action on the red box only works if the user taps the bottom half, not the top half.
My guess is its because the header is lower in the view hierarchy than the name view, but it would be hard for me to change that without messing around with a bunch of constraints.
I also tried setting nameView.hidden = true, but that doesn't work.
If you want to bring a subview to the front, you can use:
SWIFT 4 + UPDATE
self.view.bringSubviewToFront(yourView)
SWIFT 3 UPDATE
self.view.bringSubview(toFront: yourView)
Send view to back:-
SWIFT 4+ UPDATE
self.view.sendSubviewToBack(yourView)
SWIFT 3 UPDATE
self.view.sendSubview(toBack: yourView)
SWIFT 4+ UPDATE - INSERT VIEW ON SPECIFIC LOCATION IN THE STACK
parentView.insertSubview(yourView, belowSubview: requiredViewOnStack)
parentView.insertSubview(yourView, aboveSubview: requiredViewOnStack)
Swift 5.1+
UIKit draws views back to front, which means that views higher up the stack are drawn on top of those lower down. If you want to bring a subview to the front, there's a method just for you: bringSubviewToFront(). Here's an example:
parentView.bringSubviewToFront(childView)
This method can also be used to bring any subview to the front, even if you're not sure where it is:
childView.superview?.bringSubviewToFront(childView)
In Swift 5
To bring a subview to front
self.view.bringSubviewToFront(yourView)
To send a subview to back
self.view.sendSubviewToBack(yourView)
You can take a control over the order of subviews using methods: bringSubviewToFront and sendSubviewToBack from the superview.
You can access all the subviews contained by superview using self.view.subview array.
The method has been updated since swift 3
What has worked for me and hopefully for you is using :
YourView.bringSubview(toFront: yourelementA)
YourView.bringSubview(toFront: yourelementB)
Alternatively you could use the following to send the object that is in the front:
YourView.sendSubview(toBack: yourelementC)
I hope this helps
Swift 5.4
views in UIKit are drawn from the back towards the front (like adding icing onto a cake). This means that the new views will be placed on top/over the previously placed views. (e.g. Icing is the new view and the base of the cake being the old view.
You can use the following to manipulate the views Z-position using,
Forward
parent.bringSubviewToFront(child)
Backward
parent.sendSubviewToBack(child)
bringSubViewToFront did not work in my case, Take a look
As I wanted this notification count comes before image, bringSubViewToFront did not work, So I just drag this label below to stack view of my imgae and bottom label, it worked for me.
I hope this will work for all

How to assign a higher level on a uiview in order to make it override another view?

I created a uiview and added a sub-uiview on it. Then I allow user to drag the sub-view around the screen freely. But I have problem when moving the sub-view on top of the screen. Please see below picture. The sub view override the top uiview on the screen. What I want is to let the top uiview to override the sub-view. How can I change the level of a uiview in this case?
when I use self.titleView.bringSubviewToFront(sv)
I see another problem as shown in below image. The status bar is not at the top of the image view.
You can use bringSubviewToFronton parent view to bring sub view to front as follow:
objective-C:
[parentView bringSubviewToFront:childView];
swift:
parentView.bringSubviewToFront(childView)
Edit:
use below method to manage views:
bringSubviewToFront(_:)
sendSubviewToBack(_:)
removeFromSuperview()
insertSubview(_:atIndex:)
insertSubview(_:aboveSubview:)
insertSubview(_:belowSubview:)
exchangeSubviewAtIndex(_:withSubviewAtIndex:)
like,
self.view.sendSubviewToBack(myView)
For more detail check: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/doc/uid/TP40006816-CH3-SW46
Hope this helps :)
parentView.bringSubviewToFront(view)
UPDATE
Pay attention: you must considered that parentView is the parent of your view. To explain better this concept if you have a line like this:
self.container.addSubview(view)
so self.container is the view parent's, dont confuse it with the view below your view...it may not be the same.
If your draggable view is a subview of the image view try to set masksToBounds to true:
imageView.layer.masksToBounds = True
But the reason of your issue is probably because you set zPosition of draggableView.layer to some positive number. Usually it is better to use methods such as insertSubview(_:aboveSubview:) and bringSubview(toFront:) to manage subview's order.

How to change view depth?

Is there any way to make one view appear over another in the code? like adjust its depths or layer so its displayed above the other? I have two textviews and I want one to be shown but the other is currently covering it. I can't edit this in storyboard either because I created both views in the code and not in storyboard. Thanks
If you'd rather not mess with z-positions, you could always use the following methods:
- (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2;
- (void)bringSubviewToFront:(UIView *)view;
- (void)sendSubviewToBack:(UIView *)view;
Note that sending a subview to the front or back will bring it all the way to the front or back, not just by 1. You could also just remove the view and insert it where you want using:
- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index;
You can change UIView layer z order with following code.
[yourView.layer setZPosition:10];
It is applied to all objects inherited from UIVIew.

Flipped NSScrollView problems

I have an NSScrollView that needs to display a variable amount of NSViews in it. I made a custom NSView that has isFlipped return YES and put my NSViews in that before I set it to be the NSScrollView's documentView. That works well. It displays my items top to bottom like I would expect.
But, when the contents changes and I need to change the NSScrollView's documentView's frame, my contents disappears.
This would be WAY easier with a UIScrollView, but alas.
I'm assuming these are equivalent:
[NSScrollView.documentView setFrame:newFrame];
[UIScrollView setContentSize:newSize];
I would imagine that a lot of coders need to have a flipped NSScrollView, but how do you deal with changing the content size without this madness? Obviously, I'm missing something.
Anyone? Thanks!
can you try just setting
[scrollView.documentView setFrameSize:newFrame.size];
to see if your content disappears?
The problem was with the internal views and autoresizing/constraints. My internal views are actually a series of view controllers with their own xib file. In each view controller, I added the following line:
[self.view setAutoresizingMask:NSViewNotSizable];
And that solved everything.
Special thanks to lead_the_zeppelin for helping me (in chat) go through everything.

Resources