背景视图在横向模式下无法正确缩放

时间:2017-04-28 15:50:18

标签: ios objective-c xcode

由于某种原因,每页上的背景图像大约是穿过顶部并延伸到顶部的3/4,几乎就像它的肖像而不是风景,这在iOS 10 / XCode 8之前很好

我在Util文件中调用了很多值,在这里;

static float getDevicePosX( float PadPosX )
{
    AppDelegate* appDelegate = [[UIApplication sharedApplication] delegate];    
    CGRect f = appDelegate.window.frame;
    float width = f.size.width > f.size.height ? f.size.width : f.size.height;
    return PadPosX / 2.0f * width / 1024.0f;
}

static float getDevicePosY( float PadPosY )
{
    AppDelegate* appDelegate = [[UIApplication sharedApplication] delegate];
    CGRect f = appDelegate.window.frame;
    float height = f.size.width < f.size.height ? f.size.width : f.size.height;
    return PadPosY / 2.0f * height / 768.0f;
}

static float getDevicePosW( float PadPosW )
{
    AppDelegate* appDelegate = [[UIApplication sharedApplication] delegate];
    CGRect f = appDelegate.window.frame;
    float height = f.size.width < f.size.height ? f.size.width : f.size.height;
    return PadPosW / 2.0f * height / 768.0f;
}

static float getDevicePosH( float PadPosH )
{
    AppDelegate* appDelegate = [[UIApplication sharedApplication] delegate];
    CGRect f = appDelegate.window.frame;
    float height = f.size.width < f.size.height ? f.size.width : f.size.height;
    return PadPosH / 2.0f * height / 768.0f;
}

其他按钮通常都在正确的位置,它似乎只是背景图像文件的问题。

我如何初始化其中一个背景的一个例子就是这个;

UIImage* imageBackground = [UIImage imageNamed: @"logo_background"];
    UIImageView *viewBackground = [[UIImageView alloc] initWithFrame: CGRectMake(0, 0, self.view.frame.size.height, self.view.frame.size.width)];
    [viewBackground setImage:imageBackground];
    [self.view addSubview: viewBackground];

确切的呼叫是;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view.

        UIImage* imageBackground = [UIImage imageNamed: @"logo_background"];
        UIImageView *viewBackground = [[UIImageView alloc] initWithFrame: CGRectMake(0, 0, self.view.frame.size.height, self.view.frame.size.width)];
        [viewBackground setImage:imageBackground];
        [self.view addSubview: viewBackground];

        UIImage* imageNameText = [UIImage imageNamed: @"logo_name_textfield"];
        UIImage* imageNameLabel = [UIImage imageNamed: @"logo_namelabel"];
        UIImage* imageNextButton = [UIImage imageNamed: @"logo_next_button"];

1 个答案:

答案 0 :(得分:0)

我设法使用诺曼德上面发布的答案来解决这个问题,但该怎么办呢;

交换

UIImageView *viewBackground = [[UIImageView alloc] initWithFrame: CGRectMake(0, 0, self.view.frame.size.height, self.view.frame.size.width)];

使用

UIImageView *viewBackground = [[UIImageView alloc] initWithFrame:self.view.bounds];

但是将以下内容添加到顶级视图

+ (CGRect)screenBounds {
    CGRect bounds = CGRectMake(0, 0, 0, 0);
    bounds.size = [self screenSize];
    return bounds;
}

+ (CGSize)screenSizeForOrientation:(UIInterfaceOrientation)orientation {
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1) {
        UIInterfaceOrientation current = [UIApplication sharedApplication].statusBarOrientation;
        if (UIInterfaceOrientationIsLandscape(current)) {
            return (UIInterfaceOrientationIsLandscape(orientation)) ? screenSize : CGSizeMake(screenSize.height, screenSize.width);
        } else {
            return (UIInterfaceOrientationIsLandscape(orientation)) ? CGSizeMake(screenSize.height, screenSize.width) : screenSize;
        }
    } else {
        return (UIInterfaceOrientationIsLandscape(orientation)) ? CGSizeMake(screenSize.height, screenSize.width) : screenSize;
    }
}

+ (CGRect)screenBoundsForOrientation:(UIInterfaceOrientation)orientation {
    CGRect bounds = CGRectMake(0, 0, 0, 0);
    bounds.size = [self screenSizeForOrientation:orientation];
    return bounds;
}

+ (CGSize)screenSize {
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    if ((NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1) && UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
        return CGSizeMake(screenSize.height, screenSize.width);
    } else {
        return screenSize;
    }
}

我将此修复归功于一个网站,我在搜索答案时找到了,在这里;

http://perrymitchell.net/article/ios-screen-size-bounds-normalisation/

也谢谢诺德。