从纵向模式到横向模式时,屏幕上的图像放置不正确?

时间:2012-03-06 02:33:24

标签: ios xcode

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

1 个答案:

答案 0 :(得分:1)

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

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

要更改此设置,您需要以编程方式将坐标更改为您认为最适合您的视图的任何喜好。

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

#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

希望这有帮助,欢呼!