How to add UIView to the CorePlot view? - ios

I have a UIView for CorePlot and I want to add UIView under the plot as a subview. I have tried this:
[self.view bringSubviewToFront:_viewTable];
But it doesnt work. How can I make it work?
EDIT:
My Storyboard:
Green - firstPieChartView (CorePlot view)
Red - UIview with tableView
In viewDidLoad:
[self.firstPieChartView addSubview:_viewTable];
[self.firstPieChartView bringSubviewToFront:_viewTable];

Do not add subviews to a Core Plot hosting view. The iOS hosting view applies a flip transform to its children so Core Plot can share drawing code with the Mac version. Instead of making your view a subview of the hosting view, make them siblings (i.e., children of the same parent view).

You are bringing the view to front by this, not adding it as a subview. First you have to use:
[self.view addSubview:yourSubview];
--in order to add it. Then you can bring it to front, if you're overlaying it with other subviews.

Related

CAGradientLayer causes subviews not being visible

Hello Masters of iOS and Swift,
after two frustrating days, I desperately decided to ask here. The problem:
When adding a CAGradientLayer, the subviews aren't shown.
Details:
I have made a method to add a CAGradientLayer in an extension of
UIView
I simply call the method on any view and this itself works perfectly
But if I try to use this method for a UIView in a viewhierarchy (as a background) unfortunately all subviews aren't visible anymore, the
gradient seems to "overrender" all subviews
if I don't call the "addGradient" method on the container view, all subviews are shown properly
amazing detail: Although the subviews aren't visible, they are somehow present and "active" (e.g. a "invisible" UIButton fires")
I am using Autolayout
Any idea would be appreciated!
Thanks in advance.
LukeSideWalker,
Not sure but you can always try to add your layer below all other layer so it wont cover existing subViews. Try this, in your extension where you add layer to view
self.layer.insertSublayer(layer, atIndex: 0)
Tried adding ImageView as subView worked fine :) Should solve your problem as well.
I encountered a similar problem before and resolved the issue by adding a background view to my view. So the view hierarchy would be like:
view
backgroundView
someView
someViewInsideView
someOtherView
Top views alpha seems to affect subview's alpha too. So I use a view with a clear background colour and use backgroundView to give view's background color and other property's with alpha modifiers.

how does one bring superview to front?

Normally developers try to bring subview to front. On contrary, how does one bring superview to front?
I'm looking for a reverse of bringSubviewToFront(_:). It should look like bringSuperviewToFront(_:)
I'm looking for a reverse of bringSubviewToFront(:). It should look like bringSuperviewToFront(:)
That doesn't exist. Instead of giving your button a subview, make both the subview and the button subviews of some container. Then you can adjust their relative z positions to suit your purpose.
I do not think you can do that. Views are laid down in layers with each sub view layer being part of super view layer. The effect you want to materialise can be achieved by hiding/removing all the sub views of the view you want to get Bring To Front effect on.
This works well with sub views because normally you would want to show/hide subviews inside same parent view based on their layer position!
In the spirit of being less rude.
func bringSubviewToFront(view: UIView) {
let superview = view.superview
superview?.addSubview(view)
}
That will bring it to the front. As in:
bringSubviewToFront(mySubview)
What the above code does is detach the view from wherever it is in its superview's hierarchy and reattach it to the top of all the subviews. This has the effect of bringing it to the front like you want. If the view isn't part of a hierarchy, then the function does nothing.

Add a subview from within and below another subview

In an iOS app, I am adding a subview to my main view with:
[self.view addSubview:firstUIImageSubview];
However in the subview class firstUIImageSubview, I am creating and adding another subView (secondUIImageSubview) that I would like to put below the first subview with:
[self insertSubview:secondUIImageSubview belowSubview:self];
My problem is that the second subview is displayed above the first subview when I want to have it below. How is it possible to achieve that ? Thanks.
This should do the trick.
[self.superview insertSubview:secondUIImageSubview atIndex:0];
When you use insertSubview:belowSubview: it places the subview in regards to other subviews that particular object manages.
[self insertSubview:secondUIImageSubview belowSubview:self];
Doesn't make much sense. Although self is a UIView (or a subclass) it still should never manage itself as a subview. Therefore
[self insertSubview:secondUIImageSubview belowSubview:firstUIImageSubview];
is probably what you want. But remember this will only place the secondUIImageSubview below firstUIImageSubview in terms of its Z-Index (it's depth on the screen). If you want it to be physically placed below firstUIImageSubview (IE it's XY coordinate) then you need to set it's position using subview's frame or setting its origin instead (by manipulating it's center or anchor points for instance).

drawRect over subviews?

I created a container view that holds a bunch of child views - a collection view, a custom toolbar and some bits and pieces.
The design has a border on the top, left and right sides, but not the bottom, so I overrode drawRect to include border.
When I added the toolbar I noticed that it appears over the top of the border. (For some reason I initially thought it wouldn't but of course it does!).
Is there anyway I can tell drawRect to draw over the top of my subviews?
Of course there's loads of other ways to solve my problem (adjust the toolbar's frame for example) however I'm asking this question in order to get a deep understanding of how drawing works in relation to compositing and the view hierarchy.
Drawing happens beneath all subviews of a UIView. Think of it as being on the very base - an actual part of your view - and then each subview is added on top of your view. To make the drawing above the subviews is the same as wanting for the subviews to appear under the view, while still being subviews. Perhaps that analogy makes it clearer why it must always be on the bottom. And it also leads you logically to the solution:
To get the drawing to appear above subviews, simply create a new UIView subclass to place the drawing code inside, and place this above all other subviews.
It might also be a good idea to override your UIView's addSubview: implementation, to ensure your subview always remains on top.
I believe you can't, since the drawRect is called first for the view and when it has finished drawing drawRect is called for subviews to draw over it. Maybe just make another subview on top of that view that has the borders you need and is transparent everywhere else?
Subviews are drawn on top of their super views. So the answer to your question is no.
At the time when you draw the border on your container view, Cocoa hasn't even started drawing the toolbar yet.
I guess you could make the top of the border a subview or move the toolbar down a bit.

draw in a sub view added programmatically?

I have a UIScrollView added programmatically to a UIView.
I want to draw few images to the UIScrollView itself.
I know how to draw in the main view using:
[image drawInRect....];
but I just can not understand how to do it inside of a sub view?
i fill that I don't truly understand what is the context and how do i reach it?
thanks for any help.
shani
If you are asking how to add an image to a uiscrollview which has been added programatically then it is simple.
Add the image to a UIImageView then add that Imageview to the scroll view using addSubview: method.
[scrollView addSubview:imageView];
Well if any one wants to know -
the solution is to draw the images to the layer of the UIScrollView.
you can not draw to the view itself without sub classing it.

Resources