【问题标题】:UIView frame.width is incorrect correct size (on iOS 6 devices)UIView frame.width 大小不正确(在 iOS 6 设备上)
【发布时间】:2015-03-19 22:19:54
【问题描述】:

我们正在尝试以编程方式将 UIView(FB Like 按钮)添加到我们游戏在 iOS 上的主视图(* 游戏是使用 Unity 构建的)。

添加点赞按钮的代码:

like = [[FBLikeControl alloc] init];

// Calculate position.

int screenWidth = UnityGetGLViewController().view.bounds.size.width;
int screenHeight = UnityGetGLViewController().view.bounds.size.height;

int likeWidth = like.frame.size.width;
int likeHeight = like.frame.size.height;

int xPos = screenWidth - likeWidth;
int yPos = screenHeight - likeHeight;

like.frame = CGRectMake(xPos, yPos, likeWidth, likeHeight);

[UnityGetGLViewController().view addSubview:like];

在我们测试过的大多数设备上,这都能正常工作,并且在正确的位置(右下角)显示按钮。

在一些 iOS 设备(运行 iOS 6)上,按钮被剪切。看起来它的 frame.width 正好等于其正确大小的 0.5(一半)! 但只有宽度......

这可能是什么原因?这是 iOS 6 或特定设备的一些已知错误吗?还是我们的代码不正确?

【问题讨论】:

  • 您看到的width 的值是多少?您确定您没有将framebounds 混淆吗?

标签: ios objective-c iphone uiview ios6


【解决方案1】:

我建议尝试使用 initWithFrame: 方法构造按钮,并为其提供固定的宽度和高度。 我知道这不是最好的答案,但我查看了这个类,它有代码可以在你不提供时计算它的大小。此代码中可能存在错误,但我不确定。

最后的忠告:您应该使用 CGGeometry.h 中的辅助方法检索帧信息:(CGRectGetHeight(), CGRectGetWidth(), CGRectGetMinX(), CGRectGetMinY()。在 int 的情况下使用 CGFloat 也是一种从我的角度来看是个好主意。)

CGFloat screenWidth = CGRectGetWidth(UnityGetGLViewController().view.bounds);
CGFloat screenHeight = CGRectGetHeight(UnityGetGLViewController().view.bounds)

CGFloat likeWidth = CGRectGetWidth(like.frame);
CGFLoat likeHeight = CGRectGetHeight(like.frame);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-23
    • 1970-01-01
    • 2012-09-21
    相关资源
    最近更新 更多