locoboy
locoboy

Reputation: 38910

Create UILabels within UIView and center UIView

Is there a way to dynamically create a UIView with dynamically created UILabels inside it? I'd also like to center the UIView in the middle of the screen.

Any code would be appreciated.

Upvotes: 1

Views: 3864

Answers (1)

conmulligan
conmulligan

Reputation: 7148

I'm not entirely sure what you mean by "dynamically", but you can create a UIView and UILabels programmatically and use autoresizing masks to center them:

UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(20.f, 20.f, 300.f, 300.f)];
myView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | 
                          UIViewAutoresizingFlexibleRightMargin |
                          UIViewAutoresizingFlexibleTopMargin |
                          UIViewAutoreszingFlexibleBottomMargin;

UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(200.f, 200.f, 100.f, 100.f)];
myLabel.autoresizingMask = myView.autoresizingMask;
myLabel.text = @"My Label";
[myView addSubview:myLabel];
[myLabel release];

[self.view addSubview:myView];
[myView release];

Upvotes: 4

Related Questions