UIView clips subviews when it should not - uiview

I put an UIView in my viewcontroller in storyboard and then another UIView in the previous one. My second view is not completly in the bounds of the first one. The problem is that even though I have "Clip subviews" set to "NO" it is still clipping. I do not want it to be clipping. Am I missing something?

"layer.masksToBounds = NO" seems to work

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.

Why the layout is different between storyboard and the simulator?

As below image shows, in the storyboard, I put a UIScrollView (say called A) under navigation bar and another UIScrollView (say called B) inside A. B is at the top of A.
However, in the simulator, there is big margin between B and the up bound of A (I didn't use any autolayout or write any code). Can anyone tell me the reason? Thanks.
add the below line in your - (void)viewDidLoad method
self.automaticallyAdjustsScrollViewInsets = NO;
hope it will fix your issue.
Even though you're not using Auto-layout, you have Auto-layout checked on in your Storyboard. Because you do, UIScrollViews and any Subclasses of it (UITableView, UICollectionView, etc.) all automatically adjust for the UINavigationBar height when in a UINavigationController. This is implicit and there's no way to turn it off. The only solution is to "offset the offset" or to move the UIScrollView away from the UINavigationBar.
The big margin size is navigation bar height, try add a top autolayout for your scrollView.

UIScrollView Frame Always CGRectZero

I have a nib that has the standard UIView and I've also included a UIScrollView as an IBOutlet (yes, it is hooked up), not as a subview of the main UIView, but just out on its own. Autolayout is turned off. The scroll view has several subviews and is larger than the main view. In viewWillAppear:, I set the content size of the UIScrollView to its current frame size, and then set its frame to the size of the main view, and add it as a subview of the main view.
Unfortunately, nothing is showing up. When I NSLog the frame of my UIScrollView, it is coming back as {0, 0, 0, 0} (CGRectZero). I thought this was odd, so I went back and tried logging the frame before I do any changes to it. Still zeroes. Logged it out in viewDidLoad before anything is done to any of my view elements. Still zeroes. (FWIW in my nib, the frame is {0, 0, 320, 896})
I've had this issue with several of my controllers, but it seems to be hit or miss. Sometimes it works, other times I get the empty frame. Typically, recreating everything from scratch seems to fix the issue, but I don't know why, as I'm setting everything up the same both times.
Running Xcode version 6.1 (6A1052d), iOS SDK 8.1 with a deployment target of 7.0
Let me know if there is any other relevant information I can give that might help.
EDIT 1: To address the questions about my UIScrollView being a "subview", here is what my view heirarchy looks like in the document outline:
As you can see, the UIScrollView is a "subview" of the view controller, but is not a subview of the "main" UIView which has the controller's view outlet.
EDIT 2: More images and some code. Here is a better look at how my nib is set up:
I add my scrollView to the main UIView as follows:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
scrollView.contentSize = scrollView.frame.size;
scrollView setFrame:CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.size.height - 100); // I slightly modified this because there are other variables that determine its Y position and height
[self.view addSubview:scrollView]
}
But I still don't think any of that code matters- for whatever reason, my UIScrollView is coming back nil. If I have the view created in my nib, the outlet is connected properly, how would the view still be nil? I'm creating my controller via initWithNibName I've tried cleaning the project, and removing the app and reinstalling.
Best guess on an answer, and some comments:
Nibs are simply serialised object trees. You can have as many view as you want as root. The problem (big one!) is that if they are not "connected" to outlets that retain them, at some point you will loose those references.
Since your UIScrollView is not a subview of the main view controller view on the Nib, it's not retained by it (views retain their subviews). So, it's up to you to retain it on your view controller.
My guess is that at the point you are trying to set the frame in the View Controller, the scroll view is already gone. I'm not sure how sure when are you doing it, but it might be after some run loops, so the unretained scroll view is dealloced.
Workarounds:
Instead of a variable for the IBOutlet, use a strong property (#property (strong, nonatomic) IBOutlet UIScrollView *scrollView).
Add the __strong qualifier to the variable.
Add the UIScrollView as a subview within the nib, so when the object tree is deserialised it's already retained by its parent.
Given that you purposefully put the UIScrollView outside of the main view hierarchy, I'm assuming you don't want option 3, so I'll just go with 1.
First, you might want to consider using autolayout. It makes things a lot easier once you make the investment to understand how it works. Check out this Apple doc on using UIScrollview with autolayout.
If that's not an option, the problem is that you're setting up your scrollView at the wrong point in the cycle. The scrollView needs a chance to lay itself out. By the time you hit viewWillAppear, you've missed sub view layout.
So, you could try:
Configure your scrollView in willLayoutSubviews and set its setNeedsLayout property.
Explicitly call layoutIfNeeded on your scrollView in viewWillAppear. The idea is to force another subview layout cycle. But, I'm not 100% sure it would work -- it might be too late at that point.
Note that the size of the main view (self.view) isn't determined until viewDidAppear. It's not always correct in viewWillAppear. So, you have a problem in your setup: you can't really set up your scrollView until you know your view size, but you don't know that until it's too late! You might want to redesign to avoid that dependency.

Why UIScrollView can not scroll once dragging UIButton or UILabel from storyboard

I got the same issue here
The UIScrollView can scroll when adding button or label via code, but failed when adding from the storyboard or xib.
Setting the contentSize from the viewDidAppear can resolve this issue.
Set up the contentSize from viewDidLoad can not work. Question here is to figure out why ? I hope someone can explain the insider magics or this is just a bug of UIScrollView. I think it should not be designer like this way.
The reason it doesn't work in ViewDidLoad is that all the sizes are not yet final there, in fact, Im betting it would also not work in viewWillAppear.Im guessing you are not liking making the change in viewDidAppear as it is visible to the user. The correct place to set the size is viewWillLayoutSubviews.
The flow is
viewDidLoad
viewWillAppear
viewDidLayoutSubviews
viewDidApear
I suggest you reread those methods in the documentation.

UIScrollView unwanted scrolling after addSubview or changing frame

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.

Resources