nest a UIView into another UIView in storyboard without lossing contents layout - ios

In Xcode 5, using storyboards:
Take a UIView , viewA
Make it the full size of the screen
Fill it with buttons and images
Make a second UIView, viewB
Take viewA and nest it inside viewB (drag and drop)
It will nest, but all of the objects in viewA get moved to a pile in the center of the view.
I know that I can nest them first then do my layout. It also occurs to me that one could leave them as is then add viewA as a subview of viewB in viewDidLoad, viewWillLayoutSubviews, or similar.
However, Is there a way to nest the views in the storyboard without loosing the original layout of viewA

Sorry, I think this was just some weird fluke. I restarted my Mac and it is nesting the views properly now.

Related

Is it good practice to make a view subview of the main view, not a view it visually lies upon?

Say in my view controller I have a custom UIView which holds certain area inside view controller's view. Let's call this view viewA. And I have a custom UIView called viewB which lies within the bounds of viewA. I used to think that viewB MUST be a subview of viewA simply because it lies within its bounds. But today I got into an argument with a colleague of mine, who said that viewB is not necessarily should be a subview of viewA but a subview of the view controller's view instead. What do you think? Is there a common rule regarding this issue?
I think there is no such thing that viewB MUST be a subview of viewA simply because it lies within its bounds.
The view hierarchy is organized by UIView's array property subViews. Each subview has their own frame information to layout relative to parent's bounds. Overlapping is normal
In my opinion it would depend on the usage of viewA and viewB. If you always want to position viewB relative inside viewA's bounds, or of if you always want to use viewA and viewB together with each other it would probably be simpler to add viewB as a subview. If you want to position and use these two views separately or if the positions of these two views are not related per se I would say that they should be separate views.
In short, just because views overlap does not mean they belong together / that one should be superview for the other.
UITableViewCells subviews of UITableView, not because they are with the bounds of the UITableView, but they have internal connections.
In your case, you need to think whether viewA and viewB have some real relationships or just happen to be together. Maybe viewA accesses and modifies viewB a lot? Or viewB is an component of viewA? That's when you need to set viewB to be viewA's subview.

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

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.

Adding a UIScrollView to an existing UIView in Storyboard

I build an app using storyboards and I want to embed one of the view in a UIScrollView. Therefore to do so, I drag an drop the UIScrollView directly in the storyboard. I then adapt it size and position.
After that I select all my items and drag and drop them into the UIScrollView. My problem is that all my items' positions get messed up: they all align in the center of the UIScrollView so I have to reposition them myself.
Is there any way I can keep the same layout?
Thanks a lot for your help!
Cheers
One way is to select the view, then go to editor -> embed in -> scroll view.
For more complex things, temporarily disable autolayout, create a new empty viewcontroller, copy/paste all your subitems there, do your voodoo with your scrollview, paste them back when it's done.

Rearrange UIView Subviews if a Subview is removed in Auto Layout

I am in a need of implementing a drag and drop controller in Full-Auto Layout environment.
I have achieved the dragging and dropping successfully, but the issue is if I drag a subview from another view the other related subviews behave weirdly,
as an example the subviews are laid out horizontally in scrollview are related to each other, so if I remove a subview the other views should rearrange them self automatically.
Regards,
Dhanesh.
when you doing the design part of xib uncheck the autolayout.
After completing the designing then go towards to autolayout and arrange the view using constraint adding.

iOS - Main view resizes - additional UIViews do not

In my XCode project, I have a xib file and the main view resizes for the iPhone 5 (as do its subviews).
I have additional UIViews in this Xib (they're not set as subviews of the main view, though) and these ones don't resize even though I have set the auto sizing properties.
Am I doing something wrong or is there a reason that these additional UIViews aren't resizing?
Thanks.
EDIT: I am adding these views as subviews to a parent view in my viewdidload method. Maybe I should do it in viewWILLload?
Seems I've managed to figure it out! You have to set the frame of the UIViews in viewWillAppear to be whatever size you want and then the subviews will resize.
Luckily, in my case, all my tall narrow views that you can see in the screen grab above, are all going to be the height of the screen, so I can set it there and have the subviews position/resize accordingly.

Resources