ios - how to add view dynamically - iphone

i am new to ios develepoment
i have created a function name list which is called after a button is clicked it should load a view dynamically this is my code
-(IBAction)list:(id)sender{
UIView *listView=[[UIView alloc]initWithFrame:CGRectMake(0,0,1024,786)];
[self.view addSubview:listView];
}
but it does not create any view but it calls this function
can anyone help me
Thanks.
Arun

Try
listView.backgroundColor = [UIColor redColor];
If you don't see anything on self.view, check by
for(UIView *aView in self.view.subviews){
NSLog("%#\n",aView);
}

There is not any problem with your code, It will work fine.
You just check two thing
Make some background color in your view
[listView setBackgroundColor: [UIColor redColor]];
You have proper IBAction connection on your button
If you getting redColor on your View mean listView is adding over your view.
EDIT
If your want to add UILabel and UIButton in your view then
[listView addSubview:yourLabel];
[listView addSubview:yourButton];

Make property of listView in .h
-(IBAction)list:(id)sender{
self. listView = [[UIView alloc] initWithFrame:CGRectMake(0,0,1024,786)];
[self. listView setHidden:NO];
[self. listView setBackgroundColor:[UIColor grayColor]];
[self.view addSubview:self.listView];
}
Try out this.

[self. listView setBackgroundColor:[UIColor greenColor]];
your view display with green color..if it is not display check out the connection to the -(IBAction) method...

Related

Programmatically created UITextField hides other controls

I've requirement to create dynamically some controllers. In the image provided here I've programmatically added an UITextField (name), which hides UITableView.
UITableView is hidden by default. When user touches the UIButton above it, UITableVIew gets appear.
My question when UITableView gets appear, how can I make UITableView top of all other controls?
you will have to change the sequence.
Add UItextfield first
[self.view addSubview:yourTextField];
and add tableview and other views after that line of code so that they appear above it.
[self.view addSubview:yourTableView];
Try
[self.view bringSubviewToFront: yourTableView];
I think this will work fine so please implement this one.
UITextField *txtFld = [[UITextField alloc] initWithFrame:CGRectMake(65, 300, 200, 30)];
txtFld.backgroundColor = [UIColor clearColor];
// Border Style None
[txtFld setBorderStyle:UITextBorderStyleNone];
[txtFld setPlaceholder:#"Name"];
[self.view addSubview:txtFld];

Show some help text over a UITableView if the UITableView happens to be blank

I've got an app with several UITableViews in it, and if one of them is empty, I'd like to display a bit of help text, floating in front of the UITableView:
"Looks like you haven't created anything for this list yet. Make your first entry by tapping the button above."
How do I present this view, floating in front of the UITableView?
What method of the UITableViewDelegate would be used to present this view?
What would the code look like to present the view?
What method of the UITableViewDelegate would be used to hide this view once the user adds content?
Here's a quick mockup:
declare in .h file
UIView *root;
in .m file
import QuartzCore/QuartzCore.h
- (void)viewDidLoad
{
[super viewDidLoad];
root = [[UIView alloc] initWithFrame:CGRectMake(60, 50, 220, 250)];
root.backgroundColor=[UIColor grayColor];
root.layer.cornerRadius=10;
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(10, 100, 200, 50)];
label.backgroundColor=[UIColor clearColor];
label.numberOfLines=3;
label.font=[UIFont fontWithName:#"Arial" size:14];
label.lineBreakMode=UILineBreakModeTailTruncation;
label.textColor=[UIColor whiteColor];
label.text=#"Make your first entry by tapping the button at the top of the screen";
[root addSubview:label];
[self.view addSubview:root];
}
inside your button Event method
[root removeFromSuperview];
in viewDidLoad/Appear,
create a view and add it as subview [self.view addSubview:infoView] . on click event of + button remove it from superview [infoView removeFromSuperView]
I would create/remove the view in numberOfRowsInSection (if it is 0 then show the view) or whatever it is called. As for the view itself, have a UILabel in a custom view. To add the rounded corners etc you can access the view's layer, which is a CALayer. Oh and make sure that you have an ivar for the view so you can remove it easily and check if it is visible. And make sure you always reload the table view when you show your view, as otherwise numberOfRowsInSection will not be called.

Add UISegmentedControl in table cell programmatically with clear background

I have grouped table view. I want to add UISegmentedControl in table cell with clear background so that it will be displayed as FOOTER . How can I do this? I tried using clearColor. But it's not working.
Set the background view with a clear UIView object like so:
UIView *clearView = [[UIView alloc] initWithFrame:CGRectZero];
[clearView setBackgroundColor:[UIColor clearColor]];
[self setBackgroundView:clearView];
[clearView release];
And make sure you set cell selection style to none.
Edit: added some square brackets

Hide title of navigation bar without removing it

I have a customized navigation bar with a image background. I do want to show the title on the background but I need its text for the back button in the next view.
self.title=#"" will not put (naturally) in the back button the previous title.
Based on the suggestion of pheekicks, I found a tip to do it:
UILabel *label = [[UILabel alloc] init];
self.navigationItem.titleView = label;
[[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor clearColor], UITextAttributeTextColor, [UIColor clearColor], UITextAttributeTextShadowColor, nil]];
if you want to switch between view controllers, and you want to hide title text of navigation bar, that still appear back button, in root view controller, you should override this method:
- (void) viewDidAppear:(BOOL)animated{
self.navigationItem.titleView = m_anyViewYouWant;
}
This is OK!
This is a fairly old post. But I got around this problem by setting the title in the viewWillDisappear method, so it does not show when the view is displayed, but shows in subsequent views back button.
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[self setTitle:NSLocalizedString(#"mytext", nil)];
}
Try:
self.titleView.hidden = YES;
I'm using this line to hide the navigation bar on viewDidLoad:
self.navigationController.navigationBarHidden=YES;

Change UIView background color programmatically

I've just created a new view based application, and now I want to set the background color at the application startup rather then in IB. I have found this code in a tutorial:
UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
[view setBackgroundColor:[UIColor greenColor]];
But my view is still white.
How do I make it work?
Thanks in advance.
This should work. Are you adding the view to the window using addSubView ?