Search code examples
iosobjective-cios7core-graphicscore-animation

Positioning a button using CALayer not working


I added an image button into the interface builder and I want to position it using CGPointMake so I added the button IBOutlet into the .h file and then added a CALayer in the .m file, here is my code...
in .h file

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

in .m file

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

So that is great but my issue that the button position is not changing but the button opacity was changed so how can I fix that issue?
Thanks in Advance


Solution

  • Try to do the following:

    First, move your code to viewDidAppear. Get rid of the layer code. Also, add setNeedsLayout call. Here is an example:

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

    UPDATE

    I figured out what the problem was. You have to turn off "Auto-Layout". After that your view will be repositioned to where you want.

    Please see this link to see how to disable Auto Layout

    Hope this helps!