Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Change the button background color when highlighted in swift

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 5.15k
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: