繁体   English   中英

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

[英]Why self.view.frame.size.width is showing 768 in iPad landscape mode

我正在为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)模拟器中运行时会发生这种情况

请指教。

我记得苹果改变了两个版本的iOS之间的方向处理方式,可能是7和8。 在7中,宽度始终是设备的宽度,而不管设备所处的方向,而在8中,宽度是考虑到方向的。 因此,简而言之,您应该在lanscape模式iOS7中将其“宽度”用于“宽度”。

您是否在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
}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM