【问题标题】:Grabbing iPhone screenWidth and Height for reusability抓取 iPhone 的屏幕宽度和高度以实现可重用性
【发布时间】:2013-09-16 08:21:29
【问题描述】:

我创建了一个这样的类别:

#import "UIViewController+Dimensions.h"

@implementation UIView (Dimensions)

- (double) screenWidth2
{
    return self.frame.size.width;
}

- (double) screenHeight2
{
    return self.frame.size.height;
}


@end

但是当我尝试在导入它后在我的视图控制器中调用它时,我得到:

"Use of undeclared identifier screenWidth2;

有没有比我所在的每个视图控制器更好的方法来在一个地方获取屏幕宽度?

然后我在我的视图控制器中这样做了:

 _registerButton.frame = CGRectMake(0.0, 0.0, [self screenWidth2] / 2.0 - 1.0, 60.0);

但应用程序编译运行并崩溃。该类别的.h文件是:

#import <UIKit/UIKit.h>

@interface UIViewController (Dimensions)

- (double) screenWidth2;

- (double) screenHeight2;

@end

【问题讨论】:

  • 您确定在Category's .h 文件中也声明了- (double) screenWidth2 方法吗?
  • 顺便说一句。 1)您应该为您在系统类的类别中提供的方法使用前缀。 2) 您应该使用 CGFloat 而不是 double 作为返回值,特别是现在一些 iOS 设备将在 64 位环境中运行。

标签: ios objective-c ios6 uiviewcontroller ios7


【解决方案1】:

你需要添加

#import "UIViewController+Dimensions.h"

在代码中使用类别(你调用[uiViewControllerInstance screenWidth2]的文件)

【讨论】:

  • 需要添加 [self screenWidth2],我从 UIView 更改为 UIViewController,错过了一个位置
【解决方案2】:

您是否检查过这些变量是否在 .h 中声明?所以,它们是公开的。

另外,要使用类别,请添加:

#import "UIViewController+Dimensions.h"

在必须使用的类中。

【讨论】:

  • 我已经正确声明了,但我尝试这样做:_registerButton.frame = CGRectMake(0.0, 0.0, [self screenWidth2] / 2.0 - 1.0, 60.0);应用崩溃
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-01
  • 2015-09-10
  • 2010-11-25
  • 2012-11-07
  • 1970-01-01
  • 2011-09-25
  • 2012-06-15
相关资源
最近更新 更多