ios - 在 iOS super View 中强制布局位置和大小

标签 ios ipad uiview landscape superview

我遇到了一个大问题,我真的希望你能在这里帮助我......我现在很迷茫:(

我有一个使用 MainWindow.xib 运行的项目。在我的应用程序委托(delegate)文件中,我检查设备的方向并加载一个适当的 NIB 文件,该文件根据方向具有不同的布局( subview )。这是检查方向的代码:

-(void)checkTheOrientation
{
    if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft || [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight) 
    {
        viewController = [[[MyViewController alloc] initWithNibName:@"MyWideViewController" bundle:nil] autorelease];
        NSLog(@"Landscape = MyWideViewController");
    } 
    else if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait || [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortraitUpsideDown)
    {   
        viewController = [[[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil] autorelease];
        NSLog(@"Portrait = MyViewController"); 
    }
}

这按预期工作,在风景或肖像中我正在加载正确的 View 。纵向 View 加载完美,但横向 View 加载时左侧有一条粗黑边,就好像它的 x 和 y 位置没有分别设置为 0 和 0。

这是纵向 View :http://uploads.socialcode.biz/2f25352B0e3x3z2t2z21 这是带有错误的横向 View :http://uploads.socialcode.biz/1G2k3T012d1z0Y1U2q1k

在 MyViewController.m 文件中,我有一个粗略的修复来正确调整大小以避免左侧出现这个大黑条。这是为此的代码:

- (void) performLayout {
    // Ensure the main view is properly placed
    NSInteger MaxSizeHeight, MaxSizeWidth;

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
    {
        MaxSizeHeight = 1024;
        MaxSizeWidth  = 768;
    }
    if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft || [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight) {
        CGRect mainViewFrame = [[self view] frame];
        float width = mainViewFrame.size.width;
        mainViewFrame.size.width = mainViewFrame.size.height;
        mainViewFrame.size.height = width;
        mainViewFrame.origin.x = 0;
        mainViewFrame.origin.y = 0;
        [[self view] setFrame:mainViewFrame];
    } else {
        CGRect mainViewFrame = [[self view] frame];
        mainViewFrame.origin.x = MaxSizeWidth - mainViewFrame.size.width;
        mainViewFrame.origin.y = MaxSizeHeight - mainViewFrame.size.height;
        [[self view] setFrame:mainViewFrame];
    }

    // Ensure the content view is properly placed
    CGRect contentFrame = [mainContentView frame];
    if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft || [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight) 
    {
        contentFrame.size.width = MaxSizeHeight;
        contentFrame.size.height = MaxSizeWidth;
    }
    contentFrame.origin.x = 0;
    contentFrame.origin.y = 44;
    [mainContentView setFrame:contentFrame];

    // Ensure the content subviews are properly placed
    contentFrame.origin.y = 0;
    for (UIView *contentView in [mainContentView subviews]) {
        [contentView setFrame:contentFrame];
    }
}

此方法的问题在于,这只是一个非常糟糕的 hack,并没有真正解决我的问题。当它在 Landscape 中加载时,它现在会调整 subview 的大小并将其定位到 1024x768,0,0,但是我通过其他 NIB 加载的任何其他 subview 都有同样的问题。

我真正想知道的是,我究竟如何才能将横向主视图设置为 1024x768 位置 0 和 0,而不必尝试将其组合在一起并继续执行 performLayout 选择器?目前与此有很多不一致之处,因为 hack 实际上并没有正确设置 super View 的大小,而只是我在 super View 之上加载的 subview 。

我认为也许像这样的简单修复可能会解决 superview 问题,但遗憾的是它没有: window.frame = CGRectMake(0, 0, 1024, 768);

我只希望我的主 super View 在加载时正确放置和调整大小,然后 super View 就加载到正确的位置。请你能帮我吗???

最佳答案

首先,UIView 有一个名为 -layoutSubviews 的方法,您可以重写该方法以根据需要放置 subview 。它会在首次加载 View 时调用,如果您向它发送 -setNeedsLayout 消息,则会在绘制之前再次调用。

其次,您应该能够在 .xib 或 Storyboard文件中正确设置这些位置。在其 .xib 文件中选择您的主视图并检查其在大小检查器中的位置。此外,确保在属性检查器中将 View 设置为横向。如果这一切都正确,那么就会有其他事情发生。为了简化问题,在 Xcode 中创建一个新项目,其中只有一个横向的空 View 。我刚做了一个,没有位置问题。这就是您在真实应用中想要的,因此请弄清楚在您的真实应用中发生了什么会影响 View 的位置,而在您的新示例应用中不会发生。

关于ios - 在 iOS super View 中强制布局位置和大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10431612/

相关文章:

ios - 如何正确处理应用程序中与服务器的重新连接

iphone - 正在调试的应用程序不显示通知横幅?

ios - 将 UIView 放置在导航栏前面时看不到按钮

ios - 在 iOS 上检测飞行模式

iphone - touchesbegan 与多个 UIImageView 检测到不正确的 UIImageView

iOS : How to add drop shadow and stroke shadow on UIView?

ios - 为具有圆角的 UIImageView 创建阴影?

iphone - NSRepublicOfChinaCalendar 和 NSGregorianCalendar 有什么区别?

iOS Swift POST 参数和正文

ios - 在 prepareForSegue 中获取 UIPopoverController 引用?