Add subview to a xib file - ios

I'm trying to add a label to a UIView in a xib file. When I run the app, the label doesn't shows up.
- (void)viewDidLoad {
[super viewDidLoad];
self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 200)];
[self.label setText:#"This is a label"];
xibView *myxibView = [[xibView alloc] init];
[myxibView.myView addSubview:self.label];
}

1.If your controller has the same name with your xib,
For example,XibViewController.m & XibViewController.xib.
Your controller will set its view to an instance of XibViewController.xib by default.
On this condition, you just need to add the label to the self.view
[self.view addSubview:self.label];
2.If you want to load the XibViewController.xib to an different name controller like View2Controller.m, you need load the xib by name and and add the xib view to View2Controller's view.
UIView *myxibView = [[[NSBundle mainBundle] loadNibNamed:#"XibViewController" owner:self options:nil] objectAtIndex:0];
[myxibView addSubview:self.label];
[self.view addSubview:myxibView];

xibView *myxibView = [[xibView alloc] init];
[myxibView.myView addSubview:self.label];
You didn't added myxibView to actual view. Its just an instance. Why don't you do this:
[self.view addSubview:self.label];
Hope this helps.. :)

Simply add ,
self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 200)];
[self.label setText:#"This is a label"];
[self.view addSubview:self.label];
It will subView the label on the view.

Related

Custom View AddSubview programmatically, not shown UIElements…

I do this to add a Subview from Storyboard, which is a subview from a ViewController with its own Class. (TomStatusbarController) ...it is set as CustomClass in Storyboard. I do also Constraints with Layoutformats. This works fine, but the Statusbar is just red and i see no UIElements. I get lots on constraints warnings but it worked before, when i had it as an IBOutlet, but I have to change this now.
-(void)viewDidLoad {
[super viewDidLoad];
if (self) {
self.tomCaptureStatus = [[TomStatusbarController alloc] initWithFrame:CGRectMake(0, 0, 375, 78)];
self.tomCaptureStatus.layer.bounds = CGRectMake(0, 0, 375, 78);
self.tomCaptureStatus.autoresizingMask = UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight;
self.view.translatesAutoresizingMaskIntoConstraints = YES;
self.tomCaptureStatus.backgroundColor = [UIColor redColor];
[self.view addSubview:self.tomCaptureStatus];
[self.view superview];
}
}
So why i don't see any thing, expect my backgroundcolor?
Tom

initWithFrame and loadNibNamed not working together?

I'm using Paged UIScrollView with an embedded UIPageControl like explained at https://github.com/romaonthego/REPagedScrollView.
The code works fine when I use
UIView *page1 = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 280, self.view.frame.size.height - 40)];
page1.backgroundColor = [UIColor lightGrayColor];
[scrollView addPage:page1];
But I need to use a nib file for my project. I'm calling it like :
UIView *page1 = [[[NSBundle mainBundle] loadNibNamed:#"mySubViewController" owner:self options:nil] objectAtIndex: 0];
page1 = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 280, self.view.frame.size.height - 40)];
page1.backgroundColor = [UIColor lightGrayColor];
[scrollView addPage:page1];
Which almost works fine, I can call the view, only problem is that the view does not listen to initWithFrame:CGRectMake, it loads full screen and I can not see the page control anymore to navigate.
Autolayout is set to off.
Could anyone point me where I am wrong here ? What I could do to solve this issue ?
Best regards,
David
You can set frame of any view manually with method -(void)setFrame:(CGRect)frame
UIView *page1 = [[[NSBundle mainBundle] loadNibNamed:#"mySubViewController" owner:self options:nil] objectAtIndex: 0];
page1.backgroundColor = [UIColor lightGrayColor];
page1.frame = CGRectMake(20, 20, 280, self.view.frame.size.height - 40);
[scrollView addPage:page1];

UITextField embedded in a UIView not responding to touch events

Why does a UITextField not respond to touch events when embedded in a UIView (which itself is embedded in the original view controller's main UIView)?
Here's the code:
- (void)loadView
{
self.view = [UIView new];
UIView *positionalView = [[UIView alloc] initWithFrame:CGRectMake(20, 100, 280, 100)];
UITextField *usernameField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 280, 50)];
usernameField.placeholder = #"Full Name";
usernameField.delegate = self;
[usernameField addTarget:self action:#selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];
[positionalView addSubview:usernameField];
UITextField *passwordField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 280, 50)];
passwordField.placeholder = #"Password";
passwordField.delegate = self;
[passwordField addTarget:self action:#selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];
[positionalView addSubview:passwordField];
[self.view addSubview:positionalView];
}
Finding it difficult to understand this behavior as the above code works fine, if I simply remove the positionalView and add the text fields directly to the main self.view.
Can anyone explain this?
You create a local variable named positionalView, but add the text fields to a property named positionalView. These are not the same variables.
The parent UIView's size is zero, pin the top, bottom, left and right constraints of the UITextField to the parent view so the parent view's size is the size of the UITextField, or set the frame of the parent UIView so that it has a size that is not zero.
Check the Debug View Hierarchy debug tool to check!
You need bringSubviewToFront::
[positionalView addSubview:usernameField];
[positionalView bringSubviewToFront:usernameField];
Same for passwordField and positionalView

how to show the views added in view controller

I had created the the view programatically but it does not display on the screen
and gives a message in the console that
Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps
the designated entry point is not set?
Here is my code
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] ;
exmTableViewController *vc =[[exmTableViewController alloc] init];
self.window.rootViewController=vc;
self.window.backgroundColor =[UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
*Code In Viewdidload *
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *img =[[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 20)];
self.imageView=img;
[self.view addSubview:self.imageView];
UILabel *lbl =[[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 20)];
lbl.text =#"Hello";
self.view =lbl;
}
Instead this line self.view =lbl;, use [self.view addSubview:lbl]; to add subview to your view.
try this
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *img =[[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 20)];
self.imageView=img;
[self.view addSubview:self.imageView];
UILabel *lbl =[[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 20)];
lbl.text =#"Hello";
[self.view addSubview:lbl];
}
-(void)viewDidLoad
{
[super viewDidLoad];
UIImageView *img =[[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 20)]; [self.view addSubview:img];
UILabel *lbl =[[UILabel alloc] initWithFrame:CGRectMake(10, 200, 100, 20)];
lbl.text =#"Hello";
[self.view addSubview:lbl];
}
Try like this. self.view = lbl; in this line you assign self.view is as label you want to add subview [self.view addSubview:yourSubView]; do like this.
When you are creating View controller from storyboard, view controller is initiated by storyboard. You should not create windows root controller by yourself. You need to provide story board name in Plist.
After loading view viewDidLoad method will be called. Here you need to set up view(Adding and configuring subviews). But you are changing viewControllers view property, that is set by storyboard. If you want to change your view controllers view you can use,
- (void)loadView
// inside this method you can assign new view to view property.
If you are adding view as subview use view's method
- (void)addSubview:(UIView *)view

creating uiview programmatically?

Creating a view with following code.
- (void)loadView {
paintView=[[UIView alloc]initWithFrame:CGRectMake(0, 50, 320, 430)];
[paintView setBackgroundColor:[UIColor yellowColor]];
self.view = paintView;
[paintView release];
But my problem is whole screen fills with yellow color, but I want with specified frame.
I am creating this view in a view based application.. Am i doing something wrong, Overridind the original view?
You can do it in following way.
- (void)loadView {
/// make a empty view to self.view
/// after calling [super loadView], self.view won't be nil anymore.
[super loadView];
paintView=[[UIView alloc]initWithFrame:CGRectMake(0, 50, 320, 430)];
[paintView setBackgroundColor:[UIColor yellowColor]];
[self.view addSubview:paintView];
[paintView release];
};
UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,35)];
newView.backgroundColor=[UIColor clearColor];
UITextView *mytext = [[UITextView alloc] initWithFrame:CGRectMake(5.0, 0.0, 100.0, 28.0)];
mytext.backgroundColor = [UIColor clearColor];
mytext.textColor = [UIColor blackColor];
mytext.editable = NO;
mytext.font = [UIFont systemFontOfSize:15];
mytext.text = #"Mytext";
mytext.scrollEnabled=NO;
[mytext release];
[newView addSubview:mytext];
[self.view addSubview:newView];
replace self.view =paintView;
by
[self.view addSubview: paintView];
I am Going change Line No 3 in LoadView Method , you should add the subView in main View instead on assiging it Directly.
[self.view addSubview:paintview];

Resources