Vladimir Stazhilov
Vladimir Stazhilov

Reputation: 1954

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?

Upvotes: 0

Views: 1196

Answers (2)

Muhammed Sadiq.HS
Muhammed Sadiq.HS

Reputation: 773

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.

Upvotes: 0

Viktor Apoyan
Viktor Apoyan

Reputation: 10755

Read

  1. Create UIButton Programmatically
  2. Creating programmatically a label (UILabel)

And Create then in the View. Here is a very good tutorial on how to do that !

Good Luck !

Upvotes: 5

Related Questions