[ios]在点决议检测设备

标签: Xcode ios
发布时间: 2017/3/28 21:16:10
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我用 6 按钮创建一个视图,问题是,当我被旋转为横向的图形化的结果并不好。

应该看起来像这样︰ loghos.eu/hoch.png loghos.eu/quer.png

经过研究后,我看到此脚本︰链接

我试图管理这点分辨率从 iPhone。 但它不工作

    - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    #define IS_IPHONE_5 ([[UIScreen mainScreen] bounds].size.height == 568)
    #define IS_IPHONE_6_Plus ([[UIScreen mainScreen] bounds].size.height == 736)
    if( IS_IPHONE_6_Plus )
    {
        if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
            toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
        {
            eins.frame = CGRectMake(40, 75, 115, 115);
            zwei.frame = CGRectMake(530, 75, 115, 115);
            drei.frame = CGRectMake(290, 75, 115, 115);
            vier.frame = CGRectMake(290, 230, 115, 115);
            fuenf.frame = CGRectMake(40, 230, 115, 115);
            sechs.frame = CGRectMake(530, 230, 115, 115);
        }
        else
        {
            eins.frame = CGRectMake(30, 95, 115, 115);
            zwei.frame = CGRectMake(250, 95, 115, 115);
            drei.frame = CGRectMake(30, 300, 115, 115);
            vier.frame = CGRectMake(250, 300, 115, 115);
            fuenf.frame = CGRectMake(30, 530, 115, 115);
            sechs.frame = CGRectMake(250, 530, 115, 115);
        }
    }

    if( IS_IPHONE_5 )
    {
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
        toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
    {
        eins.frame = CGRectMake(40, 75, 115, 115);
        zwei.frame = CGRectMake(530, 75, 115, 115);
        drei.frame = CGRectMake(290, 75, 115, 115);
        vier.frame = CGRectMake(290, 230, 115, 115);
        fuenf.frame = CGRectMake(40, 230, 115, 115);
        sechs.frame = CGRectMake(530, 230, 115, 115);
    }
    else
    {
        eins.frame = CGRectMake(30, 95, 115, 115);
        zwei.frame = CGRectMake(190, 95, 115, 115);
        drei.frame = CGRectMake(30, 220, 115, 115);
        vier.frame = CGRectMake(190, 220, 115, 115);
        fuenf.frame = CGRectMake(30, 330, 115, 115);
        sechs.frame = CGRectMake(190, 330, 115, 115);
    }
        }
}

解决方法 1:

它是非常不好的做法,对硬编码按钮帧像那样,基于设备。 相反,你的目标应该是创建一个接口,能适应无论大小,它是在通过自动版式或你自己的数学。

我建议使用 viewDidLayoutSubviews 到布局你的观点正确。 旋转发生自认为必须再次被布局时,会自动调用此方法。 从那里,您可以检测到视图的大小类如果你在 iOS 8 或回退到检测接口取向对 iOS 7。

下面是一个示例的什么你 viewDidLayoutSubviews 方法可能看起来像︰

-(void)viewDidLayoutSubviews {

    [super viewDidLayoutSubviews];

    // first decide whether we are landscape

    BOOL isLandscape = NO;
    if ([self respondsToSelector:@selector(traitCollection)]) {

        if ([[self traitCollection] verticalSizeClass] == UIUserInterfaceSizeClassCompact) {

            isLandscape = YES;

        }

    } else {

        if ([self interfaceOrientation] == UIDeviceOrientationLandscapeLeft || [self interfaceOrientation] == UIDeviceOrientationLandscapeRight) {

            isLandscape = YES;

        }

    }

    // now layout the views depending on if it is landscape or not

    CGFloat const buttonSize = 115;
    if (isLandscape) {

        CGFloat const columnWidth = CGRectGetWidth([[self view] bounds]) / 3.0;
        CGFloat const leftColumnX = (columnWidth - buttonSize) / 2.0;
        CGFloat const middleColumnX = columnWidth + leftColumnX;
        CGFloat const rightColumnX = columnWidth * 2 + leftColumnX;

        CGFloat const rowHeight = CGRectGetHeight([[self view] bounds]) / 2.0;
        CGFloat const topRowY = (rowHeight - buttonSize) / 2.0;
        CGFloat const bottomRowY = rowHeight + topRowY;

        [[self viewOne] setFrame:CGRectMake(leftColumnX, topRowY, buttonSize, buttonSize)];
        [[self viewTwo] setFrame:CGRectMake(middleColumnX, topRowY, buttonSize, buttonSize)];
        [[self viewThree] setFrame:CGRectMake(rightColumnX, topRowY, buttonSize, buttonSize)];
        [[self viewFour] setFrame:CGRectMake(leftColumnX, bottomRowY, buttonSize, buttonSize)];
        [[self viewFive] setFrame:CGRectMake(middleColumnX, bottomRowY, buttonSize, buttonSize)];
        [[self viewSix] setFrame:CGRectMake(rightColumnX, bottomRowY, buttonSize, buttonSize)];

    } else {

        CGFloat const columnWidth = CGRectGetWidth([[self view] bounds]) / 2.0;
        CGFloat const leftColumnX = (columnWidth - buttonSize) / 2.0;
        CGFloat const rightColumnX = columnWidth + leftColumnX;

        CGFloat const rowHeight = CGRectGetHeight([[self view] bounds]) / 3.0;
        CGFloat const topRowY = (rowHeight - buttonSize) / 2.0;
        CGFloat const middleRowY = rowHeight + topRowY;
        CGFloat const bottomRowY = rowHeight * 2 + topRowY;

        [[self viewOne] setFrame:CGRectMake(leftColumnX, topRowY, buttonSize, buttonSize)];
        [[self viewTwo] setFrame:CGRectMake(rightColumnX, topRowY, buttonSize, buttonSize)];
        [[self viewThree] setFrame:CGRectMake(leftColumnX, middleRowY, buttonSize, buttonSize)];
        [[self viewFour] setFrame:CGRectMake(rightColumnX, middleRowY, buttonSize, buttonSize)];
        [[self viewFive] setFrame:CGRectMake(leftColumnX, bottomRowY, buttonSize, buttonSize)];
        [[self viewSix] setFrame:CGRectMake(rightColumnX, bottomRowY, buttonSize, buttonSize)];

    }

}

从这里,您的意见将布局正确对任何设备,因为布局是依赖的宽度和高度的视图边界。 这里是一些截图︰

iPhone 4:

iPhone 4 Portrait iPhone 4 Landscape

iPhone 5:

iPhone 5 Portrait iPhone 5 Landscape

iPhone 6 +:

iPhone 6+ Portrait iPhone 6+ Landscape

官方微信
官方QQ群
31647020