【问题标题】:iOS 6 - XCode 4.5.1 - iPad simulator - programatically getting wrong resolutioniOS 6 - XCode 4.5.1 - iPad 模拟器 - 以编程方式获取错误分辨率
【发布时间】:2012-10-14 04:04:11
【问题描述】:

为什么 XCode 4.5.1 中的 Ipad 模拟器在以编程方式签出时显示错误的大小?

MainViewController.m

- (void)viewDidLoad{
    [super viewDidLoad];

    [self.view setBackgroundColor:[UIColor redColor]];

    NSLog(@"self.view width : %f",self.view.frame.size.width);
    NSLog(@"self.view height : %f", self.view.frame.size.height);

    mTextView  =   [[UITextView alloc] initWithFrame:CGRectMake(10.0, 10.0, self.view.frame.size.width - 20, self.view.frame.size.height/2)];
    [self.view addSubview:mTextView];
}

我得到的输出是

self.view width : 320.000000
self.view height : 548.000000

我的项目规范

  1. 通用项目
  2. 以 iOS 6.0 为目标
  3. 我绝对确定我选中了“targeted for ipad” 创建 MainViewController 时的复选框
  4. 不知道是否重要,但“使用 xib 为用户界面” 未选中。

这是一个添加了文本视图的屏幕截图

【问题讨论】:

    标签: ios xcode ipad ios6 simulator


    【解决方案1】:

    试着等到:

    - (void)viewWillAppear
    

    在设置 UITextView 的框架之前。根据this post,在那之前您的视图将没有有效的几何图形。

    【讨论】:

    • 是的,当我在 viewWillAppear 中设置框架时,它可以正常工作。但令我惊讶的是,我以前在 viewDidLoad 中进行了很多视图设置,但从未遇到过这种情况.. 这是 iOS 6 中的新变化吗?
    • 我不这么认为。界面生成器中视图的尺寸是多少?
    • 我没有使用界面生成器。正如我在问题中所说,选项 "with xib for user interface" 未被选中。
    • 好的,我猜如果你有一个 xib,大小可能来自 viewDidLoad 阶段的那个,而且如果你使用 - (id)initWithFrame:(CGRect)frame 创建你的 viewController 那么你可能会在这个阶段。
    • 我想,你误解了 UIViewController 和 UIView,UIViewController 没有initWithFrame 方法。无论如何,我接受你的回答,因为它为我解决了问题..
    【解决方案2】:

    试试这个代码来确定你的计算机理解的设备:

    NSRange*  pointRange = [[[UIDevice currentDevice] model] rangeOfString:@"iPad" options:0];
    
    if (pointRange.location == NSNotFound) {
    
        NSLog(@"iPhone");
    }
    else
    
      {
        NSLog(@"iPad");
    
    }
    

    【讨论】:

      猜你喜欢
      • 2014-11-06
      • 1970-01-01
      • 2017-05-25
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-25
      相关资源
      最近更新 更多