如何在xcode中管理纵向和横向图像?

时间:2013-05-30 07:09:53

标签: iphone xcode ipad

我正在开发适用于ipad的应用程序,肖像图像工作正常但是当我切换到横向它的屏幕时,有没有办法管理图像或自动调整。

2 个答案:

答案 0 :(得分:0)

<。>在.m文件中根据当前方向做事情

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged) name:@"UIDeviceOrientationDidChangeNotification" object:nil];

-(void)orientationChanged {


    UIDeviceOrientation   orientation = [UIDevice currentDevice].orientation;

    CGRect screenBounds = [[UIScreen mainScreen] bounds];

    if(orientation == UIInterfaceOrientationLandscapeLeft)
    {

        if ([[UIDevice currentDevice] userInterfaceIdiom] ==UIUserInterfaceIdiomPhone)
        {
            if (screenBounds.size.height == 568) {
               //for iphone 5 LandscapeLeft
            } else {
               //for iphone 4 LandscapeLeft
            }
        }
        else
        {
            //for ipad LandscapeLeft
        }

    }
    else if(orientation == UIInterfaceOrientationLandscapeRight)
    {
        if ([[UIDevice currentDevice] userInterfaceIdiom] ==UIUserInterfaceIdiomPhone)
        {
            if (screenBounds.size.height == 568) {
                // for iphone 5 LandscapeRight
            } else {
               // for iphone 4 LandscapeRight
            }
        }
        else
        {
            //for ipad LandscapeRight
        }


    }
    else if(orientation == UIInterfaceOrientationPortrait)
    {
        //do change in design when ios change
        if ([[UIDevice currentDevice] userInterfaceIdiom] ==UIUserInterfaceIdiomPhone) {
            if (screenBounds.size.height == 568) {

                // for iphone 5 Portrait
            } else {
                // for iphone 4 Portrait
            }
        } else {
            //for ipad Portrait
        }
    }
    else if(orientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        //Do what you want in Portrait Upside Down
      if ([[UIDevice currentDevice] userInterfaceIdiom] ==UIUserInterfaceIdiomPhone) {
            if (screenBounds.size.height == 568) {
                // for iphone 5 PortraitUpsideDown

            } else {
              // for iphone 4 PortraitUpsideDown  
            }
        } else {
            //for ipad PortraitUpsideDown
        }
    }
}
每当您的设备改变方向时,都会调用

orientationChanged方法。而不是你可以根据你的新方向设置图像。是的,你必须为两个方向画像和景观图像创建你的图像。

答案 1 :(得分:0)

有以下选项: -

1)如果ios 6你可以使用autolayout。 2)您可以单独设置uicontrols的帧,如果ios 6

则使用这两种方法
-(BOOL)shouldAutorotate
{
    return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationLandscapeLeft ;//return UIInterfaceOrientationMaskAll;
}

之后需要像ViewDidlOAd()和viewWillAppear() 这样做

 if(UIInterfaceOrientationIsPortrait([UIApplication sharedApplication ]statusBarOrientation))
{
// change frames
}
else
{
// for landscape change frames here
}

iOS&lt; 6.x的

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation // Deprecated in iOS 6.x 
{
    return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}