The example will show you how to create a UIView programmatically.
The example will show you how to create a UIView programmatically.The example will show you how to create a UIView programmatically.
In the previous example, we have illustrated you how to load the next UIView using pushViewControler. This example is also similar to it.. the only difference is that prior, we were loading the UIView that already have been added in the application. But here we are going to create a UIView programmatically.
Code to create UIView Programmatically
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//creating a new UIViewController
UIViewController *newViewController = [[UIViewController alloc] init];
newViewController.title = @"New UIView";
viewOneViewController.view.backgroundColor = [UIColor greenColor];
//Pushing new view to load
[self.navigationController pushViewController:newViewController animated:YES];
}
In the code, you can see that we are creating a new view using UIViewController and allocated it memory using
UIViewController *newViewController = [[UIViewController alloc] init];
"newViewController" is the name of our UIView that we have just created.
You can also set Title and Background color of UIView Programmatically such as...
//Setting Title for new UIViewController
newViewController.title = @"New UIView";
//Setting background color for new UIViewController
viewOneViewController.view.backgroundColor = [UIColor greenColor];