2013-10-25 24 views
0

我在界面生成器中添加了一個圖像按鈕,我想使用CGPointMake來定位它,所以我將按鈕IBOutlet添加到.h文件中,然後在.m文件中添加一個CALayer,在這裏是我的代碼...
在.m文件使用CALayer不工作定位按鈕

// 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]; 
} 

.h文件中

// This is the .h file 
@interface ViewController : UIViewController 
{ 
    IBOutlet UIButton *thebtn; 
} 

所以這是偉大的,但我的問題是,按鈕位置沒有變化,但該鍵不透明度改爲那麼我該如何解決這個問題?
由於提前

+0

是否有任何理由使用圖層而不是視圖? – LuisCien

回答

1

儘量做到以下幾點:

首先,你的代碼移到viewDidAppear。擺脫layer的代碼。另外,請撥打setNeedsLayout電話。這裏有一個例子:

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

UPDATE

我想通了什麼問題了。您必須關閉「自動佈局」。之後,您的視圖將重新定位到您想要的位置。

請參閱this link to see how to disable Auto Layout

希望這有助於!

+0

它顯示了我這個錯誤(沒有可見@interface的'視圖控制器'...) – AFB

+0

你可以與我分享你的項目嗎?如果我在我的最後調試它會更容易。 – LuisCien

+0

好了一會兒 – AFB

1

我覺得這樣的目的,改變視圖的位置和透明度,你最好用UIView對象操作(UIButton你的情況),而不是它的CALAyer財產。這應該工作:

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

我試過這個,但位置並沒有改變 – AFB