To create a button at run time in swift please use the followin code:
this code will help you in creating a button at run time. and also change the background color of the button in highlihted State..
To change the button background at the time of touch we change the background image of the button.
let myCustomButton = UIButton()
myCustomButton .setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
myCustomButton.setTitle("Refer a Friend", forState: UIControlState.Normal)
myCustomButton.frame = CGRectMake((self.view.bounds.size.width/2)-75, 100, 150, 35)
myCustomButton.addTarget(self, action: "myCustomButtonTouched:", forControlEvents: UIControlEvents.TouchUpInside)
myCustomButton.layer.cornerRadius = 5
myCustomButton.layer.borderWidth = 1
let image = UIImage(named: "yourImageName")
myCustomButton.setBackgroundImage(image, forState: UIControlState.Highlighted)
myCustomButton.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Highlighted)
myCustomButton.layer.borderColor = UIColor.orangeColor().CGColor
self.view.addSubview(referMystroButton)
0 Comment(s)