ios - 从纵向模式转为横向模式时,屏幕上的图像位置不佳?

标签 ios xcode

我正在创建一个应用程序,一切都很顺利,但是当我旋转设备以横向显示时,图像不居中,看起来不正常。显然有一种方法可以解决这个问题。任何提示将不胜感激。提前致谢。

最佳答案

如果您使用 NIB 文件来定义 View ,则所有对象都将硬编码到具有长度和宽度尺寸的特定 (X,Y) 坐标。

更改方向时,将应用相同的坐标和尺寸。

要更改此设置,您需要以编程方式将坐标更改为您认为最适合您的 View 的坐标。

我的一个应用程序中的一些示例代码:

#pragma mark -
#pragma mark Orientation Support

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation) && self.portraitMode ) {
        adView.currentContentSizeIdentifier = (&ADBannerContentSizeIdentifierPortrait != nil) ? 
            ADBannerContentSizeIdentifierLandscape : ADBannerContentSizeIdentifier480x32;

        if (self.bannerIsVisible)
            adView.frame = CGRectMake(0, 268, 480, 12);
        else
            adView.frame = CGRectMake(0, 318, 480, 32);

        backgroundImage.frame = CGRectMake(-30, -170, 700, 500);
        bottleImage.frame = CGRectMake(200, 30, 240, 240);
        searchBox.frame = CGRectMake(57, 48, 120, 31);
        pingButton.frame = CGRectMake(50, 100, 133, 66);
        infoButton.frame = CGRectMake(444, 232, 18, 19);

        self.portraitMode = NO;
    } else if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation) && !(self.portraitMode) ) {
        adView.currentContentSizeIdentifier = (&ADBannerContentSizeIdentifierPortrait != nil) ? 
            ADBannerContentSizeIdentifierPortrait : ADBannerContentSizeIdentifier320x50;

        if (self.bannerIsVisible)
            adView.frame = CGRectMake(0, 410, 320, 50);
        else
            adView.frame = CGRectMake(0, 460, 320, 50);

        backgroundImage.frame = CGRectMake(-190, -40, 700, 500);
        bottleImage.frame = CGRectMake(40, 165, 240, 240);
        searchBox.frame = CGRectMake(99, 56, 120, 31);
        pingButton.frame = CGRectMake(92, 99, 133, 66);
        infoButton.frame = CGRectMake(282, 374, 18, 19);

        self.portraitMode = YES;
    }
}

这里是一些关于坐标系的苹果文档:

https://developer.apple.com/library/ios/#DOCUMENTATION/General/Conceptual/Devpedia-CocoaApp/CoordinateSystem.html

希望这有帮助,干杯!

关于ios - 从纵向模式转为横向模式时,屏幕上的图像位置不佳?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9577142/

相关文章:

ios - 如何在 Swift 中使图像与其旁边的文本具有相同的高度?

ios - ios-charts条形图图例中有多个系列(标签)

ios - "Use of unresolved identifier ' Realm '"

iphone - 如何改变UINavigationBar的标签位置(frame)?

objective-c - 为什么在 objective-c 的静态上下文中允许 self

ios - 是否可以在带有图标的表格 View 中列出 iPhone 设备中安装的所有应用程序?

ios - 流式音频 : HTTP Live Streaming a must for app store approval?

ios - 获取日期之间的差异,例如 iOS 上的 Facebook 评论

swift - Xcode 图标在自动完成期间表示什么?

iphone - 如何正确使用 insertRowsAtIndexPaths?