iOS:UILabel在视图中居中

时间:2011-08-15 17:11:39

标签: ios ipad uilabel uisplitviewcontroller

我有一个UISplitViewController,我试图在右视图控制器中居中(水平和垂直)UILabel。当它以纵向加载时,标签看起来是正确的,但如果将其旋转为横向,则标签不再垂直居中。我正在使用:

CGSize size = self.view.frame.size;
UILabel *noResults = [[UILabel alloc] initWithFrame:CGRectMake(0, 40.0f, size.width, size.height)];
noResults.text = @"No people were found.";
noResults.textColor = [UIColor blackColor];
noResults.textAlignment = UITextAlignmentCenter;
noResults.backgroundColor = [UIColor clearColor];
noResults.textColor = [UIColor darkGrayColor];
noResults.font = [UIFont systemFontOfSize:16.0];
noResults.shadowColor = [UIColor whiteColor];
noResults.shadowOffset = CGSizeMake(0, 1);
noResults.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.noResultsLabel = noResults;
[self.view addSubview:noResultsLabel];
[noResults release];

我认为当我使用autoresize mask时它会自动调整大小?

1 个答案:

答案 0 :(得分:1)

您还应该指定边距是灵活的,如下所示:

 noResults.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin |  UIViewAutoresizingFlexibleRightMargin;

通过使用此代码,您将获得水平重新定位的按钮。如果您还希望将其垂直重新定位,请同时指定UIViewAutoresizingFlexibleTopMarginUIViewAutoresizingFlexibleBottomMargin