IOS Set UIViews on top of each other in storyboard - ios

How can I place UIViews on top of each other in the storyboard without nesting the UIView within each other? I want them to be subviews of the parent UIView, not subviews of each other. The default functionality always forces UIViews to go within each other, not on top of one another.
I am doing this because there will be background methods that control the visibility of such UIViews, which are connected via IBOutlet.

The easiest way (the only way I know) is to drag the views from the Objects panel (the panel where all the vies and hierarchy is listed, marked with red) and create your desired hierarchy. But the annoying part of this process is, if you want to re position the views, you can't drag them because they will be nested again so you will have to move them using x, y, width, height values.

Related

Mass auto layout option for 40 viewcontrollers?

I designed about 40 view controllers using a 5.5 inch storyboard layout. After all of that I tested it on the iPhone 4S...big mistake. everything is jumbled together being for a larger screen size. I was able to fix one view controller up using Size Classes. I am wondering if there is any way I can adjust all 40 at the same time, or at least avoid doing this for every single one. It is really frustrating finding this out now. Thanks!
This is a relatively complicated issue you are attempting to solve, but I have two potential solutions. Both suggestions are based on moving your current interface into containing UIScrollView instances
If you are using storyboards, then for each of your view controller scenes, put a UIScrollView as a descendent of the view controller's view. From there, provided your subviews are contained within other views (like a container view for a set of buttons), you can move those into your scroll view. You will have to setup constraints to define the size of the scroll view's content, but this will allow the size of the device to have a smaller impact on the interface as you will get scrolling as needed.
If you are using nib files (.xib) then it is essentially the same thing, but easier. In this case, move a UIScrollView onto the canvas, but not as a subview of the default view. Once that is out there, move the original view to be a subview of the scroll view and set constraints to be 0 from the subview to the scroll view. Finally, right click drag from the File's Owner icon to the scroll view and set that as the view outlet.
Hopefully one of these will help you.

How to create a horizontal and vertical split view in iOS

I am relatively new to working with iOS, and I need some help wrapping my head around the way to construct this view :-)
What I want to accomplish is to have a view with a horizontal and a vertical split, thus making four quadrants, each being an independent view. At the cross between the vertical and the horizontal splits I want to put a draggable button that, when dragged, will control the position of both splits. This button will be relatively big, so it will overlap with the central corners of all four quadrants.
Furthermore, I would like each of the four quadrants to be scrollable in all directions.
I have been looking at ScrollViews, TableViews, and containers, but I really need a pointer in the right direction :-) How do I structure this layout in XCode?
You definitely want to use a custom container controller (with 4 container views) to do this, assuming that by independent views, you mean separate view controllers. The custom container controller can be set up in IB, by dragging in container views and sizing them how you want. The button would be a subview of the container controller over top the 4 container views. If you want the views in each of the quadrants to be scrollable, then you would need to add a scroll view to each of them, and give them a large enough content view so that they need to scroll in both directions.

Storyboard UIView components hierarchy meaning?

What is the effect of having a UIView act as a child of another UIView in the scoreboard?
Using AVCam as an example, in the storyboard of the project, the components are layered out as following:
Observe that the 3 Button components act as children of “Cam Preview View”.
I’ve made an experiment and managed to get them to be children of “View”. This does not break any UIButton outlet functionality that I’ve managed to notice.
This is a design time choice by the developer. This changes how the controls can be moved by the constraints or other layout controls like springs and strut). Moving them around will not break any wired outlets however can introduce logical errors.
That said it's possible that there is no significant effect for this demo code.
In the first image you posted, those buttons are added as subviews of the Cam Preview View. It is the same as saying
[camPreviewView addSubview:button];
In the second image instead of the buttons subviews of the Cam Preview View, they are subviews of the View at the top of the hierarchy.
This is a design choice made by the developer. Clearly s/he wanted a view with three buttons contained inside as subviews. That way, you can move the Camera Preview View around and the buttons stay in their relative locations within the view.

Positioning items in overlapping UIViews in xib file

Let's say that I have a xib with a stack of UIViews on top of each other:
The bottom UIView(A) with a UIButton on it and when the button is pressed,
this layer is hidden and the next one is shown and put on top.
The next UIView(B) contains a clock counting down from 5 to 0
and when 0 is reached this UIView is hidden and the one described above is
moved to front and shown instead.
So just to make it more clear:
the composition of the xib:
A B
--Z direction-->
update: screenshot attached at the bottom of page.
So to my problem:
When (re)positioning the button or the clock using mouse in Xcode they tend to stick to the wrong
UIView. The more the Objects and "stacked" UIViews the greater the problem.
The Question:
Is there any way to, as with the layer "eye" in photoshop, isolate the UIViews and work on them one by one?
Observe!
I am aware that there are o there ways of achieving this swapping between view BUT this is not the issue. The real problem here is to actually position the button and the clock in the Xcode wysiwyg editor with the least hassle.
Observe!
-------- Added for clarification --------
Screenshot
I would like to isolate the views visually so when working on one of them, all the others are hidden and not open for interaction.
You can drag the second view (B) out of the main view hierarchy and place it below it (on the same level of the main view). That way you'll be able to edit each view separately.
Then you connect that view (B) to an outlet in your viewcontoller inorder to be able to add it to your main view during runtime...just set the frame and add it as a sub view.

Xcode - Is there a way to drag a component from one view to another without losing its frame?

What I'd like to do is drag a component/view from one superview to another in Xcode's interface-builder without having its frame/position be reset.
Xcode's default behavior when doing this appears to be to center the view being moved vertically and horizontally in its new superview, while preserving its dimensions. This is extremely frustrating, as it means that the view needs to be manually repositioned in its new superview. But I had it positioned correctly before I moved it, so I'd like Xcode to just remember all attributes of its frame instead of just its width/height. Is this possible?
Another solution:
Select the items you want in your subview, then
(in the tool bar) Editor > Embed In > view type to embed in.
I found something that might help you guys!
The task is to regroup "child views" into "parent view" so they become children of parent view hierarchically and retain physical positions on display as before action.
First, adjust the parent view to physically covers area of child views. Second, make sure that all children are bellow it in the view list.
Now select all children with the mouse and move them e.g. one pixel up and one pixel down (just to say IB there is some change). After that release children and they will magically become children of the parent and keep their positions on display.
It works for me on OSX 10.8.2 and Xcode 4.6.
Good luck!
I managed to save a lot of time spent repositioning stuff and did this:
Add the parent view in Interface Builder on the same level as the would-be subviews.
Open the storyboard in a text editor and copy the would-be subviews inside the subviews tags of the parent view. XCode is going to update once you save it.
Not very elegant though, can't see why XCode doesn't support it with Shift or something.
Select all controls you want to move from one UIView to another UiView(nay be child) or ScrollView
Cut/Copy
Now after dragging new UIView/ScrollView to your existing UIView, Don't click once to select it, instead DOUBLE click on the new UIView/ScrollView and paste all controls.
Distance difference will remain same among all controls, but you may have to reposition controls again. So don't click anywhere until you have repositioned those, just reposition all controls by navigation arrows as those are all selected already, or you may ve to select those again.
NOTE: I m on XCode 4.2
I have done something similar to Stepan's solution, without using a storyboard. In the IB while the ViewController's view is open:
Create another view in addition to VC's main view
Move all subviews to the second view by dragging them from one view to another (dragging from the list on the left side resets their position) If you cannot select them from the IB using your mouse then select all from the list on the left side then select one final subview from the IB panel using "cmd" button.
Take them back to the final view on the initial main view.
Remove the added view, all set.
This is the best solution to copy subviews to another view and retaining the positions :
Select the items you want in your subview, then
Editor -> Embed In -> View
Copy this View (Cmd+C)
Undo (Cmd+Z) (Since you just wanted the subviews)
Go to the view you want the subviews in and paste it(Cmd+V)
Select the Embedding View that you copy-pasted and Editor -> Unembed
Step 6 would remove the embedding view and you'll have copied just the subviews.
Xcode Interface Builder messes when a Parent View is dragged and dropped into another View (UIView, ScrollView, StackView)
Q:
Embedding a View (which contains many other subviews within it) into a ScrollView or in another top level view is not straight forward with what I have seen so far. What happens soon after is all the subviews might seems to be misplaced due to it couldn’t find their original frame.
A:
Follow following steps and you will be able to resolve it easier as possible:
Select the items you want in your subview, then
Go to menubar, Editor -> Embed In -> View
Copy this new View (Cmd+C) with the subviews (for me currently all the constraints were preserved so far at this point)
Go to the view (be it a ScrollView or may be StackView) you want the subviews to be in and paste (Cmd+V) the copied view
Select the pasted Embedding View (which you newly created previously) and Go to menubar of Xcode, Editor -> Unembed
No, not finished yet! Sometimes, you may experience that there are few more UI Constraint related issues, you will have to resolve them accordingly.
I detected another approach. It is basically: Move = Cut + Paste
This way you do:
get all your subviews to be children of the new parent view (P')
keep (almost) all of your constraints in Auto-Layout based Storyboard
keep your subview's relative positions (frames) one to another
This way you do not:
edit Storyboard file in a text editor
Base the thing is that each view except one (root) in Storyboard has its parent view. Next, when you copy/move multiple subviews, you lose frames and constraints.
The answer is pretty simple. You make a copy of your subviews (SVs) by copying their parent view (P) into new parent view (P'). This way you may need to recreate only constraints from that parent new view (P') to its new parent view but not for every subview you wanted to move.
After you did make copy of parent view (P) into new one (P'), from that new view (P') you:
remove all the children except ones that you wanted to move
recreate new parent (P') constraints
recreate possible Interface Builder outlets to (SVs')
And from original parent view (P) you:
remove all the children that you wanted to move
Before:
View1
View2
P
SVs-you-want-to-move
SVs-you-do-not-want-to-move
View3
After:
View1
View2
P
SVs-you-do-not-want-to-move
View3
P'
SVs'-you-want-to-move
I should stress that this does not generalise well if you have e.g. UIScrollView as a parent view. Then a copy of it would be again a UIScrollView what may not be desirable.
Another thing is when you do remove some of the subviews (SVs) in original parent view (P), you may need to recreate some constraints if other (non-moveable subviews) reference them. But you should do that anyway.
What helped me to solve this issue was-
Create the desired parent view (scroll view or uiview) in the xib at the root level and resize it accordingly
Copy all the views that you want to be subviews of this parent view and paste them onto the new view you created in step 1
At this point you will have duplicated these views. The newly added views would be a misaligned but still in the same order and at the same distance with respect to each other, align them in the xib to match their bounds with the old copies of the same view (This assumes that the new parent view has same bounds as the old one)
The newly added views will lose some of the constraints, refer to the old views to fix those
Delete the old views from the xib

Resources