Animating UIView with auto layout when the view is just created and about to appear - ios

I have a UIView that contains subviews that contains subviews...(Hierarchy of UIViews).
Every UIView is set with auto layout. When I press a button, this UIView is created and then I would like it to drop from the top of the screen.
The problem when I use layoutIfNeeded in an animation is that if will animate everything at the same time as it is a newly created UIView (not yet display).
My question is, is there a way to do the animation of only the UIView dropping with all the subviews already laid out in it?
I guess you can do it by creating the UIView when UIViewController displays and then hide it but I was wondering it there was another way.
Cheers

If you call layoutIfNeeded right after you add it to a view, while still outside the view, it should lay out it's views correctly. And then in the animation you call it again so the view animate to the final position.

Related

Why is my UIView drawing behind my other views?

I have a UIView which is opened and closed by a button onscreen. I am positioning it over a scrollable custom text area which is a bunch of UILabels added to a UIScrollView.
I've made sure that it is added last in my view hierarchy, in the xib. I have also tried calling bringSubviewToFront, and have even removed the UIView from the parent view and re-added it after doing the layout.
Nothing that I have tried prevents it from being drawn behind this UIScrollView. I have verified that my UIView is actually the last UIView in the hierarchy by looking at the view hierarchy in the debugger at runtime.
The UIView in question, shown as a smiley in the picture here, has children. It has a 3x3 grid of invisible UIButtons, and a UIImageView to draw the image, which is normally something other than the smiley. But I am operating on the parent UIView, not the children.
Is there anything I can try, to help diagnose this? To recap, I have done all the following:
the UIView is added last in my view hierarchy, after the UIScrollView. It is a child of the same parent (the root view) as the scroll view - hence, a sibling view of the scroll view.
after doing the layout, I've tried [self.view bringSubviewToFront:myView]
after doing the layout, I've tried doing [myView removeFromSuperView] followed by [self.view addSubview:myView].
The latter two operations are performed at the very end of my layout method.
Any ideas?

Hide non child UIView in UIView

I want to hide a non child UIView in a UIView but I don't know how to do it.
To be specific, I have a UITableView. Each UITableViewCell has another view inside it (a wrapper view called wrapperView). The wrapper has some labels set up in IB and some created programmatically. I have created a custom slide mechanism that reveals buttons under the wrapper (like the standard one does).
The labels created programmatically don't exceed wrapper's bounds because it clips the subviews. The problem is with the labels created in IB. They are the subviews of contentView.
How can this be solved? Is there a way for a UIView to clip other views on the same level(not parents nor children)? Or "transfer" the labels to the wrapper view?
It isn't completely clear what you're asking. A view will only clip it's subviews, not views that happen to fall within their frame rectangle but aren't subviews.
If you want to move a view object from one view hierarchy to another you can use addSubview(_:) to do so. That will automatically remove it from it's current parent view. To quote the Apple docs:
Views can have only one superview. If view already has a superview and
that view is not the receiver, this method removes the previous
superview before making the receiver its new superview.

UITextField inside a UIView thats animated on touch dismisses the view and shows the keyboard

I have a structure for my UIView object and have form elements within it. I run the application and the program is in such a way that when i click a button inside the UIView, it moves the current frame upwards. But thats not the problem.
My phonenumberfield is of type UITextField which is inside a UIView and that UIView is a part of the parent View. I have one button called submit and a segment control on the Parent UIView. But when i touch the phonenumberfield or any textfield type on the parent view, the parent view gets dismissed and keyboard shows up. Its weird I cant understand what the problem is because can't debug this. :(
I had a problem with the constraints for Auto Layout I had set for my scrollview and managed to fix the constraint and my view started scroll. To animate the view, I set a constant height from the top of the parent view and animated the view by changing the constant which did not affect the keyboard anymore.

animation not correct with implicit autolayout

I upload my demo code here:https://github.com/liuxuan30/Problems
The main problem is, I had a view which have a scroll view inside, and there is a label and collection view inside scroll view.
There will be a unread message button generated by code, when there is unread messages, the button will pop out, and I expect the animation: label and collection view will down-shift by the button's height.
When I test the animation without adding the button in subviews, animate as expected.
When I add the button in, it seems like the label and collection view's origin.Y up-shifted, and start the animation. Turning off auto layout will solve it, but I have to have auto layout. I tried to add constraints for all views, but the animation still not work out.
You can try comment out
[self.HomeScrollView addSubview:AlertView]; and self.UnreadAlertView.alpha = 1.0f; inside the code to see the animation.
Hope someone could figure out where i did wrong.
UICollectionView is already a subclass of UIScrollView. Embedding your collection view inside a scroll view may be what is causing problems with your animation.
Try removing your scroll view and just put your UIButton, UILabel, and UICollectionView inside your overarching view.

How can I bind a UIView's orientation to that of it's parent view?

I have a UIViewController, with a toolbar at the top. I have a few views that I'm going to be adding and removing, but I don't want them to cover up the toolbar. So I made another UIView, that fills the area beneath the UIToolbar, and am adding views thee, and it works great.
When I rotate the device though, the first subview rotates with the view controller, but the UIView inside of the UIView thats inside of the UIViewController doesn't rotate!
What can I do to make it rotate with the parent UIView?
PS- If you can think of a better way to do what I'm trying to do, I'm all ears!
EDIT:
To clarify, I have a UIView subview in IB, and that is all hooked up to the code just fine. Then, depending on the button selected, that UIViews subviews are removed, and then a new one is placed in the subview array. So I use addSubview to and removeFromSuperview to add/remove the view from the UIView that is a child of the UIViewController view...

Resources