更改UIView的CALayer的框架(self.view.layer.frame = ...)似乎无效 [英] Changing frame of UIView's CALayer (self.view.layer.frame = ...) appears to have no effect

查看:133
本文介绍了更改UIView的CALayer的框架(self.view.layer.frame = ...)似乎无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确定我在这里缺少一些基本知识。我正在尝试从以下方式访问CALayers hello world代码:

I'm sure I'm missing something basic here. I'm trying out the CALayers 'hello world' code from:

http://www.raywenderlich.com/2502/introduction-to-calayers-tutorial

做第一个示例。 xcode 4.2中的新单视图项目。笔尖/故事板没有变化。导入QuartzCore。将以下代码添加到ViewController.m中的ViewDidLoad中:

Doing the very first example. New single view project in xcode 4.2. No change to the nib/storyboard. Import QuartzCore. Add the following code to ViewDidLoad in the ViewController.m:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.layer.backgroundColor = [UIColor blueColor].CGColor;
    self.view.layer.cornerRadius = 30.0;    
    self.view.layer.frame = CGRectMake(20, 20, 20, 20);
}

我运行此程序(ipad 2或ipad模拟器)并获得全屏蓝色具有圆角的矩形。我希望得到的是一个20x20的蓝色矩形,偏移20/20。

I run this (ipad 2 or ipad simulator) and get a full screen blue rectangle with rounded corners. What I hoped to get was a 20x20 blue rectangle offset by 20/20.

我显然可以控制视图层(如颜色和圆角所示)。但是,调整框架似乎没有影响。在设置之前/之后,我已经将NSLog设置为框架,并且框架已更改。根层的框架是否已锁定到uiview框架?

I'm clearly getting control over the views layer (as shown by the color and rounded corners). However, adjusting the frame seems to have no impact. I've NSLog'ed the frame before/after setting it, and it has changed. Is the frame of the root layer locked to the uiview frame?

我没有强烈的理由来更改视图层框架,我只是想说明一下通过正在发生的事情。希望这是一个简单的问题...

I don't have a strong reason to change the views layers frame, I'm just trying to reason through what is going on. Hopefully this is an easy question...

谢谢!

Paul

推荐答案

实际上,上一个答案(您无法设置uiview.layer.frame,因为它总是填充uiview)接近,但还不够完整。阅读答案后,我注册了原始站点,并评论了该教程有问题。这样一来,我发现在处理此问题的第一遍中已经有我未曾看到的评论。

Actually, the previous answer (you can't set uiview.layer.frame as it always fills the uiview) is close, but not quite complete. After reading the answer, I registered for the original site and to comment that the tutorial had issues. In doing so, I found that there were already comments that I hadn't seen in my first pass that addressed this. Using those, I started doing some testing.

最重要的是,如果将self.view.layer.frame设置代码从viewDidLoad移到viewWillAppear,效果很好。这意味着您可以更改视图根层的框架。但是,如果在viewDidLoad中执行此操作,则稍后将撤消。

The bottom line, if you move the self.view.layer.frame setting code from viewDidLoad to viewWillAppear, it works fine. That means that you can change the frame of the root layer of a view. However, if you do it in viewDidLoad it will be undone later.

但是,先前的答案仍然非常接近。我用NSLog记录了根层的框架和视图的框架。更改根层框架会更改视图框架。因此,view.layer.frame始终填充view.frame的答案是正确的。但是,设置图层框架会重置视图框架以匹配。 (我猜uiview.frame属性只是返回uiview.layer.frame ...)

However, the previous answer is still pretty close. I NSLog'ed the frame of the root layer and the frame of the view. Changing the root layer frame changes the view frame. So, the answer that the view.layer.frame always fills the view.frame is correct. However, setting the layer frame resets the view frame to match. (I'm guessing that uiview.frame property simply returns uiview.layer.frame...)

因此,在2010年到今天之间的某个时间点,环境改变了。具体来说,在viewDidLoad之后和viewWillAppear之前,uiview / layer框架似乎已重置为nib指定的值。这将覆盖viewDidLoad中的所有更改。

So, at some point in time between 2010 and today, something in the environment changed. Specifically, after viewDidLoad and before viewWillAppear the uiview/layer frame appears to be reset to the nib specified value. This overrides any changes in viewDidLoad. Changes made in viewWillAppear appear to stick.

罗宾的答案使我走上了正确的轨道,但我想给出完整的答案。

Robin's answer got me on the right track, but I wanted to spell out the full answer.

这篇关于更改UIView的CALayer的框架(self.view.layer.frame = ...)似乎无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆