有没有办法在其中动态创建UIVabel动态创建UIView?我也想把UIView放在屏幕中间。
任何代码都将受到赞赏。
答案 0 :(得分:4)
我并不完全确定“动态”是什么意思,但您可以通过编程方式创建UIView
和UILabel
并使用自动调整遮罩来居中它们:
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];