【问题标题】:UIView horizontal centering using autoresizingmask not workingUIView水平居中使用autoresizingmask不起作用
【发布时间】:2012-06-09 22:38:20
【问题描述】:

我在 iOS5 的主-详细信息应用程序模板的详细视图中间将 UIView 居中时遇到问题。视图的宽度小于 iPad 屏幕,无论设备方向如何,它都应始终显示在中心。这是我尝试过的......

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.view setAutoresizingMask:(UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin)];
    [self.view.layer setBorderWidth:2.0F];
    [self.view.layer setBorderColor:[[UIColor orangeColor] CGColor]];

    // Do any additional setup after loading the view, typically from a nib.
    UIView *someContainerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 600.0F, 56.0F)];
    [someContainerView.layer setBorderWidth:2.0F];
    [someContainerView.layer setBorderColor:[[UIColor greenColor] CGColor]];
    [someContainerView setAutoresizingMask:(UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin)];
    [self.view addSubview:someContainerView];

    [self configureView];
}

这是视图的显示方式

谁能告诉我我在这里错过了什么????

【问题讨论】:

    标签: ios5 uiview autoresizingmask


    【解决方案1】:

    好的,这就是我解决这个问题的方法。创建 UIView 时,我改变了这个

    CGRectMake((0, 0, 660.0f, 56.0F)];
    

    到这个...

    CGRectMake((self.view.bounds.size.width-660.0F)/2, 0, 660.0f, 56.0F)];
    

    由于我使用具有灵活左右边距的 autoresizingmask,因此我实际上需要将 UIView 定位在中心,以便通过在视图旋转时保持左右边距来发挥自动调整大小的魔力。

    【讨论】:

      猜你喜欢
      • 2014-10-16
      • 1970-01-01
      • 1970-01-01
      • 2015-07-11
      • 1970-01-01
      • 1970-01-01
      • 2015-12-28
      • 2016-12-17
      • 1970-01-01
      相关资源
      最近更新 更多