【问题标题】:iOS: self.view.bounds does not fill the whole windowiOS:self.view.bounds 没有填满整个窗口
【发布时间】:2011-08-30 11:41:17
【问题描述】:

我正在做一些将 CALayer 添加到 UIView 的简单测试。在我的 iPhone 4 应用程序的主控制器类中,我实现了 viewDidLoad 方法:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSLog(@"%s", __PRETTY_FUNCTION__);

    CALayer* ca = [[CALayer alloc] init];
    [ca setBounds:self.view.bounds];

    [ca setBackgroundColor:[[UIColor blueColor] CGColor]];

    [self.view.layer addSublayer:ca];
}

蓝色背景仅占屏幕的 1/4。

我想知道是不是因为我没有考虑到视网膜显示?在这种情况下,最佳做法是什么?


添加了这些调试消息:

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

输出:

frame w:320.000000 h:460.000000
bounds w:320.000000 h:460.000000

【问题讨论】:

  • self.view.frame 的值是多少?你能验证一下吗?
  • @Deepak:它是 {width:320, height:460}。也将调试输出添加到问题本身。

标签: ios uiview uiviewcontroller calayer iphone-4


【解决方案1】:

setContentsScale 无效

  [ca setContentsScale:[[UIScreen mainScreen] scale]];

但是,如果我使用

  [ca setFrame:self.view.bounds];

它有效。

【讨论】:

  • +1 好! position 为零,其余内容离开屏幕。
  • @Deepak:我明白了!因此 CALayer 根据位置/锚点定位自身,该位置/锚点位于边界的中心点。非常感谢您的帮助。
  • 你想通了。我只是解释。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-21
  • 2019-01-20
  • 1970-01-01
  • 2015-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多