UIView - 如何让UIView中的所有内容居中对齐?

4
如何使视图中的所有内容居中对齐。当我旋转iPad时,内容并没有自动居中对齐,该如何解决这个问题?感谢!

你使用Nib文件还是通过编程添加视图? - shannoga
我正在使用nib文件。我知道有对齐属性,但是即使设置了它,在旋转后仍然遇到同样的问题。 - Nitesh M
2个回答

7

提供 didRotateFromInterfaceOrientation 方法的实现,并将所有子视图居中。

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    for(UIView *subview in [self.view subviews]) {
        subview.center = self.view.center;
    }
}

0

你应该以编程方式更改内容框架,以适应横向和纵向模式。你可以尝试以下方法:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight){
        // set contents' frame for landscape
    }

    else {
        // set contents' frame for portrait

    }

    return YES;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接