Ios 使用CALayer定位按钮不起作用

Ios 使用CALayer定位按钮不起作用,ios,objective-c,ios7,core-graphics,core-animation,Ios,Objective C,Ios7,Core Graphics,Core Animation,我在界面生成器中添加了一个图像按钮,我想使用CGPointMake定位它,因此我将按钮IBOutlet添加到.h文件中,然后在.m文件中添加了一个CALayer,这是我的代码… 在.h文件中 // This is the .h file @interface ViewController : UIViewController { IBOutlet UIButton *thebtn; } 在.m文件中 // This is the .m file - (void)viewDidLoad {

我在界面生成器中添加了一个图像按钮,我想使用CGPointMake定位它,因此我将按钮IBOutlet添加到.h文件中,然后在.m文件中添加了一个CALayer,这是我的代码…
在.h文件中

// This is the .h file
@interface ViewController : UIViewController
{
   IBOutlet UIButton *thebtn;
}
在.m文件中

// This is the .m file
- (void)viewDidLoad
{
   CALayer *btn = thebtn.layer;
   btn.position = CGPointMake(480, 150);
   btn.opacity = 0.4f;
   [self.view.layer addSublayer:btn];
}
这很好,但是我的问题是按钮位置没有改变,但是按钮不透明度改变了,那么我如何解决这个问题呢?

提前感谢

我认为,为了更改视图的位置和不透明度,您最好使用
UIView
对象(
UIButton
在您的情况下),而不是它的
CALAyer
属性。这应该起作用:

- (void)viewDidLoad
{
   thebtn.center = CGPointMake(x, y);
   thebtn.alpha = 0.4f;
}

尝试执行以下操作:

首先,将代码移动到
viewdide
。去掉
代码。另外,添加
setNeedsLayout
call。以下是一个例子:

-(void)viewDidAppear 
{
    thebtn.center = CGPointMake(480, 150);
    thebtn.opacity = 0.4f;
    [self setNeedsLayout];
}
更新

我知道问题出在哪里了。您必须关闭“自动布局”。之后,视图将重新定位到所需的位置

请看


希望这有帮助

你有什么理由使用图层而不是视图吗?它向我显示了这个错误(“视图控制器”没有可见的@interface…)你能和我共享你的项目吗?如果我自己调试的话会更容易。我把它上传到dropbox这里是链接Hi@KevinMartin,我找到了。请看我的最新答案。谢谢!我禁用了自动布局,它也和CALayer一起工作