Add subView in storyboard - ios

In my program needs to create view, then create subView and add it in view.
I create view and subView in storyboard. Then in code:
[subView removeFromSuperview];
[view addSubview:subView];
[self.view addSubview:view];
How can I add subView to view in storyboard without code?

You don't actually need to write any code for this. There are many ways to do this:
Select the subview and drag over the view entry in the Objects Explorer of storyboard. Once you leave the mouse hold, the parent view will have a triangle indicating your subview has become its child and the subview will have a bigger indent than your view
Or you can use the "Embed in View" menu as shown in the below pic [source:http://codesheriff.blogspot.co.il/2014/03/8-tips-for-working-effectively-with.html]

open file inspector ->search for view drag and Drop the view in storyboard
so in this you already holding a view now you adding an view its almost like adding a sub view to the main view this is for adding view without code

Open the storyboard and just create the new view inside the subview.
What your doing above is doing nothing.
[subView removeFromSuperview]; //Your removing subview from the superview
[view addSubview:subView]; //is view a new UIView?
[self.view addSubview:view];

You Will have a View in the bottom of right side.You Just drag and Drop the view Where you Want.To Set constriants clearly We are goin Dynamic view Creation.If you do View creation programmatically it will help you in the future project.

Related

xib using autolayout not showing when added as subview

I had an UIViewControllerA created with xib for view reusability.
In the xib I simply added another red view to it and set its constraints to top,leading,trailing, and bottom space to red view's superview (which is my A's view).
Add A to another UIViewController B as B's ChildViewController by doing so:
// UIViewControllerB.m
UIViewControllerA *A = [UIViewControllerA alloc] initWithNibName:#"UIViewControllerA" bundle: nil]];
[A.view setFrame:CGRectMake(0,0, self.view.size.width, self.view.size.height/2)];
[self addChildViewController:A];
[self.view addSubview:A.view];
[self.A didMoveToParentViewController:self];
Nothing red or whatever showed up.
I did try [self.A.view setTranslatesAutoresizingMaskIntoConstraints:NO]; though
that seemed to make my constraints useless and actually did not bring up the red view.
Do I have to code the whole thing (UI elements & constraints creation) when I'm working with xib and need some extra view with autolayout?
Thanks for any advice!
The problem is this line:
[A.view setFrame:CGRectMake(0,0, self.view.size.width, self.view.size.height/2)];
Instead, give this view constraints.
When you insert a set of views that uses autolayout into a set of views that uses autolayout, you must use autolayout to position it.

Hiding parentView and subviews rather then the last added subview

I have custom parent view, which is a sub class UIView with some UIImageView's, UILabel's etc..
I add as a subview screenshot of it.I would like to hide all the subviews and the parent view rather then that screenshot.
Any help on this is appreciated.
Code:
self.temp = [self snapshotViewAfterScreenUpdates:NO];
self.temp.frame = self.bounds;
[self addSubview:self.temp];
I hope the screenshot is one of the subview of your parent view. So hiding the parent view will hide all the subviews including the screenshot.
You can do the following
Make the screenshot size, bound to the parent and add to top
Present the screenshot as a popover/modal-view and blur the parent view
Remove/hide all the subviews from the parent except the screenshot

how to show an overlapping view iOS

I have a view with some UI components and a button on it, upon touch of a button I want to show a half view with some textfields on it overlapping the initial view, the initial view should be visible partly , the overlapping view will cover only half screen from bottom. Is this possible ?
I don't have any code as I am unable to figure out what it needs to be done, as we show any view it covers the entire screen.
Thanks
there are several ways you can do this, here are two:
1) add a popover controller that gets displayed on your button press:
here's some apple documentation on popovers: https://developer.apple.com/Library/ios/documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/Popovers.html
2) add the new view as a subview to your UIViewController
PROGRAMICALLY:
in the viewDidLoad function you can do the following to initialize the halfScreenView
GLfloat topOffset = self.view.frame.size.height/2;
UIView halfScreenView = [[UIView alloc] initWithFrame: CGRectMake(0, topOffset , [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - topOffset)
[self.view addSubview:halfScreenView];
-more logic might be needed if you support Landscape orientation, you can always re-assign the location of the view with halfScreenView.frame.origin and halfScreenView.frame.size
-initially you can have this view be hidden
halfScreenView.hidden = YES;
-when you click the button it will show the overlaying view:
halfScreenView.hidden = NO;
USING STORYBOARD:
you can also set up your overlaying view in the storyboard if you have one
-drag a UIView into your UIViewController and set it up where you want it to be located
-initialize the view to be hidden by checking the hidden box in the attribute inspector
-add the view as a property to your view
-manage when to show this view with self.halfScreenView.hidden
-this technique allows you to customize what is inside the new view within the storyboard which is nice
FOR BOTH:
-be careful with layers, you don't want your view to show up behind the one you already present. In the storyboard the last thing inserted goes on top. With in the code you can always access/change the views z position with halfScreenView.layer.zPosition (higher z values are on top)
First create a new class subclassing UIViewController called SecondView (or whatever you want), then design the UI the way you want (in its .xib file)
Then go to your main view controller's file and make an IBAction for that button.
In that method, write:
SecondView* second = [[SecondView alloc] initWithFrame:CGRectMake(0, [[UIScreen mainScreen] bounds].size.height/2, height, width);
[self.view addSubview:second.view];
This will add it to the bottom half of the screen. Put your own parameters for its height and width. When you want to dismiss the view, you can do this inside your SecondView class
[self.view removeFromSuperview];
You can deal with the textFields from within the SecondView class and have them communicate with your other view by doing the following in SecondView.h
#property IBOutlet UITextField* textField;
Hope this helps!
Yes, assuming you are using Interface Builder, go ahead and build the overlapping view and hook up all of the IBOutlets and IBActions. Say this view is called myView. Set myView.hidden = YES and myView.enabled = NO. This hides and disables myView so that it isn't even there from the user's perspective. In the appropriate button's IBAction, change the hidden and enabled properties to YES. That will make the view visible and active again.

how to get coordinates of button with respect to superview?

My view hierarchy is
UIViewController.view
a subview (on main view with half size and in bottom)
a button on this subview
I want to get the co-ordinates of this button with respect to my main view
the CGRect of this button but according to superview.
[self.view convertRect:button.frame fromView:button.superview];
use
[view convertPoint:thePoint toView:subview];

Navigation controller with two views

I have a navigation controller that controls view switch. How can I show a view without removing the current view?
if your new view has a view controller use
[self.navigationController presentModalViewController:newController animated:YES];
This will add a view to the viewcontrollers view.
[parentView addSubview:subView];
This will add the view in variable subView into the view in variable parentView. In your case this would mean parentView is the viewcontroller's view and the subView is the view you want to add.
You can change the position of the subView in the parentView by setting the subview's subview.frame property.
If you are talking about a viewController you want to show take a look at the answer of Andiih.

Resources