【问题标题】:Why self.view.frame.size.width is showing 768 in iPad landscape mode为什么 self.view.frame.size.width 在 iPad 横向模式下显示 768
【发布时间】:2015-08-21 09:07:25
【问题描述】:

我正在为 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 objective-c ipad landscape device-orientation


    【解决方案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
    }
    }
    

    【讨论】:

    • 在 viewDidAppear 中,我正在调用 loadView 函数。在 loadView 我正在这样做。
    • loadView 也是一个内置函数,它会在系统 viewDidLoad 之前自动调用...您应该将其名称更改为其他名称.. .
    • 抱歉,这是 loadMyView 函数。
    【解决方案2】:

    我记得苹果改变了两个 iOS 版本之间的方向处理方式,当时可能是 7 和 8。在 7 中,宽度始终是设备的宽度,无论它处于什么方向,而在 8 中,它考虑了方向。所以简而言之,它是你应该在横向模式 iOS7 中用于“宽度”的“高度”。

    【讨论】:

      猜你喜欢
      • 2014-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      相关资源
      最近更新 更多