gpt4 book ai didi

ios - 为什么 self.view.frame.size.width 在 iPad 横向模式下显示为 768

转载 作者:行者123 更新时间:2023-12-01 18:10:39 29 4
gpt4 key购买 nike

我正在为 iPad 横向模式创建一个应用程序。我已经为整个屏幕创建了一个 UIView。但它并没有以 1024 的宽度显示整个屏幕。而是以 768 的宽度(表示 0 到 768)显示。

backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[backgroundView setBackgroundColor:[[UIColor greenColor]colorWithAlphaComponent:0.5]];
backgroundView.opaque = NO;
backgroundView.userInteractionEnabled = YES;
backgroundView.clearsContextBeforeDrawing=YES;
[self.view addSubview:backgroundView];

CGFloat x = self.view.frame.size.width;
NSLog(@"size%f",x); // 768

当我在 iPad 2 (7.1) 模拟器中运行时会发生这种情况

请指教。

最佳答案

这段代码是你在 viewdidload 中写的吗?

如果是,则 viewDidLoad 将不具有正确的宽度和高度。您需要将其编写在 viewDidLayoutSubviews 方法中。

喜欢

-(void)viewDidLayoutSubviews {
if(backgroundView == nil) {
backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[backgroundView setBackgroundColor:[[UIColor greenColor]colorWithAlphaComponent:0.5]];
backgroundView.opaque = NO;
backgroundView.userInteractionEnabled = YES;
backgroundView.clearsContextBeforeDrawing=YES;
[self.view addSubview:backgroundView];

CGFloat x = self.view.frame.size.width;
NSLog(@"size%f",x); // 768
}
}

关于ios - 为什么 self.view.frame.size.width 在 iPad 横向模式下显示为 768,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32136521/

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