gpt4 book ai didi

ios - iOS 中包含的 View Controller 的大小裁剪 91 像素

转载 作者:行者123 更新时间:2023-11-28 20:23:29 25 4
gpt4 key购买 nike

我正在试验一个包含的 View Controller ,但即使是最简单的形式也有问题。基本上,它会切断底部的 90 个像素,我不确定为什么。

我已经包含了我的代码和屏幕截图,旁边有一个类似大小的 UIView。这个 90 像素的 clipping 在不同尺寸下保持一致(即,如果高度为 200px,则显示为 110 像素)。任何帮助将不胜感激,因为这变得非常令人沮丧。为什么它的大小是这个大小,我该如何解决?

提前谢谢

- (void)viewDidLoad
{
// this is the left one below that is the problem
[super viewDidLoad];
self.myView=[[JtViewController alloc] initWithNibName:@"JtViewController" bundle:nil];
self.myView.view.frame=CGRectMake(10.0f,10.0f,100.0f,100.0f);
self.myView.view.backgroundColor=[UIColor orangeColor];
[self addChildViewController:self.myView]; // 1
[self.view addSubview:self.myView.view];
[self.myView didMoveToParentViewController:self.myView]; // 3


// this one is ok
self.testView=[[UIView alloc] init];
self.testView.frame=CGRectMake(150.0f,10.0f,100.0f,100.0f);
self.testView.backgroundColor=[UIColor whiteColor];
[self.view addSubview:self.testView];

enter image description here

编辑1

我已经更新了代码,因此前 4 条评论实际上是不相关的

编辑2

@implementation JtViewController

- (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 from its nib.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

最佳答案

这是在 - (void)viewDidLoad 中设置 subview 边界的常见错误

- (void)viewDidLoad; This is an exceptionally good place to put a lot of setup code. But be careful because the geometry of your view (its bounds) is not set yet. If you need to initialize something based on the geometry of the view, use the - (void)viewWillAppear:(BOOL)animated; method

- (void)viewWillAppear:(BOOL)animated; When this is called, your bounds has been set (via your frame by your superview or some such). Your view will probably only get “loaded” once, but it might appear and disappear a lot. So don’t put something in this method that really wants to be in viewDidLoad. Otherwise, you might be doing something over and over unnecessarily. Use this to optimize performance by waiting until this method (i.e. just before view appears) to kick off an expensive operation (might have to put up a spinning “loading” icon though).

为了更好的理解请引用Stanford lecture和 Apple 文档 Responding to Display-Related Notifications

所以只需将设置 subview 框架移动到- (void)viewWillAppear:(BOOL)animated; 方法

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.myView.view.frame=CGRectMake(10.0f,10.0f,100.0f,100.0f);
}

P.S 我注意到另一个错误(也许只是打字错误):

[self.myView didMoveToParentViewController:self.myView];

应该是:

[self.myView didMoveToParentViewController:self];

关于ios - iOS 中包含的 View Controller 的大小裁剪 91 像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15009400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com