How can I insert a subview below the other subviews - ios

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

Related

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.

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

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.

How to draw inside a view in a UIScrollView or UIViewVontroller?

I want to implement a signature feature in my tabbar application. I want to add a subView to a UIScrollView or UIViewController (which ever is the more appropriate and easier). Which control should I use for it?
I want to pick the touch events on the signature view and draw on those points to get the signature. Any help would be highly appreciated.
You can use just a UIView for drawing points get touched. I needed to implement something like that and the following page was really helpful. It explains all with code. You may want to check it out.
http://www.effectiveui.com/blog/2011/12/02/how-to-build-a-simple-painting-app-for-ios/
First thing you need to read is this - http://developer.apple.com
and check this apple demo - GLPaint
after that you may check this - tutorial

How can this bar be done in iOS?

I really like this popup with the additional functions for the selected listitem under:
But have no idea how this is done. Does anyone know how to do it or has a guide or some keywords for me?
Here is another example:
You could achieve a similar effect by adding a UISegmentedControl with <1 alpha to the view when a cell is selected - hope this helps :)
The keywords to look out for are:
UIToolBar or UISegmentedControl - both using some custom drawing or appearance modifications. Well, you could just as well create an entire custom UIView containing a bunch of UIImageView's.
Additionaly, for that shadow have a look at CALayer's shadowOffset, shadowColor, shadowOpacity, etc. ... properties.

Resources