【问题标题】:iOS: UILabel centered in viewiOS:UILabel 在视图中居中
【发布时间】:2011-08-15 17:11:38
【问题描述】:

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

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

【问题讨论】:

    标签: ios ipad uilabel uisplitviewcontroller


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-24
      • 1970-01-01
      • 2018-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多