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.
Related
I have this strange issue. To start it all, this is how my view hierarchy
looks like:
I have a UIViewController with a UIScrollView with three separate ContainerView's inside it. Each of the ContainerView's frames (and respective UIViewControllers) are set programatically.
I open the second ContainerView with a UIView.animate(...) method. Inside this UIView/UIViewController I have a UITableView. The problem is that only the first five (and half of the sixth for some reason, but maybe that's a clue) UITableViewCell's are selectable. I have about five more UITableViewCell's which is for some reason not selectable.
I don't have any UITapGestureRecognizer's which has been the problem for some other people that I've seen. Also, if I add the TouchesBegan to either the UITableView or the UIViewController holding it, it behaves in the same manner. That will say, it only prints out when I touch the same part of the screen where the UITableViewCell's are selectable.
Is there anyone that have encountered the same issue or something similar to it that can shed some light over this?
Any help, input or thought is very much appreciated.
So I resolved this issue. For anyone else having similar problems this was because the underlying contentView of the UISrollView wasn't big enough.
Even though the UITableView, and even the views under it was visible, touches could not reach it since these are somehow registered through the UIScrollView contentView (for me, this was an UIView I added in the Interface Builder on top of the UIScrollView).
Also, if you are working with autoLayout, you have to change the contentViews constraints in the Interface Builder since UIScrollView.contentSize won't bother to listen to your commands.
Question: How do I display a background image underneath a CGContext?
I have created a view controller using the storyboard and placed a view with a full-screen background image. Then, using the same view controller, I created a outlet to a UIView that has a CGContext where I draw a few lines.
Everything works except that the drawing takes place underneath the background image. (Suddenly I wonder if it's not the CGContext but the UIView that is the problem.)
I found a similar question here, but the solution makes it sound like the background image has to be constantly refreshed using CGContextDrawImage with a CGImageRef. Which may be true, but a static image being constantly refreshed doesn't sound like a very elegant solution. Thank you!
Create custom UIImageView subclass where you will draw your custom background.
Better to subclass UIImageView instead of UIView, because in this case you don't get problems with adding other views above this in Interface Builder.
Add UIImageView to your view controller, and change class to your custom class. Add all other views above.
Question: How do I display a background image underneath a CGContext?
Answer: Set the color of the view with the CGContext to Clear Color.
That way the background image is visible as well as the lines that are subsequently drawn. Guess I kind of overcomplicated that one.
Thank you for the response Vitaliy.
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.
Im trying to create a secondary view to my main view. This subview needs to be of small height and it needs to fit another big UIView with a label in it.
Problem is that when I'm using UIView addSubview, the third UIView will be shown out of bounds of secondary UIView.
For ease of understanding my situation, I have created those Subviews using Interface builder with the photo and the result on simulator with the photo.
Any help would be much appreciated.
Thanks
Interface Builder
http://postimage.org/image/ofi3m05v3/
Simulator
http://postimage.org/image/x2qps3w2f/
You need to activate the clip to bounds property of some of those views.
fooView.clipsToBounds=YES;
That should solve your problem. Just set the appropriate views to clip to their bounds.
I have a UIScrollView filled with subviews, all is well when creating it and initially filling it.
But when I add a new subview that is positionned outside of the visible screen portion, or when I just resize an existing subview that is also outside of the visible screen portion, there is a subsequent 0.3s-long scroll animation (I can see it happening from my delegate) that seems to match the newly added/resized element.
Attempts:
pagingEnabled is always NO.
Setting scrollEnabled to NO during subview manipulations doesn't help.
Doing a setContentOffset:animated:NO after subview manipulations doesn't prevent the animation.
One single giant subview with all my subviews in it doesn't help.
My current workaround is to initially set the frame to fit inside the visible screen portion, or doing resizing work inside another superview, but it feels dirty, and won't handle all situations...
Is there a way to prevent this automatic scrolling animation when programmatically manipulating subviews?
Xcode 4.3, iOS SDK for 5.1.
I too discovered this problem and found this solution http://www.iphonedevsdk.com/forum/iphone-sdk-development/94288-disabling-uiscrollview-autoscroll.html
It involves subclassing the UIScrollView and entering no code in the following method.
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated {
}
Like the guy says on the link I've found it works and no problems so far. Hope it works for you.
I had this problem because I set the content size of the scroll view prior to adding the subview.
As soon as I change the code so that the content size of the scroll view was set after adding the subview the problem went away.