ios5 – 使用autoresizingmask无法正常工作的UIView水平居中

我在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];
}

以下是视图的显示方式

有人可以告诉我,我在这里失踪了吗????

最佳答案 好的,这就是我解决了这个问题.在创建UIView时,我改变了这一点

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

对…

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

由于我使用具有灵活左/右边距的自动调整掩码,因此我实际上需要将UIView置于中心以自动调整以通过在视图旋转时保持左/右边距来发挥其魔力.

点赞