-1

I'm trying to make a button but nothing I do will make it appear on the screen. I've looked at many other posts just like this one but none of the suggestions from those work for me. Any help would be much appreciated.

/*BUTTON: Start button*/
    let startButton = UIButton(type: .RoundedRect)
    startButton.setTitle("Start", forState: UIControlState.Normal)
    startButton.center = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame)-50)
    startButton.backgroundColor = UIColor.blackColor()
    startButton.setBackgroundImage(UIImage(named: "Imaginary Inc Logo"), forState: UIControlState.Normal)

    self.view!.addSubview(startButton)
Ulthran
  • 288
  • 2
  • 14
  • Possible duplicate of [How do I create a basic UIButton programmatically?](http://stackoverflow.com/questions/1378765/how-do-i-create-a-basic-uibutton-programmatically) – Henson Fang Jan 05 '16 at 03:25

2 Answers2

3

You need to set a frame for your button otherwise your app doesn't know where to position it.

startButton.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
Beau Nouvelle
  • 6,962
  • 3
  • 39
  • 54
  • Thanks. I hadn't realized there was a difference between setting the frame and just centering it. – Ulthran Jan 05 '16 at 02:53
1

Like Beau Nouvelle says, you need to set the button's frame.

You can use his code, then after you add it as a subview, set the center position as you've done:

let startButton = UIButton(type: .RoundedRect)
startButton.setTitle("Start", forState: UIControlState.Normal)
startButton.backgroundColor = UIColor.blackColor()
startButton.setBackgroundImage(UIImage(named: "Imaginary Inc Logo"), 
  forState: UIControlState.Normal)
startButton.frame = CGRect(x: 0, y: 0, width: 50, height: 50)

self.view!.addSubview(startButton)
startButton.center = CGPointMake(CGRectGetMidX(self.frame), 
  CGRectGetMidY(self.frame)-50)
Duncan C
  • 128,072
  • 22
  • 173
  • 272