Animate to resize height of UIView - ios

I'm facing very weird issue, I have UIView grandWrapper namely in which I'm adding many subviews now when i animate the height of my grandWrapper to zero, ideally height of grandWrapper should be zero and all the inner views should disappear since they were existing inside that grandWrapper View but It animates the height to zero and all the subviews are still there. Can anyone help?
P.s. Im creating subviews programmatically. Thanks in advance

Make subview's autoresizingMask property contains UIViewAutoresizingFlexibleHeight if you want the subviews resize its height according to their parent's height.
Or you can set the view's clipsToBounds property to YES.

Little bit confused from your question
1.If you want to remove your grandWrapper sub views then call below function
-(void)clearReportContent
{
if(grandWrapper!=nil)
[[grandWrapper subviews] makeObjectsPerformSelector: #selector(removeFromSuperview)];
}
2.If sub views are appearing on screen when you do the grandWrapper height to zero,
then set grandWrapper.cliptoBounds=YES; at start where you add the sub view on grandWrapper.

Related

Can't get UIScrollView to work

This is a storyboard app. I added a UIScrollView on top of the UIView that is there in the view controller by default. Then I added some UI elements on to the scroll view. After designing the viewable part of the Scroll view, using its handles I stretched it vertically and pushed it up a bit so that that part is visible for me to design the rest of the screen. After I was done I positioned the scroll view back to fit the view. Please note that I did not resize the scroll view to match the size of the view from the IB.
Then I added all 4 constraints to the scroll view (Leading and Trailing space to superview, Top and Bottom space to superview). Then I selected the UIView, opened up the size inspector and changed the Bottom Space to superview's value to 0. That automatically resized the UIScrollView to fit inside the view controller.
Then I added the following lines of code inside the viewDidLoad method.
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.scrollView.frame.size.height);
self.scrollView.frame = self.view.frame;
But this does not work. The UIScrollView won't scroll. Yes, the scrolling is enabled.
Can anyone please tell me what I might be missing here?
Thank you.
EDIT:
I found an old project of mine here. It utilize the way I just explained above and it works! No idea why it doesn't anymore.
I also added a demo project here with the issue.
If I understood your code correctly (a screenshot of the view would help) you set the scrollviews frame to be full size of the view and set then contentSize to the view's size. A scrollview scrolls only if the contentSize is larger than its size.
This might not be the complete answer to your question, but it will help you and others for sure.
To design the views that are bigger in size than the size of the UIScrollView, the best way to have the UIView as a child view to UIScrollView and put all the content on the UIView and finally place the UIView as subview of the UISCrollView. AT last you can set the size of the UIView as the content size of the UISCrollView.
Cheers. :)

UIView's subviews or components Not Moving With View

I have 3 UIViews and added 3 labels to each view as subview. When I resize the views, so that their height becomes zero, still all 3 labels are visible.
And one more point that, initially the labels are at the center of UIViews but when I increase views height, they(labels) do not remain to the center of views.
Does any body know what is this and solution to this?
UIView property clipsToBounds is set to NO by default. Set the property to YES to avoid showing subviews outside of the current bounds.
you can use autoresizing to manage that if you don't use autolayout.

UIScrollView Not Scrolling?

I have a UIScrollView created in interface builder with a bunch of UITextView and UIImageView objects already added. I have it connected to an IBOutlet, and under -(void)viewDidLoad I set its content size to 1200x2000. User interaction Enabled, Multitouch, Scrolling Enabled and shows vertical and horizontal scroll indicators are all set to YES. When I launch the app in the iOS simulator, it doesn't scroll. What could possibly be the cause of this behavior?
viewDidLoad is to soon. You must wait until after the layout of the views has taken place. Try setting the contentSize in viewDidAppear.
Are you using autolayout? If so, check out the following answer (and forget 1.):
UIScrollView doesn't use autolayout constraints.
The best way I know how to overcome this problem without resorting to writing code is that it all can be done within a storyboard and here is how:
Leave autolayout enabled.
Make a generic UIView a subview of the UIScrollView. 2.1)Put all your View components inside this generic UIView.
Make a autolayout constraint between a subview and the UIScrollView. You must pin the right and/or bottom of the last subview to the right and/or bottom of the UIScrollView. This is how the UIScrollView knows the content size.
viewDidLoad is indeed too soon, but viewDidAppear:animated might not be late enough either. The ideal place to set the contentOffset is inside viewDidLayoutSubviews.
If you're using Autolayout, make sure you put the code to set Content size within
viewDidLayoutSubviews
I use a custom view on the UIScrollView, for pull to refresh function, and the scroll view is not scrollable. I try set contentSize with AutoLayout, and try set it by codes too. The scroll view still cannot scroll.
After spending 3 ~ 4 hours, I solve it by contentInset. It will tell scroll view to add some extra space for scrolling.
override func viewDidLayoutSubviews() {
// Add extra 15pt on the `top` of scrollView.
//self.scrollView.contentInset = UIEdgeInsetsMake(15, 0, 0, 0)
// Add extra 2pt on the `bottom` of scrollView, let it be scrollable. And the UI is more beautiful in my case.
self.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 2, 0)
}
Reference docs:
Understanding the contentOffset and contentInset properties of the UIScrollView class #blog
What's the UIScrollView contentInset property for? #Stackoverflow
This is a good walk-through of scroll-views in case anyone forgot after not using them for a while: https://medium.com/#pradeep_chauhan/how-to-configure-a-uiscrollview-with-auto-layout-in-interface-builder-218dcb4022d7.
Basically, make sure you have view -> scrollview -> view, like so:
then,
1. Set scroll view constraint (top, bottom, leading and trailing) to (0,0,0,0).
2. Set inner view constraint (top, bottom, leading and trailing) to (0,0,0,0).
3. Set inner view to have equal width and equal height with parent view.
4. Select height constraint of inner view and set priority to low (250).

how to programmaticaly increase the width of a custom uiview and all its subviews in uiviewcontroller by clicking on a button

I have a custom UIview
I need to increase its width and also the width of its subviews like webviews and another label etc. dynamically.
I have used
_view.setframe - xframe;// this increases the width but the content stays where it is
_view setneedsdisplay or needslayout are not working.
Do I use the autoresizing mask before adding the view in controller? and how?
Can someone please tell me how to go about doing this.
Thanks
You need to set autoResizesSubviews on the parent to YES, then set the autoresizingmask for each subview. Here is the documentation for autoresizingmask
https://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/CreatingViews/CreatingViews.html#//apple_ref/doc/uid/TP40009503-CH5-SW5
This image from apple should help you understand:
Thanks for your inputs
but i worked by recalling
[self layoutSubviews];//and changing the widths

uiscrollview inside of a uiview does not work

a UIScrollView works just fine by it's self (in IB). However, once you put the UIScrollView inside of a UIVIew in IB, it no longer works. I'm doing this with no subclassing so what's the deal?
Answer
You have to make sure Autoresize Subviews is checked on the containing UIView.
The next part is a bit of a hack. You need to assign a new frame to the containing UIView as well. However, the new frame CANNOT be the pre-existing size of your UIView. It must be a different size for this to work.
If you want the size contained on your UIView in InterfaceBuilder then you need to do something like this:
self.frame = CGRectMake(0,0,0,0);
self.frame = [put your desired rect size here];
Have you tried returning NO in touchesBegan if the touch falls inside your scroll view's frame?
Edit:
Make sure you set the scroll view's content size ([[self scrollView] setContentSize:CGSizeMake(320,480)]; - the values do not matter) and make sure you check "Bounce Horizontally" and/or "Bounce Vertically" (depending on which you want) as well as "Bounces":
Have you tried to set contentsize property of UiScrollView like this
self.scrollView.contentSize=CGSizeMake(320,860);

Resources