How to change view depth? - ios

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.

Related

Set layers programmatically

so I have an application with several images, buttons, etc.. Now, I would like to control programmatically which object is in the foreground and which is further in the back. I know that in the storyboard this is possible, but since I declare a button in the AppDelegate (I don't have in the storyboard) I can't use this function.
Now, is there a way to do this?
You have to use the property .zPosition to accomplish that.
Look at the "Managing View Hierachy" section on UIView to accomplish this.
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/index.html#//apple_ref/doc/uid/TP40006816-CH3-SW46
You can bring views to the front or send them to the back or adjust their index among others.
- (void)bringSubviewToFront:(UIView *)view

Changing the zPosition of searchResultsTableView

I have a UISearchBar working to dynamically show matching results from a list, but having a nightmare trying to stop the searchResultsTableView from obscuring the resultant UIView, which animates in from the right, when the result is tapped.
The magenta view, (including its shadow subview) and the solid black view should be above the list.
I've tried…
self.searchDisplayController.searchResultsTableView.layer.zPosition = 0;
recipeMeasuresView.layer.zPosition = 1;
recipeListView.layer.zPosition = 2;
…but it's messing up the gestures attached still not displaying the views in the correct order. I've also tried…
[self.view sendSubviewToBack:_searchBar];
[ingredientListView bringSubviewToFront:ingredientListView];
…still no joy. Incidently, I'm also adding – amongst other things – [_searchBar resignFirstResponder]; to my
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
I'm very much an objective-C newbie so all help greatly appreciated.
In case anybody else stumbles across this with similar problems it seems that the issue is due to the searchDisplayController adding it's own view(s) at the top of the stack, therefore obscuring anything that was added to the storyboard.
There would be no way to push anything up or down to change the order because the searchDisplayResultsTable was being added by a different view controller.
The solution I used was to add the views that were being hidden programatically, meaning I could add them as subviews to different parent views depending on what was happening. Probably not the best solution but it does appear to work.

Bring previously added view to front

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.

How can I insert a subview below the other subviews

everyone.
I have some labels that I draw them in the xib file, and
add a background view using codes,but the background view
is in the front of these labels, so I cant see them. So,
my question is how can I add a background view below these
labels.
Thank you in advance.
If you Google UIView you'll end up at the documentation - UIView Class Reference
Then you need to look down the list of methods until you find something that sounds like it will roughly fit your needs like these two:
- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index
and
- (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview
At this point you just have to read the descriptions of how the methods behave and pick whichever one is easiest to implement for your design. It's really good for your own learning if you try to find the answer yourself before you ask a question
You can use the method insertSubview:atIndex:
[self.view insertSubview:backgroundView atIndex:0];
You can call this method in Swift 3
view.insertSubview(yourSubView, at: 0)
or
view.sendSubview(toBack: yourSubView)
In IB the subviews of a view are listed in a hierarchy under that view. Subviews which appear above their siblings in that hierarchy will appear below them in the rendered view. So in the example below, the in 99:99 AM label appears under the Sign In button at runtime.
If your background is just a UIImageView, consider adding the view to your NIB and then setting its UIImage at runtime. That way, you don't get confused. Also, what Paul.s said.
Swift 3 and later.
view.insertSubview(theViewYouWantToAdd, at: 0)
https://medium.com/swift-programming/dynamic-layouts-in-swift-b56cf8049b08
Use VerticalLayout as the parent. it is quite simple and effective

stacking two UITableViews in a single view programmatically?

i'd like to stack two UITableViews in a single view. i can do this in interface builder, however, i would like to do this programmatically in code.
in my main UIViewController, i create two UITableViewControllers in my viewDidLoad method.
i thought at first i could do this by:
[self.view addSubView:controller1.view];
[self.view addSubView:controller2.view];
but this only shows the 2nd view. i've also tried:
[self.view addSubview:controller1.view];
[controller1.view addSubview:controller2.view];
but it still doesn't do what interface builder did above.
UPDATE
i want to accomplish this:
two table views
which i did in interface builder:
xib
but in code.
What is the reason to create two UITableViewControllers? Simply create two UITableView objects and manipulate them.
Overlapped? Did you set their frame different, in order to see them?
controller1.view.frame = CGRectMake(0,0,320,75);
controller2.view.frame = CGRectMake(0,75,320,385);
In a rectangle the values are measured from the upper left corner: x,y,w,h.

Resources