UIView 以编程方式添加全屏错误大小 [英] UIView added programmatically full screen wrong size

查看:40
本文介绍了UIView 以编程方式添加全屏错误大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我进入细节之前,我想提一下,我已经尝试了所有关于添加 LaunchImages/LaunchScreen.xib 等的教程,但没有一个对我有用.也许我做错了什么,所以如果你认为我错过了什么,请告诉我.

Before I get into the details I want to mention that I've tried all the tutorials about adding LaunchImages/LaunchScreen.xib, etc. and none of that has worked for me. Maybe I did something wrong so please let me know if you think I missed something.

所以我的大部分视图都是使用 Interface Builder/Auto Layout 创建的,并且它们可以缩放到适合所有模拟器(iPhone 4s-iPhone 6s plus)的大小.

So most of my views are created using Interface Builder/Auto Layout and they scale to the right size for all the emulators, iPhone 4s-iPhone 6s plus.

然而,对于我的一个观点,我的做法有点不同,无论我在哪个模拟器上运行它,它似乎都保持在 iPhone 4S 的屏幕尺寸上.(也就是视图周围有空白)

However, for one of my views I do it a little bit differently, and it seems to stay at the iPhone 4S screen size regardless of which emulator I run it on. (Aka it has white space around the view)

这就是我所做的:

我有一个 CustomView.XIB,我使用 AutoLayout 添加了一堆按钮/标签/等.

I have a CustomView.XIB and I added a bunch of buttons/labels/etc using AutoLayout.

我还制作了 CustomView.h 和 .m 文件:

I also make CustomView.h and .m files:

- (instancetype)initWithNib {
    self = [super initWithFrame:[[UIScreen mainScreen] bounds]];
    if (self) {

        NSLog(@"Screen bounds height in view: %f",[UIScreen mainScreen].bounds.size.height);

        NSBundle *bundle = [NSBundle bundleForClass:[self class]];
        UINib *nib = [UINib nibWithNibName:@"CustomView" bundle:bundle];
        CustomView *view = [nib instantiateWithOwner:self options:nil][0];

        [self addSubview:view];
    }
    return self;
}

在我的 CustomViewController.m 文件中,我有:

and in my CustomViewController.m file i have:

currentCustomView = [[CustomView alloc] initWithNib];
[self.view addSubview:currentCustomView];

所以实际上我不认为 [[UIScreen mainScreen] bounds] 是错误的.CustomViewController 和 CustomView 都返回相同的错误绑定.为什么 ViewController 的边界不正确?为什么 iPhone 4s 总是有界限,而不是随着不同的模拟器而变化?

So actually I don't think the [[UIScreen mainScreen] bounds] is wrong. Both the CustomViewController and the CustomView returns the same incorrect bound. Why is the ViewController's bounds incorrect? Why is it always the iPhone 4s bounds rather than changing with different emulators?

我在图像资产中有我的启动图像:

I have my Launch images in images assets:

在属性部分:

另一个问题:如果我只支持 IOS 9 及更高版本,在我看来我不需要 iOS 6 和之前的启动图像.但是,它在某些模拟器版本上无法正常工作.但是我以为所有的iPhone 4s-6s都可以支持iOS9,为什么他们会这样设置?

Another question: If I am only supporting IOS 9 and above, it would seem to me that I don't need the iOS 6 and Prior launch images. BUT then it wouldn't work correctly on some emulator versions. But I thought all iPhones 4s-6s can support iOS9 so why do they set it up like this?

推荐答案

在尝试了 2 天不同的事情后,刚刚想出了如何做到这一点.

JUST figured out how to do this after 2 DAYS of trying out different things.

对于阅读本文的人,甚至不必担心发布图片和所有爵士乐.只需使用 AutoLayout,它就会为您缩放视图控制器中的视图.我只需要这样做:

For people who read this, dont even worry about launch images and all that jazz. Just use AutoLayout and it will scale views in your viewcontroller for you. I just had to do:

[查看 setTranslatesAutoresizingMaskIntoConstraints:NO];

[view setTranslatesAutoresizingMaskIntoConstraints:NO];

然后是我想要的 NSLayoutConstraints.但关键是我需要在添加子视图后对 CustomViewController 和 CustomView 内部执行此操作.看看我上面的代码,看看为什么我需要这样做.

And then the NSLayoutConstraints I want. But the key is I need to do this for both the CustomViewController AND inside my CustomView after I add the subview. Take a look at my code above to see why I need to do this.

这篇关于UIView 以编程方式添加全屏错误大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆