Change UIView background color programmatically - iphone

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 ?

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];

ios - how to add view dynamically

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...

UIPopoverController and UINavigationController cuts corners

I have a problem with the display of my popover. After initWithContentViewController: and presentPopoverFromBarButtonItem:permittedArrowDirections:animated: it cuts corners of the navigation bar. How should I fix it?? Thanks.
This is the code I'm using
NavContr *nav = [NavContr new];
nav.navigationBar.backgroundColor = [UIColor redColor];
UIPopoverController *tempPop = [[UIPopoverController alloc] initWithContentViewController:nav];
[tempPop presentPopoverFromBarButtonItem:mainButtonItem permittedArrowDirections:UIPopoverArrowDirectionUp animated:NO];
EDIT: I have resolved this problem:
+ (void)configure:(UINavigationController *)navController {
UINavigationBar *navigationBar = navController.navigationBar;
UIView *contentView = nil;
for (UIView *view in navController.view.subviews) {
if ([[NSString stringWithFormat:#"%#", [view class]] isEqualToString:#"UILayoutContainerView"])
contentView = view;
}
// setting frame to navigation bar and content view
[navigationBar setFrame:CGRectMake(navigationBar.frame.origin.x, 0, navigationBar.frame.size.width, navigationBar.frame.size.height)];
[contentView setFrame:CGRectMake(contentView.frame.origin.x, 0, contentView.frame.size.width, contentView.frame.size.height + navigationBar.frame.size.height)];
[navController.view bringSubviewToFront:contentView];
for (UIView *customView in contentView.subviews)
customView.frame = CGRectMake(customView.frame.origin.x, customView.frame.origin.y + navigationBar.frame.size.height, customView.frame.size.width, customView.frame.size.height);
[contentView addSubview:navigationBar];
[contentView bringSubviewToFront:navigationBar];
}
This is probably because you have no root view controller, or are otherwise fiddling with the navigation controller in ways it was not meant to be played with. This is how you ought to be setting up the popover:
MyCustomViewController *viewController = [[UIViewController alloc] initWithNibName:#"MyCustomViewController" bundle:nil]; //or storyboard or whatever
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewController]; //you should have a root view controller before displaying the popover
tintColor = [UIColor redColor];
UIPopoverController *tempPop = [[UIPopoverController alloc] initWithContentViewController:nav];
[tempPop presentPopoverFromBarButtonItem:mainButtonItem permittedArrowDirections:UIPopoverArrowDirectionUp animated:NO];
There are a few very important things going on here:
Your navigation controller should have a root view controller before you display it.
This code is using a standard UINavigationController instance. According to the documentation, you should not subclass UINavigationController, nor should you try and reinvent the wheel. Apple has created a complex and comprehensive framework, UIKit, that you can use to build amazing apps. If you try and step outside the box, you'll be creating an awful lot of work for yourself without any appreciable benefit.
This is using the tintColor property of the UINavigationBar class. If the tint is insufficient for your UI, you can also set the background image manually (refer to the docs).
If you want to make a popover with a navigation controller, use the built-in UINavigationController class. Don't subclass it and don't reinvent it. To customize the appearance of the navigationBar, use the UI_APPEARANCE_SELECTOR methods in the UINavigationBar class.
I get the solution before add CALayer the UIPopOverController shows like
after adding below lines in table view class i get the following UIPopOverController
#import <QuartzCore/QuartzCore.h>
CALayer *imageLayer2 = self.tableView.layer;
[imageLayer2 setCornerRadius:-20];
[imageLayer2 setBorderWidth:1];
Try it in your project may be it works!!
Thanx
I have tried & replicate the issue you are facing, made some R&D. It's due to the line of code below :
nav.navigationBar.backgroundColor = [UIColor redColor];
While you set the background color of the navigation bar it will behave weird due the native shape of the pop up. Try and remove the below line, you will definitely have issue resolved.
If you are specifying the Rect where the popover appears, we've found that using decimals can result in weird distortions like that. Be sure you're using whole number for origin and size.

Draw labels and buttons programmatically onto a View

I'm building an iPhone app in which I should create a new view based upon some data... so how can I programmatically create a new view with buttons, labels and pictures dynamically? Has somebody an example?
Read
Create UIButton Programmatically
Creating programmatically a label (UILabel)
And Create then in the View. Here is a very good tutorial on how to do that !
Good Luck !
First rightclick on your Xcode project (in ios 5) and select "New file">select cocoa touch>objective c class>subclass of uiview>and name your file name. THen add the following code in the .m file.
// Implement loadView to create a view hierarchy programatically, without using a nib.
-(void)loadView {
UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
[view setAutoresizingMask:UIViewAutoresizingFlexibleHeig ht|UIViewAutoresizingFlexibleWidth];
[view setBackgroundColor:[UIColor blueColor]];
self.view = view;
[view release];
}
Then create the buttons as described in below posts.

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