Search code examples
iosobjective-cuibuttonsubviews

Programmatic Custom UIButton Does Not Appear After Timer


I have been trying to write a program in which a button appears after a timer fires. The timer is working properly, as is the firing event (I have tested this with different events). However, the button I am making appear is not showing up. Also, I would like the button background image to change when the button is pressed and held. I have tried making the button just have a background color instead of an image, but that does not work either. The code is located in the Timer selector within the view controller Any help is appreciated. Thanks!

- (void)timerFireMethod:(NSTimer *)timer
{
Button = [UIButton buttonWithType:UIButtonTypeCustom];
Button.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);
[Button setBackgroundImage:[UIImage imageNamed:@"buttonUnheldBGImage"]
                                      forState:UIControlStateNormal];
[Button setBackgroundImage:[UIImage imageNamed:@"buttonHeldBGImage"]
                                      forState:UIControlStateHighlighted];
[Button setTitle:@"Press and Hold" forState:UIControlStateNormal];
[Button addTarget:self action:@selector(aSelector:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:Button];
}

The timer class that calls this is:

- (void)timer
{

globalTimer = [NSTimer scheduledTimerWithTimeInterval:5
                                      target:self
                                    selector:@selector(timerFireMethod:)
                                    userInfo:Nil
                                     repeats:NO];

}

globalTimer is a global NSTimer


Solution

  • You have set the center of the button but you also need the frame of it, i believe that the button is showing now but his frame is (0,0).