我有一个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];


我以为使用自动调整大小蒙版时会自动调整大小?

最佳答案

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

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


通过使用此代码,您将使按钮水平放置。如果还希望将其垂直放置,请同时指定UIViewAutoresizingFlexibleTopMarginUIViewAutoresizingFlexibleBottomMargin

10-03 01:01