hellosheikh hellosheikh - 10 months ago 53
Swift Question

how to to set button horizontally in a center

I am trying to create a button programmatically and want to have the button slightly above from the bottom and space from right and left. At the moment button is displaying like this

enter image description here

here is the code

func showbutton(){
let button = UIButton(type: .System) // let preferred over var here
button.frame = CGRectMake(8, 550, 415, 50)

button.backgroundColor = UIColor.greenColor()
button.setTitle("Add new request", forState: UIControlState.Normal)
//button.addTarget(self, action: "Action:", forControlEvents: UIControlEvents.TouchUpInside)
// Set background color to see if label is centered




This will place a space of 8 points on the Left and Right while keeping all your other dimensions the same

let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width

button.frame = CGRectMake(8, 550, screenSize.width-16, 50)