1

当我尝试使用 获取主屏幕的边界时[[UIScreen mainScreen] bounds],它返回 null。我也试过了[[UIScreen applicationFrame] bounds],仍然返回null。

奇怪的是;如果我这样做NSLog(@"%@", [UIScreen mainScreen]);,它会打印出正确的界限,尽管我不知道如何解析它,如果它甚至可能的话。

以下是相关代码:

-(void)loadView {
    [super loadView];
    CGRect pagingFrame = [[UIScreen mainScreen] bounds]; 
    NSLog(@"%@", [[UIScreen mainScreen] bounds]); //prints null for me
    NSLog(@"Width of original frame = %d", pagingFrame.size.width); //prints out 0
    pagingFrame.origin.x -= pagingXOrigin;
    pagingFrame.size.width += pagingWidthAdd;
    NSLog(@"Width of adjusted frame = %d", pagingFrame.size.width); //still prints out 0
    pagingScrollView = [[UIScrollView alloc] initWithFrame:pagingFrame];
    pagingScrollView.pagingEnabled = YES;
    pagingScrollView.backgroundColor = [UIColor blackColor];
    pagingScrollView.contentSize = CGSizeMake(pagingFrame.size.width * [self imageCount], pagingFrame.size.height);
    self.view = pagingScrollView;
    //Initialize sets
    recycled = [[NSMutableSet alloc] init];
    visible = [[NSMutableSet alloc] init];
    [self tilePages];
}

pagingScrollView是在头文件中声明的,因为是回收的和可见的。

我正在使用 Xcode 3 和 iOS SDK 4.1。

我确信有一个非常简单的解决方案,但是我仍然是 Objective-C 的新手,因此我无法解决它。

谢谢

4

3 回答 3

17

NSStringFromCGRect方法使快速记录 CGRect 变得更加容易。有关更多信息,请参阅Apple 的文档,但这里有一个代码片段:

CGRect screenBounds = [[UIScreen mainScreen] bounds];
NSLog(@"%@", NSStringFromCGRect(screenBounds));
于 2010-10-13T17:35:34.570 回答
9

bounds是一个struct,不是一个对象。您不能使用%@格式说明符输出它。同样,结构的成员是浮点数,而不是整数。如果你用 输出它们%d,你一定会得到错误的结果。

于 2010-10-13T15:27:26.593 回答
1

为了进一步解释,“%@”通过在对象上调用 [obj description] 并使用从该对象返回的字符串来格式化 NSObjects。因此,在您尝试使用 %@ 格式化字符串之前,请确保您实际处理的是 NSObject 而不是其他类型。

于 2010-10-13T15:30:43.460 回答