Search This Blog

Sunday, August 23, 2015

How to careate a view programmatically in IOS?

UIWindow* window = [UIApplication sharedApplication].keyWindow;

UIView *polygonView = [[UIView alloc] initWithFrame: CGRectMake ( 0, 0, 200, 150)];
//add code to customize, 
polygonView.backgroundColor = [UIColor blackColor];

[window addSubview:polygonView];
[polygonView release];
 
This is a pattern you will use for not only this but subviews afterwards. Also, another note is with many of the templates, the viewController is already set up with it's own view. When you want to make a custom view, you create it like above but instead of the method above you set the viewControllers view to the newly created view like so:
 
viewController.view = polygonView;
 
======================================================================== 
 
UIView *newView = [[UIView alloc] initWithFrame:mainView.bounds];
[mainView addSubview:newView];
[newView release]; 

No comments:

Post a Comment