I want to ask, who the Views in the Storyboard, which are attached to a UIViewController are accessable. Who to add them to the UIViewContoller to its views programmaticliy with Objective C. The appear in the Storyboard like this:
and are in the tree in the same hirgachy as the UIViewController Node.
You can take outlet of that view in respective view controller class as you you take outlet of view put in viewcontroller's default view.
Then in your viewDidload you can add that view to your default view!
For example your outlet is outterView then in viewDidload,
[self.view addSubview:self.outerView];
Second thing if you are adding view in viewDidload and you need your view's size as screen size than in viewDidappear you can do like,
self.outerView.frame = self.view.frame;
Ok it was my fault, sorry folks.
I also need an IBOutlet to the to the ViewControllers view. So connect the them in InterfaceBuilder first and give the View the the customClass.
Referencing Outlets
view->UIViewContoller
HelloUIClass *viewThis = [[HelloUIClass alloc] init];
[self.view addSubview:viewThis]
…this is a start not sure about that.
You just have to do it right in Interface Builder!
DRAG AND DROP! the Reference Outlet in to your Headerfile under the #interface.
Open both windows. The Storyboard and your Controller Class .h file.
Grap an REFERENCING OUTLET from the View in File Inspector or right Mouse click and draw line into your Sourcecode. If you have done your class properly it will hook under your #interface line. AfterDroping you have give it a name "myViewInIB" and than you have just something like this:
#interface UIMainView : UIViewController;
#property (weak, nonatomic) IBOutlet UICoustomView *myViewInIB;
than you can use it normaly in your Class (Obj C)
[self.view addSubview:self.myViewInIB]
you said you have class of that view so you can do like this
Suppose your class name is View then,
1) in storyboard give name "View1" of class to view
2) you need to create Class if you want to give size programatically
3) for view size you can also use constraints instead of frame.
View1 *objView = [[View1 alloc]initWithFrame:CGRectMake(100, 100, 200, 200)];
viewObj.center = self.view.center;
viewObj.backgroundColor = [UIColor redColor];//so you can find view easily
[self.view addSubview: objView];
Related
I'm kinda new to iOS development.
I want to have a custom View and some Labels inside it. And In some viewControllers of my app on a button click I want to add/show that View
at the bottom of that viewController.
As far as I'm manually adding the view in storyboard in all the viewControllers in which I want the view to display. But this is not efficient. How can I add this view in viewControllers programmatically on button click?
Make one BaseViewController class inherited with UIViewController
Now create method named as designFooter in BaseViewController
func designFooter() {
var viewFooter: UIView = UIView(frame: CGRectMake(0, self.view.bounds.size.height - 50, self.view.bounds.size.width, 50))
viewFooter.backgroundColor = UIColor.redColor()
self.view!.addSubview(viewFooter)
}
For Swift 4, 5.1:
func designFooter() {
let viewFooter: UIView = UIView(frame: CGRect(x:0, y:self.view.bounds.size.height - 50, width:self.view.bounds.size.width, height:50))
viewFooter.backgroundColor = UIColor.red
self.view.addSubview(viewFooter)
}
Now inherit this BaseViewController to you ViewController where you want to add footer, and on button click just call self.designFooter()
If this subview you want to add has some dynamic content or has much of its own logic, you might want to employ view controller containment, specifically not only add a subview, but add a controller associated with that subview, too. So, you can have a scene in your storyboard for this subview that will appear on the bottom, and associate it with its own view controller. Then, when you want to add it, you'd do something like:
let child = storyboard!.instantiateViewController(withIdentifier: "storyboardid")
addChild(child)
// set the `frame` or `constraints` such that it is in the correct place, perhaps animating it into place
view.addSubview(child.view)
child.didMove(toParent: self)
And when you want to remove it:
child.willMove(toParent: nil)
child.view.removeFromSuperview()
removeChild(child)
Personally, if this can really appear and disappear from any scene in my app I actually embed the whole app in a container view controller. Then this popping of the child in and out only has to be done once, on this master container view controller.
For example, consider this storyboard:
That is an embedded "container view" (the storyboard equivalent of view controller containment, discussed above). And I can then have a label animate in an out (by animating layoutIfNeeded after changing the height constraint of some view with a label). Then, this bottom view can animate in and out regardless of which view controller's view is currently visible:
Just create a UIView and call addSubview:
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,100)];//change this frame for your purposes
UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(10,10,50,10)];
[l setText: #"My label"];
[view addSubview: l];
[self.view addSubview: view];
As far as adding this to multiple view controllers... I suppose if you had a lot of different view controllers with this same UIView you could create a subclass of UIViewController called CustomViewController. In that class add the above code to viewDidLoad. Then, subclass CustomViewController in all the view controllers with this particular view, and they will automatically add it for you.
Edit:
If you want to design the view in interface builder, make a custom subclass of UIView. Let's call this CustomView. Design it in a nib and add any code you want. Then, whenever you want to create that view, simply call CustomView *cv = [[CustomView alloc] initWithFrame:...] and then do [self.view addSubview:cv];
I got a view that I create from xib file, I have no problem when adding a UITableView. And then I tried to add a button to the xib file. The button is not appear on my screen. I already connect the button using IBOutlet to the header file.
#property (strong, nonatomic) IBOutlet UIButton *deleteNotificationButton;
and my code in m file
[self.view addSubview:_tblMain];
[self.view addSubview:_deleteNotificationButton
this is my storyboard screenshot
the debug hierarchy
the table appear but not the button, How can I make the button appear?
#Just use this line I have added below your two lines.Hope this works.
[self.view addSubview:_tblMain];
[self.view addSubview:_deleteNotificationButton
[self.view bringSubviewToFront:_deleteNotificationButton];
This is a question about the hierarchy. You need not write any code, only do this in Storyboard or xib file.
This is the hierarchy about the view, you should use the UIView as the root view not UITableView.
Then run the project, well. By the way, if you set UIView as rootView, self.view = the UIView you did set, if you set UITableView as rootView, self.view = UITableView.
You can debug with this button, when the project is running, this button will be shown, then check whether the button in the view.
i've tried to create a custom view which works like a bottom bar and it worked
Right now this function is required on multiple classes, so i try writing it into a new class and import it which likes:
//BottomBarLauncher.h
#import <UIKit/UIKit.h>
#interface bottomBarLauncher : UIViewController
-(void)launchBottomBar;
#end
And implement it as :
//BottomBarLauncher.m
-(void) launchBottomBar{
for (UIView *subView in [topView subviews]) {
[subView removeFromSuperview];
}
UIView *btnBarView = [[UIView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height - 53.3, 320, 53.3)];
btnBarView.backgroundColor = [UIColor redColor];
[self.view addSubview:btnBarView];
}
Now here's the problem, while i try implement it on a new view like follows:
//NewView.m
#import "BottomBarProtocol.h"
#interface NewView()
{
BottomBarLauncher *btnBar;
}
#end
//blahblahblah
[btnBar launchBottomBar];
and nothing happens, i think the problem was with
[self.view addSubview:btnBarView];
but i have no idea how to select the current view as target which i can add subview onto.
First a suggestion, looking at your requirements/code I think you want to create custom view. For creating a custom view, create a class which inherits from UIView rather than creating a UIViewController.
Now moving to the code, your btnBar is a UIViewController which has its own view self.view so when you call this [btnBar launchBottomBar] internally you are adding the bottom bar on self.view that is your btnBar controllers view and not on NewView controllers view. Hope you understand what I am pointing out.
Here you are missing out few calls,
btnBar.view.frame = CGRectMake(0,self.view.bounds.size.height-40,self.view.bounds.size.width,40); // Add suitable frame.
//This call will add the btnBar's view as subview onto your current view controller's view.
[self.view addSubView:btnBar.view];
This is not correct/recommended way and you can face serious challenges regarding memory leaks. To avoid those mistakes, as I suggested, create a custom UIView instead. Take a look around on how to create custom views.
Hope that helps!
You can return the UIView form launchBottomBar method and add as a subView in your current ViewController class
Make custom class and delegate and add that view in window and set its frame so that it is not visible and set its frame and slide from bottom when needed so you can use it in all view controller.
Thanks.
I want to show my own custom UIView in storyboard. By far I have done following but my custom view is not showing up.
Dragged and dropped a UIView instance in my screen.
Defined the class for this UIView as my custom class.
Have connected this UIView with an IBOutlet in my view controller.
I even tried with below code in viewWillAppear.
self.myView = [[MyCustomView alloc] initWithFrame:frame];
This works if I create an instance of my custom view and add as a subview to my IBOutlet property for my view. So, below code is working but I want to keep track of only my IBOutlet iVar and do not want to play with another object for changes on my custom view:
self.myExtraView = [[MyCustomView alloc] initWithFrame:frame];
[self.myView addSubview:self.myExtraView];
Any idea how to do this in a better way so that I could have just one reference of my custom view and could change properties on it as per will.
Found the issue. With storyboard we must initialize anything in initWithCode method. I was implementing the regular init method.
I created an uiview , that contained , many textfields , and I have an uiviewcontroller with xib file responsible for making signature .Is it possible to add this viewcontroller to my uiview in order to have this componant of drawing signature in the footer of my uiview ??
- (void)init { //parent view
...
FooterViewController *fvc = [[FooterViewController alloc] initWithNibName:#"myNibName" bundle:nil];
[self addSubview: fvc.view];
fvc.view.origin = CGPointMake(0, self.frame.size.height - fvc.view.frame.size.height);
}
this is how you can create your viewController from xib file and add its view to the main view. assuming this is what you are trying to do.
If you are using storyboards, the easiest way to go is to drag a Container View object from the Object library (where buttons, labels, etc. are) to your view controller's view. This will create a child view controller that you can handle separately and that will be already resized to mirror the size of the container view controller.